首页 > 代码库 > BNUOJ 2345 Muddy Fields

BNUOJ 2345 Muddy Fields

Muddy Fields

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2226
64-bit integer IO format: %lld      Java class name: Main
 
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 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.
 

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.
 

Source

USACO 2005 January Gold
 
 

 解题:求最小点覆盖,也就是求最大匹配。将每行的连续水块标上号,作为一个顶点集合的,将每一列连续的水块标上号,作为一个顶点集合的,然后每个水格以其所在的行块标号和列块标号为端点,建立边。二分图建立,然后求最大匹配即可。

 

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #define LL long long13 #define INF 0x3f3f3f3f14 using namespace std;15 char mp[60][60];16 int row,col;17 int r[60][60],c[60][60];18 vector<int>g[6000];19 int lik[6000],cnt;20 bool used[6000];21 void init(){22     int i,j;23     char pre = -;24     cnt = 1;25     memset(r,0,sizeof(r));26     memset(c,0,sizeof(c));27     for(i = 0; i < row; i++){28         for(j = 0; j < col; ){29             if(mp[i][j] == *){30                 while(j < col && mp[i][j] == *) {r[i][j] = cnt;j++;}31                 cnt++;32             }else j++;33         }34     }35     for(i = 0; i < col; i++){36         for(j = 0; j < row; ){37             if(mp[j][i] == *){38                 while(j < row && mp[j][i] == *){c[j][i] = cnt;j++;}39                 cnt++;40             }else j++;41         }42     }43 }44 bool dfs(int u){45     for(int i = 0; i < g[u].size(); i++){46         if(!used[g[u][i]]){47             used[g[u][i]] = true;48             if(lik[g[u][i]] == -1 || dfs(lik[g[u][i]])){49                 lik[g[u][i]] = u;50                 return true;51             }52         }53     }54     return false;55 }56 int main(){57     int i,j,ans;58     while(~scanf("%d%d",&row,&col)){59         for(i = 0; i < row; i++)60             scanf("%s",mp[i]);61         for(i = 0; i < 6000; i++){62             g[i].clear();63             lik[i] = -1;64         }65         init();66         for(i = 0; i < row; i++){67             for(j = 0; j < col; j++){68                 if(mp[i][j] == *){69                     g[r[i][j]].push_back(c[i][j]);70                     g[c[i][j]].push_back(r[i][j]);71                 }72             }73         }74         for(ans = 0,i = 1; i < cnt; i++){75             memset(used,false,sizeof(used));76             if(dfs(i)) ans++;77         }78         cout<<(ans>>1)<<endl;79     }80     return 0;81 }
View Code