poj 2226
Muddy Fields
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13386 | Accepted: 4923 |
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.
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.
* 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.
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; const int maxn = 55; char mp[maxn][maxn]; bool vis[maxn*maxn],G[maxn*maxn][maxn*maxn]; int id1[maxn][maxn],id2[maxn][maxn],tt1,tt2,g[maxn*maxn],n,m,tt; bool dfs(int u){ for(int v=1;v<=tt;v++){ if(!G[u][v]) continue; if(!vis[v]){ vis[v]=1; if(!g[v]||dfs(g[v])){ g[v]=u; return 1; } } } return 0; } void debug(){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) printf("%c ",mp[i][j]); puts(""); } } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",mp[i]+1); // debug(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ if(mp[i][j]!='*') continue; if(!id1[i-1][j]) id1[i][j]=++tt1; else id1[i][j]=id1[i-1][j]; if(!id2[i][j-1]) id2[i][j]=++tt2; else id2[i][j]=id2[i][j-1]; } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ if(mp[i][j]=='*') G[id1[i][j]][id2[i][j]]=1; } int ans=0; tt=max(tt1,tt2); for(int i=1;i<=tt;i++){ memset(vis,0,sizeof(vis)); if(dfs(i)) ans++; } printf("%d\n",ans); return 0; }