【二分图|最大匹配】ZOJ-1654 Place the Robots

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

————————————————————ハロウィンの分割線————————————————————
思路:首先是转化为最大匹配的过程:
1. 把行和列分别当做顶点(建立二分图)
2. 空地就是 i 行和 j 列之间的边
3. 在某行/列放置了机器人,那么仅能选择列/行顶点中的1个。(每行/列1个机器人)
4. 因此本题求最大匹配
然后是建图过程:
1. 因为有墙壁的阻挡,所以按照机器人的射程将行顶点、列顶点化为“行块”和“列块”
2. 每个空地就是两个“块”之间的边
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 55, M = N*N;
char mat[N][N];
int col[N][N][2], dot, tot, row, n, m, head[M];
bool vis[M];
int cou[M];
struct Edge {
	int v;
	int next;
	Edge(){}
	Edge(int _v, int _next):
		v(_v), next(_next){}
}edge[M];

void init()
{
	row = tot = 0; dot = 0;
	memset(head, -1, sizeof(head));
	memset(mat, '#', sizeof(mat));
	memset(col, -1, sizeof(col));
}

void add(int u, int v)
{
	edge[tot] = Edge(v, head[u]); head[u] = tot++;
}

void Flood()
{
	bool flag;
	for(int i = 1; i <= n; i++) {
		flag = 0;
		for(int j = 1; j <= m; j++) {
			if(mat[i][j] == 'o') {
				if(!flag) dot++;
				col[i][j][0] = dot; flag = 1;
			}
			else if(mat[i][j] == '#') flag = 0;
		}
	}
	row = dot;
	for(int j = 1; j <= m; j++) {
		flag = 0;
		for(int i = 1; i <= n; i++) {
			if(mat[i][j] == 'o') {
				if(!flag) dot++;
				col[i][j][1] = dot; flag = 1;
			}
			else if(mat[i][j] == '#') flag = 0;
		}
	}
}

void build()
{
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			int u = col[i][j][0], v = col[i][j][1];
			if(u != -1 && v != -1) {
				add(u, v);
			}
		}
	}
}

bool dfs(int u)
{
	for(int i = head[u]; ~i; i = edge[i].next) {
		int v = edge[i].v;
		if(!vis[v]) {
			vis[v] = true;
			if(cou[v] == -1 || dfs(cou[v])) {
				cou[v] = u; 
				return true; 
			}
		}
	}
	return false;
}

int match()
{
	int ret = 0;
	memset(cou, -1, sizeof(cou));
	for(int i = 1; i <= row; i++) {
		memset(vis, 0, sizeof(vis));
		if(dfs(i)) ret++;
	}
	return ret;
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	int Case, kase = 1;
	scanf("%d", &Case);
	while(Case--) {
		init();
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= m; j++) {
				scanf(" %c", &mat[i][j]);
			}
		}
		Flood();
		build();
		int ans = match();
		printf("Case :%d\n%d\n", kase++, ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值