poj 2226 Muddy Fields(最小点覆盖+巧妙构图)

 

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 : Two space-separated integers: R and C 

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

Output

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

Sample Input

*.*.
.***
***.
..*.

Sample Output


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

 

题目大意:用木板将'*'覆盖,同一行或同一列的'*'可以用一块木板覆盖,'.'不能被覆盖。问最少用多少块木板可以把全部的'*'覆盖?
木板只能够覆盖连续的横着的泥巴和竖着的泥巴,中间有草地就要隔开

解题思路:二分匹配的经典构图题目
构图思路:
将横着的木板和看成一边的点的集合,将竖着的木板看成另外一边的点的集合,如果他们相交于一点就连边
如果要把所有的泥巴覆盖,又要所需要的木板最少,那么就是求最小点覆盖

所以用匈牙利求最大匹配数即可

构图的代码要好好再看看!

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 560
int n,m; char mp[N][N];
int cnt[N][N];
int cnt1[N][N];
int fina[N][N];
int match[N];
int vis[N];
int tmp,tmp1;
bool dfs(int x){
for(int i=;i<=tmp1;i++){
if(!vis[i] && fina[x][i]) {
vis[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=x;
return true;
}
}
}
return false;
}
void solve(){
memset(match,-,sizeof(match));
int ans=;
for(int i=;i<=tmp;i++){
memset(vis,,sizeof(vis));
if(dfs(i)){
ans++;
}
}
printf("%d\n",ans); }
int main()
{
while(scanf("%d%d",&n,&m)==){
memset(mp,,sizeof(mp));
for(int i=;i<n;i++){
scanf("%s",mp[i]);
}
memset(cnt,-,sizeof(cnt));
memset(cnt1,-,sizeof(cnt1));
tmp=;
for(int i=;i<n;i++){
int flag=;
for(int j=;j<m;j++){ if(flag== && mp[i][j]=='*'){
flag=;
cnt[i][j]=++tmp;
}
else if(flag== && mp[i][j]=='*'){
cnt[i][j]=tmp;
}
else if(mp[i][j]=='.'){
flag=;
}
}
} tmp1=;
for(int j=;j<m;j++){
int flag=;
for(int i=;i<n;i++){
if(flag== && mp[i][j]=='*'){
flag=;
cnt1[i][j]=++tmp1;
}
else if(flag== && mp[i][j]=='*'){
cnt1[i][j]=tmp1;
}
else if(mp[i][j]=='.'){
flag=;
}
}
}
memset(fina,,sizeof(fina));
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(cnt[i][j]!=- && cnt1[i][j]!=-){
fina[cnt[i][j]][cnt1[i][j]]=;
}
}
} solve();
}
return ;
}
上一篇:POJ 3751 JAVA


下一篇:JS request函数 用来获取url参数