• ZOJ 1654 Place the Robots


     题目

       

    Place the Robots

    Time Limit: 5 Seconds      Memory Limit: 32768 KB

    Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following:

    Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them.

    Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map.


    Input


    The first line contains an integer T (<= 11) which is the number of test cases. 

    For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '*', or 'o' which represent Wall, Grass, and Empty, respectively.


    Output

    For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.


    Sample Input

    2
    4 4
    o***
    *###
    oo#o
    ***o
    4 4
    #ooo
    o#oo
    oo#o
    ***#


    Sample Output

    Case :1
    3
    Case :2
    5

    大意

       有一个N*M(N,M<=50)的棋盘,棋盘的每一格是三种类型之一:空地、草地、墙。机器人只能放在空地上。在同一行或同一列的两个机器人,若它们之间没有墙,则它们可以互相攻击。问给定的棋盘,最多可以放置多少个机器人,使它们不能互相攻击

    分析

        

    我们将每一行,每一列被墙隔开,且包含空地的连续区域称作“块”。显然,在一个块之中,最多只能放一个机器人。我们把这些块编上号。

         

    同样,把竖直方向的块也编上号。

      

    代码

      

     1 #include<iostream>
     2 #include<vector>
     3 #include<cstring>
     4 using namespace std;
     5 int n,m;
     6 bool cover[2502];
     7 char mapp[52][52];
     8 int link[2502];
     9 int h[52][52],l[52][52];
    10 vector <int> f[2502];
    11 bool find(int x)  //匹配
    12 {
    13     for (int i=0;i<f[x].size();i++)
    14     {
    15         if (!cover[f[x][i]])
    16         {
    17             cover[f[x][i]]=true;
    18             int q=link[f[x][i]];
    19             link[f[x][i]]=x;
    20             if (q==0||find(q)) return true;
    21             link[f[x][i]]=q;
    22         }
    23     }
    24     return false;
    25 }
    26 int main ()
    27 {
    28     int T;
    29     cin>>T;
    30     for (int k=1;k<=T;k++)
    31     {
    32         cin>>n>>m;
    33         memset(link,0,sizeof(link));
    34         for (int i=0;i<2500;i++) //清空向量
    35            f[i].clear();
    36         string s;
    37         for (int i=1;i<=n;i++)
    38         {
    39             cin>>s;
    40             for (int j=0;j<s.size();j++)
    41                mapp[i][j+1]=s[j];
    42         }
    43         int hh=0,ll=0;
    44         memset(h,0,sizeof(h));
    45         memset(l,0,sizeof(l));
    46         bool flag;
    47         for(int i=1;i<=n;i++)  //构图h
    48         {
    49             flag=0;
    50             for(int j=1;j<=m;j++)
    51             {
    52                 if(mapp[i][j]=='o')
    53                 {
    54                     if(!flag) hh++;
    55                     flag=1;
    56                     h[i][j]=hh;
    57                 }
    58                 else if(mapp[i][j]=='#') flag=0;
    59             }
    60         }
    61         for(int j=1;j<=m;j++)//构图l
    62        {
    63             flag=0;
    64             for(int i=1;i<=n;i++)
    65             {
    66                 if(mapp[i][j]=='o')
    67                 {
    68                     if(!flag) ll++;
    69                     flag=1;
    70                     l[i][j]=ll;
    71                 }
    72                 else if(mapp[i][j]=='#') flag=0;
    73             }
    74         }
    75         for (int i=1;i<=n;i++) //连边
    76             for (int j=1;j<=m;j++)
    77               if (h[i][j]!=0&&l[i][j]!=0)
    78                  f[h[i][j]].push_back(l[i][j]);
    79         int ans=0;
    80         for (int i=1;i<=hh;i++)
    81         {
    82             memset(cover,0,sizeof(cover));
    83             ans+=find(i);
    84         }
    85         cout<<"Case :"<<k<<endl<<ans<<endl;
    86     }
    87     return 0;
    88 }
    为何要逼自己长大,去闯不该闯的荒唐
  • 相关阅读:
    哈哈,原来这叫做“松鼠症”……并谈谈我建议的学习方法
    入坑IT十年(二)技术以外
    入坑IT都快十年了
    也来谈谈IT培训
    技术,是不是越新越好?
    十年之后再看“面向对象”
    话说,你这样根本就不应该去“创业型”公司
    我为什么不做外包
    NetTopologySuite Geometry&WKT&WKB&GeoJSON互转
    wpf 打印图片
  • 原文地址:https://www.cnblogs.com/zjzjzj/p/10159202.html
Copyright © 2020-2023  润新知