标签:des style blog io ar color os sp java
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 394 Accepted Submission(s): 178
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 9 #define N 55 10 11 int max(int x,int y){return x>y?x:y;} 12 int min(int x,int y){return x<y?x:y;} 13 int abs(int x,int y){return x<0?-x:x;} 14 15 char map[N][N]; 16 int n, m; 17 bool visited[2505]; 18 int from[2505]; 19 int a[N][N]; //行缩点 20 int b[N][N]; //列缩点 21 int cnt_a, cnt_b; 22 vector<int>ve[N*N]; 23 24 int march(int u){ 25 int i, v; 26 for(i=0;i<ve[u].size();i++){ 27 v=ve[u][i]; 28 if(!visited[v]){ 29 visited[v]=true; 30 if(from[v]==-1||march(from[v])){ 31 from[v]=u; 32 return 1; 33 } 34 } 35 } 36 return 0; 37 } 38 39 main() 40 { 41 int t, i, j, k; 42 cin>>t; 43 while(t--){ 44 scanf("%d %d",&n,&m); 45 memset(map,‘\0‘,sizeof(map)); 46 memset(a,0,sizeof(a)); 47 memset(b,0,sizeof(b)); 48 for(i=1;i<=n;i++) scanf("%s",map[i]+1); 49 cnt_a=1; 50 for(i=1;i<=n;i++){ //对行缩点 51 for(j=1;j<=m;j++){ 52 if(map[i][j]!=‘#‘){ 53 if(map[i][j-1]==‘#‘){ 54 cnt_a++; 55 a[i][j]=cnt_a; 56 } 57 else a[i][j]=cnt_a; 58 } 59 } 60 cnt_a++; 61 } 62 for(i=0;i<=cnt_a;i++) ve[i].clear(); 63 cnt_b=1; 64 for(j=1;j<=m;j++){ //对列缩点 65 for(i=1;i<=n;i++){ 66 if(map[i][j]!=‘#‘){ 67 if(map[i-1][j]==‘#‘){ 68 cnt_b++; 69 b[i][j]=cnt_b; 70 } 71 else b[i][j]=cnt_b; 72 } 73 } 74 cnt_b++; 75 } 76 for(i=1;i<=n;i++){ //缩点后的行列建边 77 for(j=1;j<=m;j++){ 78 if(map[i][j]==‘*‘){ 79 ve[a[i][j]].push_back(b[i][j]); 80 } 81 } 82 } 83 84 memset(from,-1,sizeof(from)); 85 int ans=0; 86 for(i=1;i<=cnt_a;i++){ //匈牙利求最大匹配 87 memset(visited,false,sizeof(visited)); 88 if(march(i)) ans++; 89 } 90 printf("%d\n",ans); 91 } 92 }
标签:des style blog io ar color os sp java
原文地址:http://www.cnblogs.com/qq1012662902/p/4127355.html