Test instructions
The shortest number of steps from the beginning to the end of a maze.
But in this maze there are many volcanoes, which can spray magma, magma spreads every second, magma to the point can not go, but the movement of people over the magma.
It means that if a certain moment, magma and people arrive at the same time, then if this point is the exit, this point can go.
Ideas:
First BFS pretreatment all the ignition mountain spread to the minimum time, is to put all the volcano into the team to do BFS.
Then employ the person to do the BFS, pay attention to consider the above man's mobile priority over the magma.
Code:
#include "cstdlib" #include "Cstdio" #include "CString" #include "Cmath" #include "queue" #include "algorithm" #include " iostream "#include" map "using namespace std; #define EPS 1e-13#define ll __int64int time[1234][1234];int used[1234][1234] ; int N,m;char Mp[1234][1234];int dis[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};struct node{int x,y,t;}; void Bfs1 () {for (Int. i=0; i<n; i++) for (int j=0; j<m; j + +) Time[i][j]=-1; Node Cur,next; queue<node>q; for (int i=0, i<n; i++) {for (int j=0; j<m; J + +) {if (mp[i][j]== '! ') {cur.x=i; Cur.y=j; cur.t=0; time[i][j]=0; Q.push (cur); }}} while (!q.empty ()) {Cur=q.front (); Q.pop (); for (int i=0; i<4; i++) {next.x=cur.x+dis[i][0]; NEXT.Y=CUR.Y+DIS[I][1]; next.t=cur.t+1; if (next.x<0 | | next.y<0 | | next.x>=n | | next.y>=m|| mp[next.x][next.y]== ' # ' | | TIME[NEXT.X][NEXT.Y]!=-1) continue; time[next.x][next.y]=next.t; Q.push (next); }} return; int BFS2 () {for (Int. i=0; i<n; i++) for (int j=0; j<m; j + +) used[i][j]=0; Node Cur,next; queue<node>q; for (int i=0, i<n; i++) {for (int j=0; j<m; J + +) {if (mp[i][j]== ' S ') { Cur.x=i; Cur.y=j; cur.t=0; Used[i][j]=1; Q.push (cur); }}} while (!q.empty ()) {Cur=q.front (); Q.pop (); for (int i=0; i<4; i++) {next.x=cur.x+dis[i][0]; NEXT.Y=CUR.Y+DIS[I][1]; next.t=cur.t+1; if (next.x<0 | | next.y<0 | | next.x>=n | | next.y>=m | | mp[next.x][next.y]== ' # ' | | used[next.x][next.y] | | time[n EXT.X][NEXT.Y]<NEXT.T) continue; Used[next.x][next.y]=1; if (mp[next.x][next.y]== ' E ') rEturn 1; else if (Next.t!=time[next.x][next.y]) Q.push (next); }} return 0;} int main () {int t; cin>>t; while (t--) {scanf ("%d%d", &n,&m); for (int i=0; i<n; i++) scanf ("%s", Mp[i]); BFS1 (); int ANS=BFS2 (); Puts (ans? ") Yes ":" No "); } return 0;}
[BFS] Fzu OJ 2196 Escape