A - Muddy Fields POJ - 2226
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.
题意:给出一个田地,然后'*'号代表是泥土地,'.'代表植物,我们有无数块木板,宽为1,长为任意的,然后求用最少的木板数量盖住所有的泥土地,不能盖住植物,木板可以重叠
思路:因为我们盖住泥地的话,肯定是尽量长一点,也就是说我们可以把图中的泥土分为行联通块和列联通块, 因为一个模板肯定是把一个行联通块或者列联通块盖住,所以我们可以
把联通块当成是一个点,然后我们又必须要把所有的点都覆盖住,就是求一个二分图的最小点覆盖,
又有个定理是 最小点覆盖数=二分图最大匹配
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int mp[1001][1001]; int vis[1001]; int girl[1001]; int lid,rid; int dfs(int x) { for(int i=1;i<=rid;i++) { if(mp[x][i]&&vis[i]==0) { vis[i]=1; if(girl[i]==0||dfs(girl[i])) { girl[i]=x; return 1; } } } return 0; } int main() { int n,m; char str[1001][1001]; int col[1001][1001],row[1001][1001]; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%s",str[i]+1); } lid=0,rid=0; memset(col,0,sizeof(col)); memset(row,0,sizeof(row)); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(str[i][j]=='*') { int x=i,y=j; if(col[i][j]==0) { lid++; while(y<=m&&str[i][y]=='*') col[i][y++]=lid;//给所有的行联通块编号 } if(row[i][j]==0) { rid++; while(x<=n&&str[x][j]=='*') row[x++][j]=rid;//给所有的列联通快编号 } } } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(str[i][j]=='*') { mp[col[i][j]][row[i][j]]=1; } } } int sum=0; for(int i=1;i<=lid;i++) { memset(vis,0,sizeof(vis)); sum+=dfs(i); } printf("%d",sum); }