题目链接
Problem B: Fire!
Joe wZ??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcmtzIGluIGEgbWF6ZS4gVW5mb3J0dW5hdGVseSwgcG9ydGlvbnMgb2YgdGhlIG1hemUgaGF2ZSBjYXVnaHQgb24gZmlyZSwgYW5kCiB0aGUgb3duZXIgb2YgdGhlIG1hemUgbmVnbGVjdGVkIHRvIGNyZWF0ZSBhIGZpcmUgZXNjYXBlIHBsYW4uIEhlbHAgSm9lIGVzY2FwZSB0aGUgbWF6ZS4KPHA+PC9wPgo8cD5HaXZlbiBKb2U="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 (not diagonally). The fire spreads all four directions from each square that is on fire. 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 Specification
The 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 integers R and C, separated by spaces, with 1 <= R,C <= 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of:-
#, a wall
., a passable square
J, Joe's initial position in the maze, which is a passable square
F, a square that is on fire There will be exactly one
J in each test case.
Sample Input
2 4 4 #### #JF# #..# #..# 3 3 ### #J. #.F
Output Specification
For each test case, output a single line containing IMPOSSIBLE if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.Output for Sample Input
3 IMPOSSIBLE
Malcolm Sharpe, Ond?ej Lhoták
题目大意:一个平面迷宫中有一个人,迷宫中有些点起火了,火和人每个单位时间只能向相邻的格子移动,其中有一些空间被墙壁占据,问这个人在不背或烧到的情况下,离开迷宫的最快时间。
题解:两次bfs,先bfs出每个地方着火的时间,再bfs人走出迷宫的时间。注意有的地方火不能到达但人是可以走的。
/** * @author neko01 */ //#pragma comment(linker, "/STACK:102400000,102400000") #include#include #include #include #include #include #include #include #include #include