http://acm.hdu.edu.cn/showproblem.php?pid=4185
View Code
1 //4185 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int N=610; 7 char mz[N][N]; 8 const int dx[4]={0,1,0,-1}; 9 const int dy[4]={1,0,-1,0}; 10 int n; 11 int mat[N][N],vis[N][N]; 12 int h(int x,int y) 13 { 14 return (x-1)*n+y-1; 15 } 16 bool find(int u) 17 { 18 int x=u/n+1, y=u%n+1; 19 for(int d=0;d<4;d++) 20 { 21 int nx=x+dx[d], ny=y+dy[d]; 22 if(mz[nx][ny]=='#' && !vis[nx][ny]) 23 { 24 vis[nx][ny]=1; 25 if(mat[nx][ny]==-1 || find(mat[nx][ny])) 26 { 27 mat[nx][ny]=u; 28 return 1; 29 } 30 } 31 } 32 return 0; 33 } 34 int maxmatch() 35 { 36 int cnt=0; 37 memset(mat,-1,sizeof(mat)); 38 for(int i=1;i<=n;i++) 39 for(int j=1;j<=n;j++) if((i+j)%2 && mz[i][j]=='#') 40 { 41 memset(vis,0,sizeof(vis)); 42 if(find(h(i,j))) cnt++; 43 } 44 return cnt; 45 } 46 int main() 47 { 48 int T,C=0; 49 scanf("%d",&T); 50 while(T--) 51 { 52 memset(mz,0,sizeof(mz)); 53 scanf("%d",&n); 54 for(int i=1;i<=n;i++) scanf("%s",mz[i]+1); 55 int ans=maxmatch(); 56 printf("Case %d: %d\n",++C,ans); 57 } 58 return 0; 59 }