BFS求迷宫
来源:互联网 发布:天猫淘宝设计规范 编辑:程序博客网 时间:2024/05/14 15:15
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;}}
- BFS求迷宫
- BFS求迷宫的最短路径
- dfs求连通性,bfs求迷宫最短路
- BFS~~~迷宫
- BFS(迷宫)
- 迷宫-BFS
- BFS------迷宫
- 迷宫-BFS
- POJ 3322 Bloxorz I(BFS:求迷宫最短路径)
- poj 2251 < bfs 求3D迷宫出路>
- BFS循环求迷宫最短路径及路径数
- [容易] BFS求最短路(迷宫类问题)
- 走迷宫sdut1269(附带dfs求所有路径与bfs求最佳路径)
- hdu1728 逃离迷宫(bfs)
- POJ3984 迷宫问题 BFS
- 经典迷宫问题BFS
- 迷宫问题 bfs
- 迷宫问题BFS暴搜
- mariadb安装与使用
- handlebars-----with的this终极应用
- 编译python3.4
- Odoo运行机制(四)----URL路由
- C++ List的用法(整理)
- BFS求迷宫
- 纯C++的Socket访问Http封装类
- 理解矩阵(一)
- rabbitmq的简单管理命令笔记
- 中文注释 MariaDB my.cnf 大型服务器配置模板
- Git的使用十五(Feature分支)仅限Mac亲测
- css ie6,ie7,ie8 兼容性写法,CSS hack写法
- C++ Template之技巧性基础知识 和 typeid(x).name()用法
- 详解Linux内核之双向循环链表