zoj 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.
有一个N*M(N,M<=50)的棋盘,棋盘的每一格是三种类型之一:空地、草地、墙。机器人只能放在空地上。在同一行或同一列的两个机器人,若它们之间没有墙,则它们可以互相攻击。问给定的棋盘,最多可以放置多少个机器人,使它们不能互相攻击。

题解

构图一:
每个空地都拆成两个点,然后能够互相攻击的点连线,求最大独立集

构图二:
一行连续的空地(中间不隔着墙)为一个点,一列连续的空地(同上)为一个点,然后行的点与能攻击到的列的点连线,求最大独立集=最大匹配数

代码

#include <cstdio>
#include <cstring>

using namespace std;

int t,n,m,e,ans;
int ls[3000],ne[250000],y[250000],link[3000];
bool cover[3000];
int a[60][60],c[60][60];

void add(int x,int h){
	ne[++e]=ls[x];ls[x]=e;y[e]=h;
}

bool dfs(int k){
	for (int i=ls[k];i;i=ne[i])
	if (!cover[y[i]]){
		int t=link[y[i]];
		link[y[i]]=k;
		cover[y[i]]=1;
		if (t==0||dfs(t)) return 1;
		link[y[i]]=t;
	}
	return 0;
}

int main(){
	scanf("%d",&t);
	for (int id=1;id<=t;id++){		
		scanf("%d%d",&n,&m);
		ans=0;
		for (int i=1;i<=n;i++){
			char b[60];
			scanf("%s",b);
			for (int j=1;j<=m;j++){
				if (b[j-1]=='*') a[i][j]=-1; else
				if (b[j-1]=='#') a[i][j]=-2; else
				{
					a[i][j]=0;
				}
				c[i][j]=0;
			}
		}
		int cnt=0;
		memset(ls,0,sizeof(int)*(n*n));		
		for (int j=1;j<=m;j++){
			int bz=0,bzz=0;
			for (int i=1;i<=n;i++)
			if (a[i][j]==0){
				if (bz==0) cnt++;
				bz=1;bzz=1;
				c[i][j]=cnt;
			} else
			if (a[i][j]==-2&&bz){
				bz=0;
			}
		}
		int cn2=cnt;
		memset(link,0,sizeof(int)*(cn2+3));
		cnt=0;e=0;
		for (int i=1;i<=n;i++){
			int bz=0,bzz=0;
			for (int j=1;j<=m;j++)
			if (a[i][j]==0){
				if (bz==0) cnt++;
				bz=1; bzz=1;
				int k=i;
				while (k>=1){
					if (a[k][j]==0) add(cnt,c[k][j]);
					if (a[k][j]==-2) break;
					k--;
				}
				k=i+1;
				while (k<=n){
					if (a[k][j]==0) add(cnt,c[k][j]);
					if (a[k][j]==-2) break;
					k++;
				}
			}else
			if (a[i][j]==-2&&bz){
				bz=0;
			}
		}
		for (int i=1;i<=cnt;i++){
			memset(cover,0,sizeof(bool)*(cn2+3));
			if (dfs(i)) ans++;
		}
		printf("Case :%d\n",id);
		printf("%d\n",ans);
	}
}

一曲黄梅调朦胧了黄梅天的一季黄梅烟雨,明知不过一枕黄粱,却抵不过油纸伞下青衫转过屋檐的一点心悸,极平,极静,极欢,极喜。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值