poj2226 muddy fields

这篇博客详细解析了POJ2226 'Muddy Fields'问题,主要涉及图论中的二分图和最小覆盖概念。通过实例分析,介绍了如何求解连通块并应用这些理论来解决问题。

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

Description

Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don't want to get their hooves dirty while they eat. 

To prevent those muddy hooves, Farmer John will place a number of wooden boards over the muddy parts of the cows' field. Each of the boards is 1 unit wide, and can be any length long. Each board must be aligned parallel to one of the sides of the field. 

Farmer John wishes to minimize the number of boards needed to cover the muddy spots, some of which might require more than one board to cover. The boards may not cover any grass and deprive the cows of grazing area but they can overlap each other. 

Compute the minimum number of boards FJ requires to cover all the mud in the field.

Input

* Line 1: Two space-separated integers: R and C 

* Lines 2..R+1: Each line contains a string of C characters, with '*' representing a muddy patch, and '.' representing a grassy patch. No spaces are present.

Output

* Line 1: A single integer representing the number of boards FJ needs.




木板只能放在行或列的泥地联通块里。
把行、列连通块看成左、右部的点,每个泥地对他所在的行、列连通块连一条边。
然后求最小覆盖。




#include<cstdio>
#include<cstring>
#define M(a) memset(a,0,sizeof(a))
int first[70000],next[70000],to[70000],r,c,m,n,num1[60][60],num2[60][60],f[70000];
bool map[60][60],v[70000];
bool dfs(int p)
{
	int i,j,k,x,y,z;
	for (i=first[p];i;i=next[i])
	  if (!v[to[i]])
	  {
	  	v[to[i]]=1;
	  	if (!f[to[i]]||dfs(f[to[i]]))
	  	{
	  		f[to[i]]=p;
	  		return 1;
		}
	  }
	return 0;
}
int main()
{
	int i,j,k,x,y,z,ans;
	bool con;
	char s[60];
	scanf("%d%d",&c,&r);
	for (i=1;i<=c;i++)
	{
		scanf("%s",s+1);
		for (j=1;j<=r;j++)
		  map[i][j]=(s[j]=='*');
	}
	m=0;
	for (i=1;i<=c;i++)
	{
		con=0;
		for (j=1;j<=r;j++)
		  if (!map[i][j])
		    con=0;
		  else
		  {
		  	if (con) num1[i][j]=m;
		  	else
		  	{
		  		num1[i][j]=++m;
		  		con=1;
			}
		  }
	}
	for (j=1;j<=r;j++)
	{
		con=0;
		for (i=1;i<=c;i++)
		  if (!map[i][j])
		    con=0;
		  else
		  {
		  	if (con) num2[i][j]=m;
		  	else
		  	{
		  		num2[i][j]=++m;
		  		con=1;
			}
		  }
	}
	n=0;
	for (i=1;i<=c;i++)
	  for (j=1;j<=r;j++)
	    if (map[i][j])
	    {
	    	x=num1[i][j];
	    	y=num2[i][j];
	    	n++;
	    	next[n]=first[x];
	    	to[n]=y;
	    	first[x]=n;
		}
	ans=0;
	for (i=1;i<=m;i++)
	{
		M(v);
		if (dfs(i)) ans++;
	}
	printf("%d\n",ans);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值