浙大 zoj 1654 Place the Robots (二分匹配 + 关于x,y轴的构造边)

浙大   zoj  1654 Place the Robots  (二分匹配  +  关于x,y轴的构造边)



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






题意:有一块地方,要布置警卫机器人,而机器人能够查询并发着激光武器,它的预警范围是上下左右直线上的地方,要是遇到墙就中断,要是遇到空地和草坪就都算预警
区域,同是在同一行,或者同一列,没有墙隔着的地方,不放两台机器人,因为这样会冲突,激光武器容易摧毁一台,于是问你,最多可以放几台机器人,同是要将
这块区域都预警到
首先输入T,表示有多少测试案例
接着是n,m,表示这块区域的大小
然是n行m列的大小
o表示空地
*表示草地
#表示墙





题解:这也是一种二分匹配,是对x轴和y轴进行匹配,因为在没有墙的时候,同一行和同一列只能存在一台机器人,就相当于一行或者一列只能选取一个
这就是一种二分匹配。
而现在有了墙,所有我们要设计的x和y要进行稍稍的变形
当设计x的时候,从第一列上到下,要是中间有墙,则下面的o空地的数字标记就加1,然后换一列了也要加1
同理,设计y的时候一样,从第一行的左边到右边,要是o空地上,有#墙出现,就在右边的O空地上的数字标记加1,换行也要加1
这样就构造边完成了



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

const int maxn = 1500;

int n, m;
int num1, num2;
char ch[100][100];                        //用来储存墙,草地,空地的位置
int map[maxn][maxn];                        //记录x,y是否能连接
int vis[maxn];
int link[maxn];                            //匹配的标记

struct node{                                //记录每个空地上x轴的数字标记和y轴的数字标记
	int x, y;
}flag[maxn][maxn];

int Find (int x){                            //Find()的匹配遍历
	int i;
	
	for (i = 1; i < num2; ++i){
		if (!vis[i] && map[x][i]){
			vis[i] = 1;
			if (link[i] == -1 || Find (link[i])){
				link[i] = x;
				return 1;
			}
		}
	}
	return 0;
}

int main (){
	int T, i, j, cas = 1;
	
	while (scanf ("%d", &T) != EOF){
		while (T--){
			scanf ("%d%d", &n, &m);
			getchar ();
			memset (map, 0, sizeof (map));
			memset (link, -1, sizeof (link));
			memset (flag, 0, sizeof (flag));
			for (i = 0; i < n; ++i)                            //坐标的标记
				scanf ("%s", ch[i]);
			num1 = 1;
			num2 = 1;
			for (i = 0; i < n; ++i)                         //对x轴的数字标记
			{
				int xx = 0, yy = 0;
				for (j = 0; j < m; ++j)
				{
					if (ch[i][j] == 'o')
					{
						if (xx == 1){
							num1++;
							xx = 0;
						}
						yy = 1; 
						flag[i][j].x = num1;                  //x轴的数字标记放入到flag[][]中去
					}
					else if (ch[i][j] == '#')
					{
						if (yy == 1)
							xx = 1;
					}
				}
				if (yy == 1)
					num1++;
			}
			for (i = 0; i < m; ++i){                                 //对y轴的数字标记
				int xx = 0, yy = 0;
				for (j = 0; j < n; ++j){
					if (ch[j][i] == 'o'){
						if (xx == 1){
							num2++;
							xx = 0;
						}
						yy = 1;
						flag[j][i].y = num2;                         //将构造好的y轴的数字标记放到flag[][]中去
					}
					else if (ch[j][i] == '#'){
						if (yy == 1)
							xx = 1;
					}
				}
				if (yy == 1)
					num2++;
			}
			for (i = 0; i < n; ++i){
				for (j = 0; j < m; ++j){
					if (ch[i][j] == 'o'){
						map[flag[i][j].x][flag[i][j].y] = 1;                  //判断时候x,y分组中,是否能够连接
					}
				}
			}
			int ant = 0;
			for (i = 1; i < num1; ++i){
				memset (vis, 0, sizeof (vis));
				if (Find(i))
					ant++;
			}
			printf ("Case :%d\n", cas++);
			printf ("%d\n", ant);
		}
	}
	
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值