ZOJ 1654 Place the Robots

来源:互联网 发布:mac ruby 2.3.0 安装 编辑:程序博客网 时间:2024/05/16 13:48

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


思路:二分匹配,一行变多行,一列变多列;


#include<stdio.h>#include<string.h>int mapx[100][100];int mapy[100][100];int x,y,n,m;int visit[2500];int match[2500][2500];int link[2500];char mmp[100][100];int find(int t){int i;for(i=1;i<=y;i++){if(!visit[i]&&match[t][i]){visit[i]=1;if(!link[i]||find(link[i])){link[i]=t;return 1;}}}return 0;}int main(){int t;while(scanf("%d",&t)!=EOF){int k;for(k=1;k<=t;k++){memset(link,0,sizeof(link));memset(match,0,sizeof(match));memset(mapx,0,sizeof(mapx));memset(mapy,0,sizeof(mapy));scanf("%d %d",&n,&m);getchar();int i,j;for(i=1;i<=n;i++){for(j=1;j<=m;j++){mmp[i][j]=getchar();mapx[i][j]=i;            //记录原来的x;mapy[i][j]=j;            //记录原来的y;(可以用结构体)}getchar();}x=0;for(i=1;i<=n;i++)      //在一行中出现#就重新换一行;{for(j=1;j<=m;j++){if(mmp[i][j]=='#'&&j!=1&&mmp[i][j-1]!='#') x++;    else if(mmp[i][j]=='o') mapx[i][j]+=x;}}y=m+1;for(i=1;i<=m;i++)      //在一列中出现#就重新换一列,和x不一样要从m+1开始;{int g=0;for(j=1;j<=n;j++){if(mmp[j][i]!='#'&&!g) continue;       else if(mmp[j][i]=='#'&&j!=1&&mmp[j-1][i]!='#'){if(!g) g=1;else y++;}else if(mmp[j][i]=='o') mapy[j][i]=y;}y++;}for(i=1;i<=n;i++){for(j=1;j<=m;j++){if(mmp[i][j]=='o'){match[mapx[i][j]][mapy[i][j]]=1;}}}int sum=0;for(i=1;i<=n+x;i++){memset(visit,0,sizeof(visit));if(find(i))sum++;}printf("Case :%d\n",k);printf("%d\n",sum);}}}



0 0
原创粉丝点击