Topic Connection: http://acm.hust.edu.cn/vjudge/problem/28833
/*first to the entire picture BFS once get flame burning timetable after the BFS search path with a fire table judgment pit point is: If the timetable equals 0 should be never burned ... If you don't differentiate it, WA*/#include<bits/stdc++.h>#defineScan (x) scanf ("%d", &x)#defineM (x) memset (x,0,sizeof (x))#defineREF (i,n) for (int i=1;i<=n;i++)using namespacestd;Const intmax=1e3+Ten;CharMat[max][max];intBook[max][max];intVis[max][max];intn,m,stx,sty;structnode{intX,y,step; Node () {x=y=step=0;} Node (intXxintYyintss): X (xx), Y (yy), step (ss) {}};intnex[4][2]={0,1,1,0,0,-1,-1,0};queue<node>que1;voidbfs1 () {M (book); M (VIS); Node u,v; while(!Que1.empty ()) {u=Que1.front (); Que1.pop (); for(intk=0;k<4; k++) {v.x=u.x+nex[k][0]; V.y=u.y+nex[k][1]; V.step=u.step+1; if(v.x<1|| v.y<1|| v.x>n| | V.Y>M)Continue; if(mat[v.x][v.y]=='#')Continue; if(Book[v.x][v.y])Continue; BOOK[V.X][V.Y]=V.step; Que1.push (v); } }}intbfs2 () {M (VIS); Queue<node>que; Node u,v; Que.push (Node (stx,sty,0)); Vis[stx][sty]=1; while(!Que.empty ()) {u=Que.front (); Que.pop (); for(intk=0;k<4; k++) {v.x=u.x+nex[k][0]; V.y=u.y+nex[k][1]; V.step=u.step+1; if(v.x<1|| v.y<1|| v.x>n| | V.Y>M)returnV.step; if(book[v.x][v.y]!=0&&book[v.x][v.y]<=v.step)Continue;//book[i][j]!=0 if(mat[v.x][v.y]=='#'|| mat[v.x][v.y]=='F')Continue;//You can't rule out a point that never catches fire. if(Vis[v.x][v.y])Continue; VIS[V.X][V.Y]=1; Que.push (v); } } return-1;}intMain () {intT; for(Scan (T); T t--) {cin>>n>>M;getchar (); while(!que1.empty ()) Que1.pop (); Ref (I,n) {ref (J,M) {scanf ("%c",&Mat[i][j]); if(mat[i][j]=='J') stx=i,sty=J; if(mat[i][j]=='F') Que1.push (node (i,j,0)); } getchar (); } BFS1 (); intans=BFS2 (); if(ans!=-1) cout<<ans<<Endl; Elsecout<<"Impossible"<<Endl; } return 0;}
Fire UVA 11624