BZOJ1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场
1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 310 Solved: 183
[Submit][Status][Discuss]
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.
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.
第1行:两个整数R和C.
第2到R+1行:每行C个字符,其中“*’代表泥地,“.”代表草地.
Output
* Line 1: A single integer representing the number of boards FJ needs.
最少需要多少木板.
Sample Input
*.*.
.***
***.
..*.
Sample Output
HINT
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm> 6 7 inline void read(int &x) 8 { 9 x = 0;char ch = getchar(), c = ch; 10 while(ch < '0' || ch > '9')c = ch, ch - getchar(); 11 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar(); 12 if(c == '-')x = -x; 13 } 14 15 const int MAXN = 1000 + 10; 16 17 char gg[MAXN][MAXN]; 18 19 int lk[MAXN], b[MAXN], n, m, g[MAXN][MAXN], row[MAXN][MAXN], line[MAXN][MAXN], r, c; 20 21 int dfs(int u) 22 { 23 for(register int v = 1;v <= m;++ v) 24 { 25 if(g[u][v] && !b[v]) 26 { 27 b[v] = 1; 28 if(lk[v] == -1 || dfs(lk[v])) 29 { 30 lk[v] = u; 31 return 1; 32 } 33 } 34 } 35 return 0; 36 } 37 38 int xiongyali() 39 { 40 int ans = 0; 41 memset(lk, -1, sizeof(lk)); 42 for(register int i = 1;i <= n;++ i) 43 { 44 memset(b, 0, sizeof(b)); 45 ans += dfs(i); 46 } 47 return ans; 48 } 49 50 int main() 51 { 52 read(r), read(c); 53 for(register int i = 1;i <= r;++ i) 54 scanf("%s", gg[i] + 1); 55 register int tot = 0; 56 for(register int i = 1;i <= r;++ i) 57 for(register int j = 1;j <= c;++ j) 58 if(gg[i][j] == '*') 59 { 60 ++ tot; 61 while(gg[i][j] == '*' && j <= c)row[i][j] = tot, ++ j; 62 } 63 n = tot; 64 tot = 0; 65 for(register int j = 1;j <= c;++ j) 66 for(register int i = 1;i <= r;++ i) 67 if(gg[i][j] == '*') 68 { 69 ++ tot; 70 while(gg[i][j] == '*' && i <= r)line[i][j] = tot, ++ i; 71 } 72 m = tot; 73 for(register int i = 1;i <= r;++ i) 74 for(register int j = 1 ;j <= c;++ j) 75 if(gg[i][j] == '*') 76 g[row[i][j]][line[i][j]] = 1; 77 printf("%d", xiongyali()); 78 return 0; 79 }