Ultraviolet A-11624-Fire! (BFS Application)
A-Fire!
Time Limit:1000 MS
Memory Limit:0 KB
64bit IO Format:% Lld & % lluSubmitStatus
Description
Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or horizontally (notdiagonally ). the fire spreads all four directions from each square that is onfire. joe may exit the maze from any square that borders the edge of the maze. neither Joe nor the fire may enter a square that is occupied by a wall.
Input SpecificationThe first line of input contains a single integer, the number of test cases to follow. the first line of each test case contains the two integersR and C, separatedby spaces, with 1 <= R, C <= 1000. the followingR lines of the test caseeach contain one row of the maze. each of these lines contains exactlyC characters, and each of these characters is one:
#, A wall ., A passable square J, Joe's initial position in the maze, which is a passable square F, A square that is on fireThere will be exactly one JIn each test case. Sample Input
24 4#####JF##..##..#3 3####J.#.F
Output SpecificationFor each test case, output a single line containing IMPOSSIBLEIf Joe cannot exit the maze beforethe fire reaches him, or an integer giving the earliest time Joe can safely exit themaze, in minutes. Output for Sample Input
3IMPOSSIBLE
Graph theory basics ..
AC code:
# Include
# Include
# Include
# Include
Using namespace std; const int maxn = 1010; int n, m; char g [maxn] [maxn]; // used to store the entire figure queue
> Q; // The queue int a [maxn] [maxn] For bfs; // It is used to store the time when each grid starts to fire. int move [] [2] = {0, 1 },{ 0,-1 },{ 1, 0 },{-1, 0 }}; // used for the first traversal of void bfs1 () from top to bottom, preprocessing of each grid fire time {memset (a,-1, sizeof (a); // initialize while (! Q. empty () q. pop (); // clear the queue for (int I = 0; I
Tmp = q. front (); q. pop (); int x = tmp. first, y = tmp. second; for (int I = 0; I <4; I ++) {int t1 = x + move [I] [0], t2 = y + move [I] [1]; if (a [t1] [t2]! =-1) continue; if (t1 <0 | t2 <0 | t1> = n | t2> = m) continue; if (g [t1] [t2] = '#') continue; a [t1] [t2] = a [x] [y] + 1; q. push (make_pair (t1, t2) ;}} int B [maxn] [maxn]; int bfs2 () // The second traversal {memset (B,-1, sizeof (B); while (! Q. empty () q. pop (); for (int I = 0; I
Tmp = q. front (); q. pop (); int x = tmp. first, y = tmp. second; if (x = 0 | y = 0 | x = n-1 | y = m-1) return B [x] [y] + 1; for (int I = 0; I <4; I ++) {int t1 = x + move [I] [0], t2 = y + move [I] [1]; if (t1 <0 | t2 <0 | t1> = n | t2> = m) continue; if (B [t1] [t2]! =-1) continue; if (g [t1] [t2] = '#') continue; if (a [t1] [t2]! =-1 & B [x] [y] + 1> = a [t1] [t2]) continue; B [t1] [t2] = B [x] [y] + 1; q. push (make_pair (t1, t2) ;}} return-1; // No Way To Go Out} int main () {int T; scanf ("% d ", & T); while (T --) {scanf ("% d", & n, & m); for (int I = 0; I