标签:des style http color os io for ar art
3 5 6 XXXXXX XZ..ZX XXXXXX M.G... ...... 5 6 XXXXXX XZZ..X XXXXXX M..... ..G... 10 10 .......... ..X....... ..M.X...X. X......... .X..X.X.X. .........X ..XX....X. X....G...X ...ZX.X... ...Z..X..X
1 1 -1
#include <stdio.h> #include <string.h> struct{ int x,y,step; }que1[1000000],que2[1000000],que[1000000],t; int n,m,gxa,gya,gxb,gyb,xa,ya,xb,yb,nxt[4][2]={{0,1},{1,0},{0,-1},{-1,0}},top,bottom,gt; char mp[800][805]; bool vis1[800][800],vis2[800][800],vis[800][800]; void divide()//鬼分身,让有鬼的地方都变成墙 { int i; while(top<bottom) { t=que[top]; if(t.step>=gt) return; t.step++; for(i=0;i<4;i++) { t.x+=nxt[i][0]; t.y+=nxt[i][1]; if(t.x>=0 && t.x<n && t.y>=0 && t.y<m && !vis[t.x][t.y]) { vis[t.x][t.y]=1; mp[t.x][t.y]='X'; que[bottom++]=t; } t.x-=nxt[i][0]; t.y-=nxt[i][1]; } top++; } } int bfs() { int i,t1,t2,top1,top2,bottom1,bottom2; memset(vis,0,sizeof vis); memset(vis1,0,sizeof vis1); memset(vis2,0,sizeof vis2); top=0; bottom=2; que[0].x=gxa; que[0].y=gya; que[0].step=0; que[1].x=gxb; que[1].y=gyb; que[1].step=0; gt=2; t1=1; t2=3; top1=0; bottom1=1; que1[0].x=xa; que1[0].y=ya; que1[0].step=0; vis1[xa][ya]=1; top2=0; bottom2=1; que2[0].x=xb; que2[0].y=yb; que2[0].step=0; vis2[xb][yb]=1; while(1) { divide();//鬼先分身 bool flag=0; while(top1<bottom1)//女孩走 { t=que1[top1]; if(t.step>=t1) break; t.step++; for(i=0;i<4;i++) { t.x+=nxt[i][0]; t.y+=nxt[i][1]; if(t.x>=0 && t.x<n && t.y>=0 && t.y<m && mp[t.x-nxt[i][0]][t.y-nxt[i][1]]!='X' && mp[t.x][t.y]!='X' && !vis1[t.x][t.y]) { flag=1; if(vis2[t.x][t.y]) return t1; vis1[t.x][t.y]=1; que1[bottom1++]=t; } t.x-=nxt[i][0]; t.y-=nxt[i][1]; } top1++; } while(top2<bottom2)//男孩走 { t=que2[top2]; if(t.step>=t2) break; t.step++; for(i=0;i<4;i++) { t.x+=nxt[i][0]; t.y+=nxt[i][1]; if(t.x>=0 && t.x<n && t.y>=0 && t.y<m && mp[t.x-nxt[i][0]][t.y-nxt[i][1]]!='X' && mp[t.x][t.y]!='X' && !vis2[t.x][t.y]) { flag=1; if(vis1[t.x][t.y]) return t1; vis2[t.x][t.y]=1; que2[bottom2++]=t; } t.x-=nxt[i][0]; t.y-=nxt[i][1]; } top2++; } if(!flag) return -1; gt+=2; t1++; t2+=3; } } int main() { int T,i,j,cnt; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(i=0;i<n;i++) scanf("%s",mp[i]); cnt=0; for(i=0;i<n;i++) for(j=0;j<m;j++) { if(mp[i][j]=='G') xa=i,ya=j; else if(mp[i][j]=='M') xb=i,yb=j; else if(mp[i][j]=='Z') { mp[i][j]='X'; if(!cnt) gxa=i,gya=j; else gxb=i,gyb=j; cnt++; } } printf("%d\n",bfs()); } }
标签:des style http color os io for ar art
原文地址:http://blog.csdn.net/faithdmc/article/details/38876439