POJ 2226 Muddy Fields
Muddy Fields
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6798 | Accepted: 2506 |
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
题意:R*C的矩阵表示一块田地,'*'表示湿地,'.'表示草地。现在FJ要在田地上铺木板盖掉所有的湿地,露出所有的草地。每块木板的宽度为1,长度为任意长。问FJ最少用几块木板就可以完成任务?
思路:看了半天,实在是没思路,看了看别人的报告才明白。把它转化为一个二分图,求最小点覆盖。
但如何构成二分图呢??
就是把每一行中不相连的一块区域看成一个点,同理每一列也一样。然后就有点像3041那题一样。求它的最小点覆盖
4 4
*.*.
.***
**..
..*.
图g1:
1 0 2 0
0 3 3 3
4 4 0 0
0 0 5 0
图g2:
1 0 4 0
0 3 4 6
2 3 0 0
0 0 5 0
如上数据 答案是5
思路:看了半天,实在是没思路,看了看别人的报告才明白。把它转化为一个二分图,求最小点覆盖。
但如何构成二分图呢??
就是把每一行中不相连的一块区域看成一个点,同理每一列也一样。然后就有点像3041那题一样。求它的最小点覆盖
4 4
*.*.
.***
**..
..*.
图g1:
1 0 2 0
0 3 3 3
4 4 0 0
0 0 5 0
图g2:
1 0 4 0
0 3 4 6
2 3 0 0
0 0 5 0
如上数据 答案是5
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,m,cnt1,cnt2,map[2520][2520],linker[2520],vis[2520]; int g1[60][60],g2[60][60],tg[60][60]; char str[60][60]; int DFS(int u){ int v; for(v=1;v<=cnt2;v++) if(map[u][v] && !vis[v]){ vis[v]=1; if(linker[v]==-1 || DFS(linker[v])){ linker[v]=u; return 1; } } return 0; } int Hungary(){ int u,ans=0; memset(linker,-1,sizeof(linker)); for(u=1;u<=cnt1;u++){ memset(vis,0,sizeof(vis)); if(DFS(u)) ans++; } return ans; } int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){ for(int i=0;i<n;i++) scanf("%s",str[i]); memset(g1,0,sizeof(g1)); memset(g2,0,sizeof(g2)); cnt1=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++){ if(str[i][j]=='*'){ if(j==0) g1[i][j]=++cnt1; else if(str[i][j-1]=='*') g1[i][j]=g1[i][j-1]; else g1[i][j]=++cnt1; } } cnt2=0; for(int j=0;j<m;j++) //注意这里 for(int i=0;i<n;i++){ if(str[i][j]=='*'){ if(i==0) g2[i][j]=++cnt2; else if(str[i-1][j]=='*') g2[i][j]=g2[i-1][j]; else g2[i][j]=++cnt2; } } memset(map,0,sizeof(map)); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(str[i][j]=='*') map[g1[i][j]][g2[i][j]]=1; int ans=Hungary(); printf("%d\n",ans); } return 0; }