zoj 1654 Place the Robots

來源:互聯網
上載者:User

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

/*本題運用構建二分圖思想將每一行被牆隔開、且包含空地的連續地區稱作“塊”(每一列同理)。在一個塊之中,最多隻能放一個機器人,把這些塊編上號。把每個橫向塊看作二部圖中頂點集合 X 中的頂點,豎向塊看作集合 Y 中的頂點。由於每條邊表示一個空地(即一個橫向塊和一個豎向塊的公用空地),有衝突的空地之間必有公用頂點。若兩個塊有公用的空地(注意,每兩個塊最多有一個公用空地),則在它們之間連邊。所以問題轉化為在二部圖中找沒有公用頂點的最大邊集,這就是二分圖最大匹配問題。*/#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define maxn 51*51using namespace std;int n,m,ans;          // n-行 m-列 ans-答案int cntx,cnty,flag;   // cntx-橫向塊數  cnty-豎向塊數int g[maxn][maxn],mapx[51][51],mapy[51][51];   // g[][]存二分圖  mapx[][]-原圖對應的橫向序號 mapy[][]同理int cx[maxn],cy[maxn],mk[maxn];  // cx存匹配的cy序號  cy同理  mk標記cy是否匹配char mapp[51][51];    // 存原圖int path(int u){    int v;    for(v=1;v<=cnty;v++)    {        if(g[u][v]&&!mk[v])        {            mk[v]=1;            if(!cy[v]||path(cy[v]))            {                cx[u]=v;                cy[v]=u; //             printf("u:%d v:%d\n",u,v);                return 1;            }        }    }    return 0;}void maxmarch(){    int i,j;    ans=0;    memset(cx,0,sizeof(cx));    memset(cy,0,sizeof(cy));    for(i=1;i<=cntx;i++)    {        if(!cx[i])        {            memset(mk,0,sizeof(mk));            ans+=path(i);        }    }}int main(){int i,j,k,t;scanf("%d",&t);for(k=1;k<=t;k++){    memset(g,0,sizeof(g));    memset(mapp,0,sizeof(mapp));scanf("%d%d",&n,&m);for(i=1;i<=n;i++)        {            getchar();            for(j=1;j<=m;j++)            {                scanf("%c",&mapp[i][j]);            }        }        cntx=0;        for(i=1;i<=n;i++)         // 將X塊抽象出來        {            flag=0;            for(j=1;j<=m;j++)            {                if(mapp[i][j]=='o')                {                    if(flag==0)                    {                        flag=1;                        cntx++;                    }                    mapx[i][j]=cntx;                }                else if(mapp[i][j]=='#')  flag=0;            }        }        cnty=0;        for(j=1;j<=m;j++)         // 將Y塊抽象出來        {            flag=0;            for(i=1;i<=n;i++)            {                if(mapp[i][j]=='o')                {                    if(flag==0)                    {                        flag=1;                        cnty++;                    }                    mapy[i][j]=cnty;                }                else if(mapp[i][j]=='#')  flag=0;            }        }        for(i=1;i<=n;i++)          // 對原圖中空地操作 連邊構建二分圖        {            for(j=1;j<=m;j++)            {                if(mapp[i][j]=='o')                {                    g[mapx[i][j]][mapy[i][j]]=1;                }            }        }        maxmarch();          // dfs求最大匹配模板函數 就不贅述了        printf("Case :%d\n",k);        printf("%d\n",ans);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.