Muddy Fields
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12683 | Accepted: 4696 |
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 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 //#include<queue> 7 //#include<set> 8 #include<bitset> 9 //#include<cassert> 10 #include<vector> 11 #define INF 0x3f3f3f3f 12 #define re register 13 #define Ii inline int 14 #define Il inline long long 15 #define Iv inline void 16 #define Ib inline bool 17 #define Id inline double 18 #define ll long long 19 #define Fill(a,b) memset(a,b,sizeof(a)) 20 #define R(a,b,c) for(register int a=b;a<=c;++a) 21 #define nR(a,b,c) for(register int a=b;a>=c;--a) 22 #define Min(a,b) ((a)<(b)?(a):(b)) 23 #define Max(a,b) ((a)>(b)?(a):(b)) 24 #define Cmin(a,b) ((a)=(a)<(b)?(a):(b)) 25 #define Cmax(a,b) ((a)=(a)>(b)?(a):(b)) 26 #define abs(a) ((a)>0?(a):-(a)) 27 #define D_e(x) printf("&__ %d __& ",x) 28 #define D_e_Line printf("----------------- ") 29 #define Pause system("pause"); 30 using namespace std; 31 const int N=2505; 32 Ii read(){ 33 int s=0,f=1;char c; 34 for(c=getchar();c>'9'||c<'0';c=getchar())if(c=='-')f=-1; 35 while(c>='0'&&c<='9')s=s*10+(c^'0'),c=getchar(); 36 return s*f; 37 } 38 Iv print(int x){ 39 if(x<0)putchar('-'),x=-x; 40 if(x>9)print(x/10); 41 putchar(x%10^'0'); 42 } 43 using namespace std; 44 int vis[N],match[N],tim; 45 vector<int>V[N]; 46 Ib Hungary(int u){ 47 for(vector<int>::iterator v=V[u].begin();v!=V[u].end();++v) 48 if(vis[*v]!=tim){ 49 vis[*v]=tim; 50 if(!match[*v]||Hungary(match[*v])){ 51 match[*v]=u;return 1; 52 } 53 } 54 return 0; 55 } 56 char mp[55][55]; 57 int main(){ 58 int n,m; 59 while(~scanf("%d%d",&n,&m)){ 60 R(i,0,n) 61 vector<int>().swap(V[i]),match[i]=0,vis[i]=0; 62 char *s=new char[10]; 63 R(i,1,n){ 64 scanf("%s",s+1); 65 R(j,1,m) 66 mp[i][j]=s[j]; 67 } 68 delete []s; 69 int **a=new int *[n+1]; 70 int **b=new int *[n+1]; 71 R(i,0,n+1){ 72 a[i]=new int[m+1]; 73 b[i]=new int[m+1]; 74 } 75 Fill(a,0),Fill(b,0); 76 int x=0,y=0; 77 R(i,1,n) 78 R(j,1,m) 79 if(mp[i][j]=='*') 80 a[i][j]=(mp[i][j-1]=='*')?a[i][j-1]:++x; 81 R(i,1,n) 82 R(j,1,m) 83 if(mp[i][j]=='*'){ 84 b[i][j]=(mp[i-1][j]=='*')?b[i-1][j]:++y; 85 V[a[i][j]].push_back(b[i][j]); 86 } 87 R(i,0,n+1){ 88 delete []a[i]; 89 delete []b[i]; 90 } 91 delete []a; 92 delete []b; 93 int ans=0; 94 tim=0; 95 R(i,1,x) 96 ++tim, 97 ans+=Hungary(i); 98 print(ans),putchar(' '); 99 } 100 return 0; 101 }