POJ 2226 Muddy Fields

Language:
Muddy Fields
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9699 Accepted: 3597

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.

Sample Input

4 4
*.*.
.***
***.
..*.

Sample Output

4

Hint

OUTPUT DETAILS: 

Boards 1, 2, 3 and 4 are placed as follows: 
1.2. 
.333 
444. 
..2. 
Board 2 overlaps boards 3 and 4.

【题目分析】

    怎么说呢,确实是一道好题,一个泥地,是属于两个数值(行数&列数)自然而然的想到了行列的二分图匹配,然后复制代码,改了改,顺利的拿到了一个WA。后来又想到,木板不能盖在草地上,然后就开始了漫长的求行列连通分量。每一个连通分量看做一个点,然后将行和列的两个连通分量相连,就可以过了。(小小的嘚瑟一下,这个求连通分量的写法还是非常优美的)


【代码】

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int map1[51][51],map2[51][51],map[51][51],line[901][901],vis[901],match[901];
int n,m,t1,t2;
bool find(int x)
{
	for (int j=1;j<=t2;++j){
		if (line[x][j]&&vis[j]==0){
			vis[j]=1;
			if (match[j]==0||find(match[j]))
			{
				match[j]=x;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;++i)
		for (int j=1;j<=m;++j){
			char ch;
			cin>>ch;
			map[i][j]=(ch=='*');
		}
	memcpy(map1,map,sizeof map);
	memcpy(map2,map,sizeof map);
	for (int i=1;i<=n;++i){
		int flag=0;
		for (int j=1;j<=m;j++){
			if (map1[i][j]&&!flag){
				flag=!flag;
				t1++;
			}
			if (map1[i][j]) map1[i][j]=t1;
			if (!map1[i][j]&&flag) flag=!flag;
		}
	}
	for (int i=1;i<=m;++i){
		int flag=0;
		for (int j=1;j<=n;j++){
			if (map2[j][i]&&!flag){
				flag=!flag;
				t2++;
			}
			if (map2[j][i]) map2[j][i]=t2;
			if (!map2[j][i]&&flag) flag=!flag;
		}
	}
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;++j)
			if (map[i][j]) line[map1[i][j]][map2[i][j]]=1;
	int ans=0;
	for (int i=1;i<=t1;i++){
		memset(vis,0,sizeof vis);
		if (find(i)) ans++;
	}
	cout<<ans<<endl;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值