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> #define MAXN 2505 int xy[5][2]= {{0,0},{0,1},{0,-1},{-1,0},{1,0}}; bool vis[MAXN]; int pre[MAXN]; struct R { int r,s,t; } r[MAXN]; struct C { int c,s,t; } c[MAXN]; int n1,n2; bool map[MAXN][MAXN]; bool fix(int x,int y,int m,int n) { if (x>=1&&x<=m&&y>=1&&y<=n) return 1; return 0; } bool find(int deal) { int i; for (i=1; i<=n2; ++i) { if (map[deal][i]) { if (vis[i]) continue; else { vis[i]=1; if (pre[i]==-1||find(pre[i])) { pre[i]=deal; return 1; } } } } return 0; } int main() { // freopen("a.txt","r",stdin); bool mark[55][55]; char g[55][55]; int t,cas,i,n,m,j,ti,tj,size,count,total; scanf("%d",&t); memset(g,0,sizeof(g)); for (cas=1; cas<=t; ++cas) { scanf("%d%d",&m,&n); getchar(); for (i=1; i<=m; ++i) { for (j=1; j<=n; ++j) { scanf("%c",&g[i][j]); } getchar(); } total=0; n1=0; memset(mark,0,sizeof(mark)); for (i=1; i<=m; ++i) { for (j=1; j<=n; ++j) { if (g[i][j]=='o'&&!mark[i][j]) { mark[i][j]=1; ti=i; tj=j; n1++; r[n1].s=tj; r[n1].r=i; while (g[ti][tj]!='#'&&fix(ti,tj,m,n)) { mark[ti][tj]=1; tj++; } r[n1].t=tj-1; } } } memset(mark,0,sizeof(mark)); n2=0; for (i=1; i<=m; ++i) { for (j=1; j<=n; ++j) { if (g[i][j]=='o'&&!mark[i][j]) { mark[i][j]=1; ti=i; tj=j; n2++; c[n2].s=ti; c[n2].c=j; while (g[ti][tj]!='#'&&fix(ti,tj,m,n)) { mark[ti][tj]=1; ti++; } c[n2].t=ti-1; } } } for (i=1; i<=n1; ++i) { for (j=1; j<=n2; ++j) { if (r[i].r>=c[j].s&&r[i].r<=c[j].t&&c[j].c>=r[i].s&&c[j].c<=r[i].t&&g[r[i].r][c[j].c]=='o') map[i][j]=1; else map[i][j]=0; } } count=0; memset(pre,-1,sizeof(pre)); for (i=1; i<=n1; ++i) { memset(vis,0,sizeof(vis)); if (find(i)) count++; } printf("Case :%d/n%d/n",cas,count); } return 0; }