Muddy Fields(POJ 2226)

Muddy Fields

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.

题目大意:在一块 n×m 的地面上,有一些格子是泥泞的,有一些格子是干净的。现在需要用一些宽度为 1 、长度任意的木板把泥地盖住,同时不能盖住干净的地面,木板可以重叠。求最少需要多少块木板;

这种题目要是硬做,可能会想到搜索,很难想到图论;更别说二分图匹配了;

个人认为图论的哪些算法最难的还是建模,怎么把看似一个毫无关系的题目运用到图论的算法上面,这才是拉开差距的一个地方吧;

言归正传,我们可以把一个泥块当做一条边,这条边的两个端点就是这个泥块对应的行泥泞块列泥泞块,然后套用二分图的最小点覆盖模型,把行泥泞块作为左部节点,列泥泞块作为右部节点。求出二分图最大匹配就行;

这道题还有个难度就是建边,我们可以先求出行泥泞块,标号为从1开始(拿样例来说):

1 0 2 0
0 3 3 3
4 4 4 0
0 0 5 0

然后在求列泥泞块:

1 0 4 0
0 3 4 5
2 3 4 0
0 0 4 0

然后最后在枚举每个 * (泥泞块),行和列建边就行;

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<math.h>
#include<queue>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=100100;
const int M=200100;
const LL mod=998244353;
char ma[55][55];
int mr[55][55],mc[55][55]; 
int n1,n2;
struct Node{
	int to,nex;
}edge[N];
int head[1100];
int cnt;
void add(int p,int q){
	edge[cnt].to=q;
	edge[cnt].nex=head[p];
	head[p]=cnt++;
}
bool v[1100];//标记右半部分是否匹配 
int fa[1100];//记录右半部分所匹配的左半部分是什么 
bool find(int p){
	for(int i=head[p];~i;i=edge[i].nex){
		int q=edge[i].to;
		if(!v[q]){
			v[q]=true;
			if(fa[q]==0||find(fa[q])){//跟并查集有点相似 
				fa[q]=p;
				return true;
			}
		}
	}
	return false;
}
int main(){
	ios::sync_with_stdio(false);
	int r,c;
	while(cin>>r>>c){
		cnt=0;
		memset(head,-1,sizeof(head));
		for(int i=1;i<=r;i++){
			for(int j=1;j<=c;j++) cin>>ma[i][j];
		}
		int ans=0;
		for(int i=1;i<=r;i++){
			for(int j=1;j<=c;j++){
				if(ma[i][j]=='*'&&ma[i][j-1]=='*') mr[i][j]=ans;
				else if(ma[i][j]=='*'&&ma[i][j-1]!='*') mr[i][j]=++ans;
			}
		}
		n1=ans;//左部的点数 
		ans=0;
		for(int i=1;i<=c;i++){
			for(int j=1;j<=r;j++){
				if(ma[j][i]=='*'&&ma[j-1][i]=='*') mc[j][i]=ans;
				else if(ma[j][i]=='*'&&ma[j-1][i]!='*') mc[j][i]=++ans;
			}
		}
		n2=ans;//右部的点数 
		for(int i=1;i<=r;i++){
			for(int j=1;j<=c;j++){
				if(ma[i][j]=='*') add(mr[i][j],mc[i][j]);//建边 
			}
		}
		int s=0;
		for(int i=1;i<=n1;i++){
			memset(v,false,sizeof(v));
			if(find(i)) s++; 
		}
		cout<<s<<endl;
	} 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值