#include <cstdio>#include<iostream>#include<queue>#include<string>#include<math.h>#include<stack>#include<cstdlib>#include<map>#include<cstring>#include<vector>#include<algorithm>#include<cctype>#include<sstream>typedefLong Longll;using namespacestd;structnode{intx, y; intT; Node (intXintYintt): X (x), Y (y), T (t) {}};intn,m;Charmaps[1050][1050];BOOLvis[1050][1050];intfiret[1050][1050];intFx,fy,sx,sy;inttx[4] = {-1,0,1,0 };intty[4] = {0,-1,0,1};queue<Node>Fq;queue<Node>JQ;BOOLEscapeintDxintdy) { returndx==0|| dx==n-1|| dy==0|| dy==m-1;}BOOLOkintDxintDyintt) { if(Maps[dx][dy] = ='#')return false; if(Vis[dx][dy])return false; if(Firet[dx][dy]! =0&& Firet[dx][dy] <= t)return false; return true;}voidinput () { while(!fq.empty ()) Fq.pop (); memset (Maps,0,sizeof(maps)); memset (Vis,0,sizeof(VIS)); memset (Firet,0,sizeof(Firet)); scanf ("%d%d",&n,&m); for(inti =0; I < n; i + +) {scanf ("%s", Maps[i]); for(intj =0; J < M; J + +) if(Maps[i][j] = ='F') {Fq.push (Node (I,j,1)); FIRET[I][J]=1; }Else if(Maps[i][j] = ='J') {SX= i, sy =J; } }}voidBfs_fire () { while(!Fq.empty ()) {Node tmp=Fq.front (); Fq.pop (); intx = tmp.x, y =tmp.y; for(inti =0; I <4; i + +){ intxx = x +Tx[i]; intyy = y +Ty[i]; intt = tmp.t+1; if((maps[xx][yy] = ='.'|| MAPS[XX][YY] = ='J') && (xx>=0&& xx<n && yy>=0&& yy<m)&& Firet[xx][yy] = =0){//Fire meets conditionsFIRET[XX][YY] =T; Fq.push (Node (xx,yy,t)); } } } }voidBfs_escape () {Node start (Sx,sy,1); Jq.push (start); while(!Jq.empty ()) {Node tmp=Jq.front (); Jq.pop (); intx = tmp.x, y = tmp.y, tmpt =tmp.t; if(Escape (x, y)) {printf ("%d\n", tmpt);return; } for(inti =0; I <4; i + +){ intxx = x +Tx[i]; intyy = y +Ty[i]; intt = tmp.t +1; if(OK (xx,yy,t)) {Vis[xx][yy]=true; Jq.push (Node (xx,yy,t)); }}} printf ("impossible\n");}intMain () {intT; scanf ("%d",&T); while(t--) {input (); Bfs_fire (); Bfs_escape (); } return 0;}
UVA 11624 don't know why WA