UVA11624(KB1-J)

Posted Penn000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11624(KB1-J)相关的知识,希望对你有一定的参考价值。

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 (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

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.

Output

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.

Sample Input

2

4 4

####

#JF#

#..#

#..#

3 3

###

#J.

#.F

Sample Output

3

IMPOSSIBLE

  1 //2017-03-01
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <queue>
  6 
  7 using namespace std;
  8 
  9 struct node{
 10     int x, y, step;
 11     void setNode(int x, int y, int step){
 12         this->x = x;
 13         this->y = y;
 14         this->step = step;
 15     }
 16 };
 17 char maze[1005][1005];
 18 int dis[1005][1005], n, m;
 19 bool vis[1005][1005];
 20 const int inf = 0x3f3f3f3f;
 21 int dx[4] = {0, 1, 0, -1};
 22 int dy[4] = {1, 0, -1, 0};            
 23 
 24 void bfs(int sx, int sy)
 25 {
 26     queue<node> q;
 27     node tmp;
 28     memset(vis, 0, sizeof(vis));
 29     if(sx == -1){
 30         for(int i = 0; i < n; i++)
 31               for(int j = 0; j < m; j++)
 32                   if(maze[i][j] == F)
 33                 {
 34                     tmp.setNode(i, j, 0);
 35                     q.push(tmp);
 36                     vis[i][j] = 1;
 37                     dis[i][j] = 0;
 38                 }
 39     }else{
 40         tmp.setNode(sx, sy, 0);
 41         q.push(tmp);
 42         vis[sx][sy] = 1;
 43     }
 44     int x, y, step, ans;
 45     bool fg = false;
 46     while(!q.empty())
 47     {
 48         x = q.front().x;
 49         y = q.front().y;
 50         step = q.front().step;
 51         q.pop();
 52         if(maze[sx][sy]==J&&(x == n-1 || y == m-1 || x == 0 || y == 0)){
 53             fg = true;
 54             ans = step+1;                                                
 55         }
 56         for(int i = 0; i < 4; i++)
 57         {
 58             int nx = x + dx[i];
 59             int ny = y + dy[i];
 60             if(nx>=0&&nx<n&&ny>=0&&ny<m&&!vis[nx][ny]&&maze[nx][ny]!=#){
 61                 vis[nx][ny] = 1;
 62                 if(maze[sx][sy]==J){
 63                       if(step+1 >= dis[nx][ny])continue;
 64                     else if(nx == n-1 || ny == m-1 || nx == 0 || ny == 0){
 65                         fg = true;
 66                         ans = step+2;
 67                     }
 68                 }else dis[nx][ny] = min(dis[nx][ny], step+1);
 69                 tmp.setNode(nx, ny, step+1);
 70                 q.push(tmp);
 71             }
 72         }
 73         if(fg)break;
 74     }
 75     if(maze[sx][sy] == J)
 76           if(fg)printf("%d\n", ans);
 77         else printf("IMPOSSIBLE\n");
 78 }
 79 
 80 int main()
 81 {
 82     int T, jx, jy;
 83     scanf("%d", &T);
 84     while(T--)
 85     {
 86         scanf("%d%d", &n, &m);
 87         getchar();
 88         for(int i = 0; i < n; i++){
 89               for(int j = 0; j < m; j++){
 90                   scanf("%c", &maze[i][j]);
 91                 if(maze[i][j] == J){
 92                     jx = i; jy = j;
 93                 }
 94             }
 95             getchar();
 96         }
 97         memset(dis, inf, sizeof(dis));
 98         bfs(-1, -1);
 99         bfs(jx, jy);
100     }
101 
102     return 0;
103 }

 

以上是关于UVA11624(KB1-J)的主要内容,如果未能解决你的问题,请参考以下文章

UVA 11624 Fire!两点BFS

Fire! UVA - 11624

UVa 11624 大火蔓延的迷宫

UVA 11624 Fire

UVA 11624

UVA 11624 不知道为什么WA