Joe works in a maze. Unfortunately, portions of the maze have
caught on fire, and the owner of the maze neglected to create a fireescape plan. Help Joe escape the maze.Given Joe’s location in the maze and which squares of the mazeare on fire, you must determine whether Joe can exit the maze beforethe fire reaches him, and how fast he can do it.Joe and the fire each move one square per minute, vertically orhorizontally (not diagonally). The fire spreads all four directionsfrom each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the firemay enter a square that is occupied by a wall.Input:
The first line of input contains a single integer, the number of testcases to follow. The first line of each test case contains the twointegers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. Thefollowing R lines of the test case each contain one row of the maze. Each of these lines contains exactlyC 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 fireThere will be exactly one J in each test case.Output:
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before thefire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.Sample Input:
24 4#####JF##..##..#3 3####J.#.FSample Output:3IMPOSSIBLE题意:有一个迷宫,那么又是逃离了,现在知道Joe的初始位置,也知道火种的初始位置,Joe和火种是可以同时移动的(上下左右),这次要逃离的标准是Joe比火种先到边界,任何一个边界都可以,就算逃离成功,那么我们只需要分别统计Joe和火种达到每一点需要的步数(时间),然后在边界位置进行比较就可以了,两个BFS就行了(关键是火种可能不止一处有,也可能一处都没有)。
#include#include #include #include using namespace std;const int N=1100;const int INF=0x3f3f3f3f;struct node{ int x, y;}no[N]; ///no结构体记录每个火种的初始位置char Map[N][N];int k, m, n, X, Y, visJ[N][N], visF[N][N]; ///k表示火种的个数,X和Y记录Joe的初始位置,visJ数组保存Joe到达每一点需要的时间,visF数组保存火种到达每一点需要的时间int dir[4][2] = { { 1,0}, {-1,0}, { 0,1}, { 0,-1} };void Init(){ int i, j; for (i = 0; i <= m; i++) { for (j = 0; j <= n; j++) { visJ[i][j] = 0; visF[i][j] = INF; ///这里如果和visJ一样初始化为0,那么当没有火种时,visJ显然是不能小于visF的,那不就不能逃离了,其实这种情况下只要边界不是'#',就可以逃离,所以初始化为INF } } k = 0;}void BFSF() ///计算火种到达每一点的时间{ int i, x, y; node next, now; queue Q; for (i = 0; i < k; i++) { now.x = no[i].x; now.y = no[i].y; visF[now.x][now.y] = 1; Q.push(now); } while (!Q.empty()) { now = Q.front(); Q.pop(); for (i = 0; i < 4; i++) { next.x = x = now.x + dir[i][0]; next.y = y = now.y + dir[i][1]; if (x >= 0 && x < m && y >= 0 && y < n && Map[x][y] != '#' && visF[x][y] == INF) { visF[x][y] = visF[now.x][now.y] + 1; Q.push(next); } } }}int BFSJ() ///计算Joe到达每一点的时间,并在边界与火种比较{ int i, x, y; node next, now; queue Q; now.x = X; now.y = Y; Q.push(now); visJ[X][Y] = 1; while (!Q.empty()) { now = Q.front(); Q.pop(); if ((now.x == 0 || now.x == m-1 || now.y == 0 || now.y == n-1) && visJ[now.x][now.y] < visF[now.x][now.y]) return visJ[now.x][now.y]; for (i = 0; i < 4; i++) { next.x = x = now.x + dir[i][0]; next.y = y = now.y + dir[i][1]; if (x >= 0 && x < m && y >= 0 && y < n && Map[x][y] != '#' && !visJ[x][y]) { visJ[x][y] = visJ[now.x][now.y] + 1; Q.push(next); } } } return -1;}int main (){ int T, i, j, ans; scanf("%d", &T); while (T--) { scanf("%d%d", &m, &n); for (i = 0; i < m; i++) scanf("%s", Map[i]); Init(); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { if (Map[i][j] == 'J') { X = i; Y = j; } else if (Map[i][j] == 'F') ///火种可能不止一处 { no[k].x = i; no[k++].y = j; } } } BFSF(); ans = BFSJ(); if (ans == -1) printf("IMPOSSIBLE\n"); else printf("%d\n", ans); } return 0;}