ZOJ 1654 Place the Robots(最大匹配)
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
題意:機器人能攻擊跟它所在同一行跟列的全部東西,僅僅有‘o‘才幹放機器人,‘#‘表示墻壁,能擋住機器人的攻擊(意味著墻壁之間能放機器人),要你求出n*m的矩陣上能放多少機器人
思路:最大獨立集。可惜眼下沒有不論什麽算法能求出最大獨立集。
那麽我們換一個思路,之前做過POJ3041,能夠類似地做這道題,可是本題中的墻壁為我們的標記提供了難度。那麽我麽能夠用xs數組看做X集合(表示每行中的‘o‘的位置。假設不相容則標記為同一個數),同理做一個列的ys數組!
相應下來。在每個‘o‘上連接兩集合的點形成邊,我們發現符合題目的每條邊之間不能有公共點,所以就轉化為了最小邊覆蓋的問題,剛好就是最大匹配!
AC代碼:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 55; const int maxn=2600; int link[maxn][maxn]; int xs[N][N],ys[N][N]; int col[maxn],vis[maxn]; int mx,my; int n,m; int match(int x) { int i; for(i=1;i<=my;i++){ if(link[x][i]&&!vis[i]) { vis[i]=1; if(!col[i]||match(col[i])) { col[i]=x; return 1; } } } return 0; } char str[N][N]; int main() { #ifndef ONLINE_JUDGE freopen("in.cpp","r",stdin); freopen("out.cpp","w",stdout); #endif // ONLINE_JUDGE int t; scanf("%d",&t); int cas=1; while(t--) { int cnt=1; scanf("%d %d",&n,&m); memset(xs,0,sizeof(xs)); memset(ys,0,sizeof(ys)); getchar(); for(int i=0;i<n;i++) { scanf("%s",str[i]); for(int j=0;j<m;j++) { if(str[i][j]==‘o‘) { xs[i][j]=cnt; } else if(str[i][j]==‘#‘) cnt++; } cnt++; } int maxx=cnt; mx=cnt; cnt=1; for(int j=0;j<m;j++) { for(int i=0;i<n;i++) { if(str[i][j]==‘o‘) ys[i][j]=cnt; else if(str[i][j]==‘#‘) cnt++; } cnt++; } my=cnt; memset(link,0,sizeof(link)); memset(col,0,sizeof(col)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(str[i][j]==‘o‘) { link[xs[i][j]][ys[i][j]]=1; } } } int tot=0; for(int i=1;i<=mx;i++) { memset(vis,0,sizeof(vis)); if(match(i))tot++; } printf("Case :%d\n",cas++); printf("%d\n",tot); } return 0; }
ZOJ 1654 Place the Robots(最大匹配)