UVA Live 7958 (Codeforces Gym 101201G) Maximum Islands 二分图染色+匹配

本文介绍了一种解决卫星图像中最大岛屿数量问题的算法。通过DFS遍历已知陆地并标记,然后对未知区域(云覆盖部分)进行最优填充以获得最大数量的岛屿。文章详细解释了如何构造二分图并通过匈牙利算法求解最大独立集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://icpcarchive.ecs.baylor.edu/external/79/7958.pdf


7958 Maximum Islands


You are mapping a faraway planet using
a satellite.
Your satellite has captured an image of the planet’s surface. The photographed section can be modeled as a
grid. Each grid cell is either land, water,
or covered by clouds. Clouds mean that
the surface could either be land or water, but we can’t tell.
An island is a set of connected land cells. Two cells are considered connected if they share an edge.
Given the image, determine the maximum number of islands that is consistent with the given
information.


Input


The input fle contains several test cases, each of them as described below.
The frst line contains two space-separated integers
n and m (1 n; m 40).
Each of the next
n lines contains m characters, describing the satellite image. Land cells are denoted
by ‘
L’, water cells are denoted by ‘W’, and cells covered by clouds are denoted by ‘C’.


Output
For each test case, print, on a single line, a single integer indicating the maximum number of islands
that is consistent with the given grid.


Sample Input


5 4
LLWL
CCCC
CCCC
CCCC
LWLL


Sample Output


8


给你一个地图,L表示陆地,W表示水,C表示你可以任意安排,问最多有多少个陆地联通块。


先把所有已知陆地联通块dfs。接着,把不和陆地相邻的所有C区域安排为陆地,最优情况肯定是一个格子为一片陆地。


要构造二分图,就要黑白染色。染色之后,把相邻的C互相连边,对所有C组成的图求最大独立集。

当然,不黑白染色也可以,只是这时会有重边,数组要开够。

说道这里我就要吐槽了!这题亲测,数据范围应该是N<=60,然而题目里却是40,好坑啊!

导致我调到现在,WA了二十几次。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=65,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int match[maxn*maxn];
int p[maxn][maxn],head[maxn*maxn];
bool visit[maxn][maxn],v[maxn*maxn];
char s[maxn][maxn]; 
int dir[4][2];
int n,m,num;

struct Edge {
	int from,to,pre;
};
Edge edge[maxn*maxn*2];

void addedge(int from,int to) {
	edge[num]=(Edge){from,to,head[from]};
	head[from]=num++;
	edge[num]=(Edge){to,from,head[to]};
	head[to]=num++;
}

void dfs(int i,int j) {
	visit[i][j]=1;
	for (int k=0;k<4;k++) {
		int x=i+dir[k][0],y=j+dir[k][1];
		if (x>0&&y>0&&x<=n&&y<=m) 
		if (!visit[x][y]&&s[x][y]=='L') dfs(x,y);
		if (s[x][y]=='C') s[x][y]='W';
	}
}

bool hungary(int now) {
	for (int i=head[now];i!=-1;i=edge[i].pre) {
		int to=edge[i].to;
		if (!v[to]) {
			v[to]=1;
			if (!match[to]||hungary(match[to])) {
				match[to]=now;match[now]=to;
				return true;
			}
		}
	}
	return false;
}

int main() {
	dir[0][0]=dir[1][0]=dir[2][1]=dir[3][1]=0;
	dir[0][1]=dir[2][0]=1;dir[1][1]=dir[3][0]=-1;
	int i,j,k,ans=0;
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++) {
		scanf("%s",s[i]+1);
	}
	mem0(visit);
	for (i=1;i<=n;i++) 
		for (j=1;j<=m;j++) 
			if (!visit[i][j]&&s[i][j]=='L') dfs(i,j),ans++;
	int cnt=0;
	memset(head,-1,sizeof(head));
	num=0;
	for (i=1;i<=n;i++)
	    for (j=1;j<=m;j++) 
	    	if (s[i][j]=='C') 
	    		p[i][j]=++cnt;
	for (i=1;i<=n;i++) 
		for (j=1;j<=m;j++) 
			if (s[i][j]=='C'&&(i+j)%2==0) 
			    for (k=0;k<4;k++) {
			    	int x=i+dir[k][0],y=j+dir[k][1];
			   		if (x>0&&y>0&&x<=n&&y<=m) 
					   if (s[x][y]=='C') addedge(p[i][j],p[x][y]);
			    }
	int sum=0;
	mem0(match);
	for (i=1;i<=cnt;i++) 
		if (!match[i]) {
			mem0(v);v[i]=1;
			if (hungary(i)) sum++;
		}
	ans+=cnt-sum;
	printf("%d\n",ans);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值