题目原文:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1654
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
解题思路:这道题的建图过程略复杂一些,根据题意我们需要考虑空地与空地之间的关系,所以需要把空地和草地分成横向的连续的块(草地可以连续,用墙分割,每一块中至少包含一块空地),和纵向的连续的块,对两种块分别进行编号,然后根据这个编号,对相互有交集的块进行连边,因为连边的的两个块只能取一个,跑二分图最大匹配。
AC代码:
/*
@Author: wchhlbt
@Date: 2017/8/3
*/
#include <bits/stdc++.h>
#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 51
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
#define _ << " " <<
using namespace std;
typedef long long ll ;
const double eps =1e-8;
const int mod = 998244353;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
inline int read(){ int num; scanf("%d",&num); return num;}
int n,m;
char s[maxn][maxn];
int x[maxn][maxn];
int y[maxn][maxn];
//注意给nx,ny赋值
int vis[maxn*maxn];//存储每个点是否被访问过
int g[maxn*maxn][maxn*maxn];//邻接矩阵
int cx[maxn*maxn];//cx[i]表示最终求得的最大匹配中与xi匹配的y顶点
int cy[maxn*maxn];
int nx,ny;//分别存储x集合和y集合中的顶点个数
int dfs(int u)
{
for(int v = 1; v<=ny; v++){//遍历Y集合
if(g[u][v] && !vis[v]){
vis[v] = 1;
if(cy[v]==-1 || dfs(cy[v])){
cx[u] = v;
cy[v] = u;
return 1;
}
}
}
return 0;
}
int MaxMatch()
{
int res = 0;
memset(cx,-1,sizeof cx);
memset(cy,-1,sizeof cy);
for(int i = 1; i<=nx; i++){//遍历X集合
if(cx[i]==-1){
memset(vis,0,sizeof vis);
res += dfs(i);
}
}
return res;
}
void init()
{
memset(x,0,sizeof x);
memset(y,0,sizeof y);
memset(g,0,sizeof g);
nx = ny = 0;
}
int main()
{
//cout << (2<<1) << endl;
int t = read();
for(int kase = 1; kase<=t; kase++){
n = read(); m = read();
for(int i = 1; i<=n; i++)
scanf("%s",s[i]+1);
init();
for(int i = 1; i<=n; i++){//对横向块进行编号
int first = 1;
for(int j = 1; j<=m; j++){
if(s[i][j]=='o'){
if(first) x[i][j] = ++nx, first = 0;
else x[i][j] = nx;
}
else if(s[i][j]=='#') first = 1;
}
}
for(int j = 1; j<=m; j++){//对纵向块进行编号
int first = 1;
for(int i = 1; i<=n; i++){
if(s[i][j]=='o'){
if(first) y[i][j] = ++ny,first = 0;
else y[i][j] = ny;
}
else if(s[i][j]=='#') first = 1;
}
}
for(int i = 1; i<=n; i++){
for(int j = 1; j<=m; j++){
if(x[i][j]) g[x[i][j]][y[i][j]] = 1;
}
}
int ans = MaxMatch();
printf("Case :%d\n", kase);
cout << ans << endl;
}
return 0;
}
/*
unsigned int 0~4294967295
int 2147483648~2147483647
unsigned long 0~4294967295
long 2147483648~2147483647
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:18446744073709551615
__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615
*/