BFS求迷宫

来源:互联网 发布:天猫淘宝设计规范 编辑:程序博客网 时间:2024/05/14 15:15
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 (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

54 4#####JF##..##..#3 3####J.#.F9 11.#..........#F#######..#.#.....#..#J#.###.#..#.#..F#.#..#.#####.#..#..........#########............6 11..#..#..#....#.F#..#....#FJ#..###..#..#..##...#..#..#....#..#..#..10 10#........##..#.##.###.##.##.##.#....##.###.##..#.##..#.##F.J#....F.##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

12

4

1


#include<iostream>#include<string.h>#include<algorithm>using namespace std;const int maxr = 1000 + 5;const int maxc = 1000 + 5;const int INF = 1000000000;char Map[maxr][maxc];bool visted[2][maxr][maxc];int ji, jj;//joe的出生地int number;//共有多少个Fireint dis[2][maxr][maxc];//dis[0][maxr][maxc]定义了joe到达每个点的路径//dis[1][maxr][maxc]定义了Fire到达每个点的路径int direction[4][2] = { {-1,0}, {0,1}, {0,-1}, {1,0} };struct point{int x;int y;};struct point Fire[maxr];//Fire的出生地struct MyQueue{int front;int rear;struct point D[maxr];}Queue;int row, column;//表示几行几列void bfs(int kind){while (Queue.front != Queue.rear){struct point curr = Queue.D[Queue.front++];for (int i = 0; i < 4; i++){int nx = curr.x + direction[i][0];int ny = curr.y + direction[i][1];if (nx >= 0 && nx < column && ny >= 0 && ny < column && Map[nx][ny] == '.' && !visted[kind][nx][ny]){visted[kind][nx][ny] = true;struct point p;p.x = nx; p.y = ny;Queue.D[Queue.rear++] = p;dis[kind][nx][ny] = dis[kind][curr.x][curr.y] + 1;}}}}int ans;void check(int r, int c){if (Map[r][c] != '.' || !visted[0][r][c])return; // 必须是Joe可达的边界格子if (!visted[1][r][c] || dis[0][r][c] < dis[1][r][c])ans = min(ans, dis[0][r][c] + 1); // Joe必须先于火到达}int main(){//freopen("sample_input.txt", "r", stdin);int T;cin >> T;for (int test_case = 0; test_case < T; test_case++){cin >> row >> column;memset(Map, '#', sizeof(Map));memset(visted, false, sizeof(visted));memset(dis, 0, sizeof(dis));for (int i = 0; i < row; i++){cin >> Map[i];for (int j = 0; j <= column; j++){if (Map[i][j] == 'J'){ji = i; jj = j;Map[i][j] = '.';}else if (Map[i][j] == 'F'){struct point tmp;tmp.x = i; tmp.y = j;Fire[number++] = tmp;Map[i][j] = '.';}}}//初始化QueueQueue.front = Queue.rear = 0;//joe的BFS求解dirvisted[0][ji][jj] = true;dis[0][ji][jj] = 0;//joe的出生点入队列struct point tmp;tmp.x = ji;tmp.y = jj;Queue.D[Queue.rear++] = tmp;bfs(0);//Fire的BFS求解dir,Fire可能不止一个for (int i = 0; i < number; i++){visted[1][Fire[i].x][Fire[i].y] = 1;dis[1][Fire[i].x][Fire[i].y] = 0;Queue.D[Queue.rear++] = Fire[i];}bfs(1);//check4个方向ans = INF;//check left and rightfor (int i = 0; i < row; i++){check(i, 0);check(i, column - 1);}//check top and bottomfor (int i = 0; i < column; i++){check(0, i);check(row - 1, i);}if (ans == INF)cout<<"IMPOSSIBLE"<<endl;elsecout<<ans << endl;}}


0 0
原创粉丝点击