Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10149 | Accepted: 3783 |
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
将连续的一段横/纵条算作同一个,若横纵条相交则连边,跑二分图匹配。
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<vector> 8 using namespace std; 9 const int mxn=1010; 10 int m,n; 11 char a[mxn][mxn]; 12 // 13 int idx,idy; 14 int hs[mxn][mxn]; 15 int link[mxn]; 16 int mp[mxn][mxn]; 17 bool vis[mxn]; 18 // 19 void init(){ 20 int i,j; 21 for(i=1;i<=n;i++){ 22 for(j=1;j<=m;j++){ 23 if(a[i][j]=='*'){ 24 ++idx; 25 while(a[i][j]=='*')hs[i][j++]=idx; 26 } 27 } 28 } 29 idy=idx; 30 for(j=1;j<=m;j++) 31 for(i=1;i<=n;i++){ 32 if(a[i][j]=='*'){ 33 ++idy; 34 while(a[i][j]=='*'){ 35 mp[idy][hs[i][j]]=1; 36 mp[hs[i++][j]][idy]=1; 37 } 38 } 39 } 40 return; 41 } 42 bool DFS(int u){ 43 for(int i=1;i<=idy;i++) 44 if(!vis[i] && mp[u][i]){ 45 vis[i]=1; 46 if(link[i]==-1 || DFS(link[i])){ 47 link[i]=u; 48 return 1; 49 } 50 } 51 return 0; 52 } 53 int ans=0; 54 void solve(){ 55 memset(link,-1,sizeof link); 56 for(int i=1;i<=idx;i++){ 57 memset(vis,0,sizeof vis); 58 if(DFS(i))ans++; 59 } 60 return; 61 } 62 int main(){ 63 scanf("%d%d",&n,&m); 64 int i,j; 65 for(i=1;i<=n;i++) 66 scanf("%s",a[i]+1); 67 init(); 68 solve(); 69 printf("%d ",ans); 70 return 0; 71 }