标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 993 Accepted Submission(s): 422
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cstdlib> #include<cmath> using namespace std; int tt,n,ans,g[610][610],nx,ny,mark[610],link[610]; char s[610][610]; struct node { int x,y; }a0[610],a1[610]; bool dfs(int t) { for(int i=0;i<ny;i++) { if(mark[i]==-1&&g[t][i]) { mark[i]=1; if(link[i]==-1||dfs(link[i])) { link[i]=t; return true; } } } return false; } int main() { int cas=1; scanf("%d",&tt); while(tt--) { scanf("%d",&n); ans=0,nx=0,ny=0; memset(g,0,sizeof(g)); getchar(); for(int i=0;i<n;i++) scanf("%s",s[i]); for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(s[i][j]==‘#‘) { if((i+j)%2==0) { a0[nx].x=i,a0[nx].y=j; nx++; } else { a1[ny].x=i,a1[ny].y=j; ny++; } } } memset(link,-1,sizeof(link)); for(int i=0;i<nx;i++) for(int j=0;j<ny;j++) { if(((a0[i].x==a1[j].x)&&abs(a0[i].y-a1[j].y)==1)||((a0[i].y==a1[j].y)&&abs(a0[i].x-a1[j].x)==1)) g[i][j]=1; } for(int i=0;i<nx;i++) { memset(mark,-1,sizeof(mark)); if(dfs(i)) ans++; } printf("Case %d: %d\n",cas,ans); cas++; } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4248705.html