标签:des style blog http color java os io
4 4 *.*. .*** ***. ..*.
4
解题:求最小点覆盖,也就是求最大匹配。将每行的连续水块标上号,作为一个顶点集合的,将每一列连续的水块标上号,作为一个顶点集合的,然后每个水格以其所在的行块标号和列块标号为端点,建立边。二分图建立,然后求最大匹配即可。
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 long 13 #define INF 0x3f3f3f3f 14 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 }
BNUOJ 2345 Muddy Fields,布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/crackpotisback/p/3888196.html