According to the White book, first use BFS, to find out the time of each point fire
Once again BFS find out if you can get out of the maze
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <queue>6#include <vector>7 using namespacestd;8 9 Const intMAXN =1005;Ten Const intINF =1<< --1; One intR,c;//r row, column C A intD[MAXN][MAXN]; - intVIS[MAXN][MAXN]; - CharG[MAXN][MAXN]; the intSx,sy; - - intdir[4][2] = {1,0,-1,0,0,1,0,-1}; - + structnode{ - intx, y; + intStep; A }P[MAXN]; at -Queue<node>Q; - - voidbfs1 () { -memset (Vis,0,sizeof(Vis)); - while(!Q.empty ()) { inNode V =Q.front (); Q.pop (); - for(inti =0; I <4; i++){ to intxx = v.x + dir[i][0]; + intyy = V.y + dir[i][1]; - if(XX <1|| xx > R | | yy <1|| yy > C | | VIS[XX][YY] | | G[XX][YY]! ='.')Continue; theVIS[XX][YY] =1; *D[xx][yy] = min (d[xx][yy],v.step+1); $Q.push (node{xx,yy,v.step+1});Panax Notoginseng } - } the } + A voidbfs2 () { theQueue<node>Q; +memset (Vis,0,sizeof(Vis)); -Q.push (Node{sx,sy,0}); Vis[sx][sy] =1; $ $ while(!Q.empty ()) { -Node U =Q.front (); Q.pop (); - if(u.x = = R | | u.y = = c | | u.x = =1|| U.y = =1){ theprintf"%d\n", U.step +1); - return;Wuyi } the for(inti =0; I <4; i++){ - intxx = u.x + dir[i][0]; Wu intyy = U.y + dir[i][1]; - if(XX <1|| xx > R | | yy <1|| yy > C | | VIS[XX][YY] | | G[XX][YY]! ='.')Continue; About if(U.step +1>= D[xx][yy])Continue; $VIS[XX][YY] =1; -Q.push (node{xx,yy,u.step+1}); - } - } APuts"Impossible"); + } the - intMain () { $ intT; thescanf"%d",&T); the while(t--){ the while(!Q.empty ()) Q.pop (); thescanf"%d%d",&r,&c); - for(inti =1; I <= r;i++){ in for(intj =1; J <= c;j++) d[i][j] =INF; the } the for(inti =1; I <= r;i++){ About for(intj =1; J <= c;j++) { theCin>>G[i][j]; the if(G[i][j] = ='F') Q.push (Node{i,j,0}); the if(G[i][j] = ='J') SX = I,sy =J; + } - } the BFS1 ();Bayi BFS2 (); the } the return 0; -}
View Code
Come on ~~~gooooooo~~.
UVA 11624 fire! "BFS"