Two times wide Search
#include <cstdio>#include<cstring>#include<cmath>#include<queue>#include<algorithm>using namespacestd;Const intmaxn=305;intN,m,sx,sy;intMAP[MAXN][MAXN];intY[MAXN][MAXN];intE[MAXN][MAXN];intFLAG[MAXN][MAXN];intFLAG[MAXN][MAXN];intF[MAXN][MAXN];intdir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};intans,sum;structpoint{intx, y; Point (intAintb) {x=a;y=b;}};voidinit () {memset (flag,0,sizeofFlag);//Mark has not traversedmemset (F,0,sizeofF);//whether the tag is a boundarymemset (Map,0,sizeofMap); memset (Flag,0,sizeofFlag); Memset (E,0,sizeofe); memset (Y,0,sizeofY); Ans=-1, sum=0;}voidRead () { for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) {scanf ("%d",&Map[i][j]); Y[I][J]=Map[i][j]; }}voidBFS1 () {Queue<Point>p; Point P (Sx,sy); Flag[sx][sy]=1; Q.push (P); while(!Q.empty ()) {Point P=Q.front (); Q.pop (); if(map[p.x][p.y]!=0&&map[p.x][p.y]!=-1) {F[P.X][P.Y]=1;//Boundary Mark 1 Continue; } for(intI=0;i<4; i++) { intnewx=p.x+dir[i][0]; intnewy=p.y+dir[i][1]; if(!Flag[newx][newy])if(newx>=1&&newx<=N)if(newy>=1&&newy<=M) {FLAG[P.X][P.Y]=1; Point P (newx,newy); Q.push (P); } } } /*for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) printf ("%d", f[i][j]); printf ("\ n"); } */}voidBFS2 () {Queue<Point>p; Point P (0,0); flag[0][0]=1; Q.push (P); while(!Q.empty ()) {Point P=Q.front (); Q.pop (); //printf ("%d%d%d\n", p.x,p.y,map[p.x][p.y]); if(Map[p.x][p.y]) {if(f[p.x][p.y]) {sum=sum+map[p.x][p.y]-1; Continue; } Elsesum=sum+MAP[P.X][P.Y]; } for(intI=0;i<4; i++) { intnewx=p.x+dir[i][0]; intnewy=p.y+dir[i][1]; if(flag[newx][newy]==0) if(newx>=0&&newx<=n+1) if(newy>=0&&newy<=m+1) {Flag[newx][newy]=1; Point P (newx,newy); Q.push (P); } } }}voidDfsintXinty) { if(x==0|| x==n+1|| y==0|| y==m+1) {ans=1;return;} E[x][y]=1; for(intI=0;i<4; i++) { intnewx=x+dir[i][0]; intnewy=y+dir[i][1]; if(y[newx][newy]==0) if(e[newx][newy]==0) DFS (newx,newy); }}voidsolve () { for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) if(map[i][j]==-1) Sx=i,sy=J; E[sx][sy]=1;d FS (Sx,sy); if(ans==1) {printf ("Ali win\n");return;} BFS1 (); BFS2 (); //printf ("%d\n", sum); if(sum%2==1) printf ("Ali win\n"); Elseprintf"Baba win\n");}intMain () { while(~SCANF ("%d%d",&n,&M)) {init (); Read (); Solve (); } return 0;}
HDU 4101 Ali and Baba