POJ2226 Muddy Fields

Muddy Fields

Muddy Fields
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 12948Accepted: 4787

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.

Source

在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水,木板可以重叠,但是所有的草地都不能被木板覆盖。问至少需要的木板数。

题解

显然木板应该贪心的横着或竖着放到最大长度。把横的模板和竖的模板看成节点。

2要素:对于每一滩水,横着的和竖着的木板至少选一个。

于是这是一个二分图,跑最小点覆盖。

#include<iostream>
#include<vector>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
    for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;

co int N=56;
int n,m,tot=1,a[N][N][2],f[N*N],ans;
char s[N][N];
bool v[N*N];
vector<int> e[N*N];

bool dfs(int x){
	for(unsigned i=0;i<e[x].size();++i){
		int y=e[x][i];
		if(v[y]) continue;
		v[y]=1;
		if(!f[y]||dfs(f[y])){
			f[y]=x;
			return 1;
		}
	}
	return 0;
}
int main(){
	read(n),read(m);
	for(int i=1;i<=n;++i) scanf("%s",s[i]+1);
	for(int i=1;i<=n;++i)for(int j=1;j<=m+1;++j){
		if(s[i][j]=='*') a[i][j][0]=tot;
		else ++tot;
	}
	int t=tot;
	for(int j=1;j<=m;++j)for(int i=1;i<=n+1;++i){
		if(s[i][j]=='*') a[i][j][1]=tot;
		else ++tot;
	}
	for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)
		if(s[i][j]=='*') e[a[i][j][0]].push_back(a[i][j][1]);
	for(int i=1;i<t;++i){
		memset(v,0,sizeof v);
		ans+=dfs(i);
	}
	printf("%d
",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/autoint/p/10972410.html