ZJU 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



题意,有这么一块地方,#表示墙,o表示空地,*表示草地

在这里面放机器人(机器人只能放在空地o)

机器人可以朝4个方向放光束,光束可以穿过草地不可以穿过墙。

问在机器人不能互相攻击的条件下,最多能摆放机器人的数量。


假如地图是一个空地,那么,大家应该对这个很熟悉了。摆棋子问题(ZOJ1002)

对于规模较小的此类问题可以用搜索解决。

但是此题的规模太大,所以选择用二分匹配。


1.把图分成2个集合(行、列),以墙为分割点,分割出所有行段和列段(注意:这些段必须至少包含1个o,这样机器人才能站在此段)。

2.求行和列的相交(即2段如果有交点,并且交点是草地o(只有这样才算匹配)

3.求最大匹配


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define max(a,b) a<b?b:a
const int MAXN = 25*51;
int uN, vN;
bool g[MAXN][MAXN];
int xM[MAXN], yM[MAXN]; // 输出量
bool chk[ MAXN];// 辅助量检查某轮y[v]是否被check
bool SearchPath(int u) {
	int v;
	for (v = 0; v < vN; v++)
		if (g[u][v] && !chk[v]) {
			chk[v] = true;
			if (yM[v] == -1 || SearchPath(yM[v])) {
				yM[v] = u;
				xM[u] = v;
				return true;
			}
		}
	return false;
}
int MaxMatch() {
	int u, ret = 0;
	memset(xM, -1, sizeof(xM));
	memset(yM, -1, sizeof(yM));
	for (u = 0; u < uN; u++)
		if (xM[u] == -1) {
			memset(chk, false, sizeof(chk));
			if (SearchPath(u))
				ret++;
		}
	return ret;
}
char str[52][52];
struct point {
	int s, e, l;			//起始点,终点,行列号
} H[MAXN], V[MAXN];			
int Hcnt, Vcnt;				//行列段数量
int main() {
	int t, i, j, n, m,cnt=0;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &m);
		Hcnt = Vcnt = 0;
		int total = (n*m)>>1|1;
		for (i = 0; i < total; i++)
			H[i].s = H[i].e = V[i].s = V[i].e = -1;
		for (i = 0; i < n; i++) //横
		{
			scanf("%s", str[i]);
			j = 0;
			while (j <= m) {
				if (str[i][j] == '#' || str[i][j] == '\0') {
					if (H[Hcnt].e != -1)
					{
						H[Hcnt].l=i;
						Hcnt++;
					}
					else {
						H[Hcnt].s = H[Hcnt].e = -1;
					}
				} else {
					if (H[Hcnt].s == -1)
					{
						if(str[i][j]=='o')
							H[Hcnt].e=j;
						H[Hcnt].s = j;
					}
					else {
						if (H[Hcnt].e != -1 || str[i][j] == 'o')
							H[Hcnt].e = j;
					}
				}
				j++;
			}
		}
		for (i = 0; i < m; i++) //竖
		{
			j = 0;
			while (j <= n) {
				if (str[j][i] == '#' || str[j][i] == '\0') {
					if (V[Vcnt].e != -1)
					{
						V[Vcnt].l=i;
						Vcnt++;
					}
					else {
						V[Vcnt].s = V[Vcnt].e = -1;
					}
				} else {
					if (V[Vcnt].s == -1)
					{
						V[Vcnt].s = j;
						if(str[j][i]=='o')
							V[Vcnt].e = j;
					}
					else {
						if (V[Vcnt].e != -1 || str[j][i] == 'o')
							V[Vcnt].e = j;
					}
				}
				j++;
			}
		}

		uN=Hcnt;
		vN=Vcnt;
		memset(g,0,sizeof(g));
		for(i=0;i<uN;i++)
		{
			for(j=0;j<vN;j++)
			{
				if(!(V[j].e < H[i].l || V[j].s >H[i].l ||H[i].s>V[j].l ||H[i].e <V[j].l)&&str[H[i].l][V[j].l]=='o')
					g[i][j]=true;
			}
		}

		printf("Case :%d\n%d\n",++cnt,MaxMatch());
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值