标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8881 | Accepted: 3300 |
Description
Input
Output
Sample Input
4 4 *.*. .*** ***. ..*.
Sample Output
4
Hint
Source
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 11 #define N 50 12 13 int max(int x,int y){return x>y?x:y;} 14 int min(int x,int y){return x<y?x:y;} 15 int abs(int x,int y){return x<0?-x:x;} 16 17 int n, m; 18 vector<int>ve[N*N]; 19 int from[N*N]; 20 bool visited[N*N]; 21 char map[N][N]; 22 int map1[N][N]; 23 int map2[N][N]; 24 25 int march(int u){ 26 int i, v; 27 for(i=0;i<ve[u].size();i++){ 28 v=ve[u][i]; 29 if(!visited[v]){ 30 visited[v]=true; 31 if(from[v]==-1||march(from[v])){ 32 from[v]=u; 33 return 1; 34 } 35 } 36 } 37 return 0; 38 } 39 40 main() 41 { 42 int i, j, k; 43 while(scanf("%d %d",&n,&m)==2){ 44 for(i=0;i<n;i++) scanf("%s",map[i]); 45 memset(map1,-1,sizeof(map1)); 46 memset(map2,-1,sizeof(map2)); 47 int num=1; 48 int maxh=0; 49 //横着处理 50 for(i=0;i<n;i++){ 51 for(j=0;j<m;j++){ 52 if(map[i][j]==‘*‘){ 53 if(j==0) map1[i][j]=num; 54 else{ 55 if(map[i][j-1]==‘*‘) map1[i][j]=map1[i][j-1]; 56 else map1[i][j]=num; 57 } 58 } 59 else { 60 if(j<m-1&&map[i][j+1]==‘*‘) num++; 61 } 62 } 63 num++; 64 maxh=max(maxh,num); 65 } 66 //竖着处理 67 num=1; 68 for(j=0;j<m;j++){ 69 for(i=0;i<n;i++){ 70 if(map[i][j]==‘*‘){ 71 if(i==0) map2[i][j]=num; 72 else{ 73 if(map[i-1][j]==‘*‘) map2[i][j]=map2[i-1][j]; 74 else map2[i][j]=num; 75 } 76 } 77 else{ 78 if(i<n-1&&map[i+1][j]==‘*‘) num++; 79 } 80 } 81 num++; 82 maxh=max(maxh,num); 83 } 84 //建二分图 85 for(i=0;i<maxh;i++) ve[i].clear(); 86 for(i=0;i<n;i++){ 87 for(j=0;j<m;j++){ 88 if(map1[i][j]!=-1&&map2[i][j]!=-1){ 89 ve[map1[i][j]].push_back(map2[i][j]); 90 } 91 } 92 } 93 //二分匹配 94 memset(from,-1,sizeof(from)); 95 num=0; 96 for(i=0;i<maxh;i++){ 97 memset(visited,false,sizeof(visited)); 98 if(march(i)) num++; 99 } 100 printf("%d\n",num); 101 } 102 }
标签:
原文地址:http://www.cnblogs.com/qq1012662902/p/4643821.html