Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12565 | Accepted: 4651 |
Description
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
* 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
Sample Input
4 4
*.*.
.***
***.
..*.
Sample Output
4
Hint
Boards 1, 2, 3 and 4 are placed as follows:
1.2.
.333
444.
..2.
Board 2 overlaps boards 3 and 4.
//F - Muddy Fields POJ - 2226
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
const int N=maxn*maxn/;
int G[N][N],vis[N],link[N];
int n;
bool Find(int u){
for(int i=;i<=n;i++){
if(G[u][i]&&!vis[i]){
vis[i]=;
if(link[i]==-||Find(link[i])){
link[i]=u;
return true;
}
}
}
return false;
}
int solve(){
int cnt=;
memset(link,-,sizeof(link));
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
if(Find(i))cnt++;
}
return cnt;
}
char mat[maxn][maxn];
int vis1[maxn][maxn],vis2[maxn][maxn];
int main(){
int m;
for(int i=;i<=;i++)
mat[][i]=mat[i][]='.';
while(~scanf("%d%d",&n,&m)){
for(int i=;i<=n;i++){
for(int j=;j<=m;j++)
cin>>mat[i][j];
}
int tot1=,tot2=;
memset(vis1,,sizeof(vis1));
memset(vis2,,sizeof(vis2));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mat[i][j]=='*'){
if(!vis1[i][j-])vis1[i][j]=tot1++;
else vis1[i][j]=vis1[i][j-];
if(!vis2[i-][j])vis2[i][j]=tot2++;
else vis2[i][j]=vis2[i-][j];
}
}
}
memset(G,,sizeof(G));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mat[i][j]=='*')
G[vis1[i][j]][vis2[i][j]]=;
}
}
n=max(tot1,tot2);
printf("%d\n",solve());
}
return ;
}