UVA 11624 fire! Topic: J Work in the Maze, one day The Maze fire, the fire has many places. Every second, the fire will spread to four directions, J will also move to four directions, asked J can run out, to output him to run out of the shortest time, otherwise output "impossible" problem-solving ideas: first to carry out a BFS, find every point of flame spread to the shortest time of the place. Then, according to the "Fire map", the path of J is BFS. Note J at the Edge, and when there is no ignition.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;Const intN =1005;typedef Long LongllintN, M;intdir[4][2] = {{1,0}, {-1,0}, {0,1}, {0, -1}};structnode{intX, Y, D;BOOL operator< (Constnode& x)Const{returnD > x.d; }}; Node s;intCntintFire[n][n]; queue<Node>Q;priority_queue<node> Q2;Char MapN [N];intBfs_f () { while(! Q.empty ()) {Node p = q.front (); Q.pop ();intpx, py; for(inti =0; I <4; i++) {px = p.x + dir[i][0], py = p.y + dir[i][1];if(PX <0|| PY <0|| PX >= N | | PY >= m)Continue;if(Map[PX] [PY] = =' # ')Continue;Map[PX] [PY] =' F ';if(Fire[px][py] > P.D +1|| Fire[px][py] = =-1) {Fire[px][py] = P.D +1; Q.push (Node) {px, py, P.D +1}); } } } }intBFS () { while(! Q2.empty ()) {Node p = q2.top (); Q2.pop (); for(inti =0; I <4; i++) {intpx = p.x + dir[i][0], py = p.y + dir[i][1];if(PX <0|| PY <0|| PX >= N | | PY >= m)Continue;if(Map[PX] [PY] = =' # '||Map[PX] [PY] = =' J ')Continue;if(Fire[px][py] >= P.D +2) {if(px = =0|| PY = =0|| px = = N-1|| py = = M-1) {returnP.D +1; }Map[PX] [PY] =' J '; Q2.push (Node) {px, py, P.D +1}); } } }return 0;}intMain () {intTscanf("%d", &t); while(t--) {cnt =0;memset(Fire,-1,sizeof(fire)); while(! Q.empty ()) Q.pop (); while(! Q2.empty ()) Q2.pop ();scanf("%d%d\n", &n, &m);intFlag =0, Flag2 =0; for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) {scanf("%c", &MapI [j]);if(MapI [j] = =' J ') {if(i = =0|| j = =0|| i = = N-1|| j = = M-1) {flag =1; } q2.push (Node) {i, J,0}); }Else if(MapI [j] = =' F ') {Flag2 =1; FIRE[I][J] =0; Q.push (Node) {i, J,0}); }} getchar (); }if(!FLAG2)memset(Fire,0x3f,sizeof(fire));if(flag) {printf("1\n");Continue; } bfs_f ();intB = BFS ();if(! B) {printf("impossible\n"); }Else printf("%d\n", B +1); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission can also be reproduced, but to indicate the source oh.
UVA 11624 fire! (Multi-source BFS)