`

Uva 11624 - Fire! 多源宽度优先遍历

 
阅读更多

   连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=543&problem=2671&mosmsg=Submission+received+with+ID+11653475

 

Problem B: 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 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

 

/*
普通的宽度搜索题
先预处理一下map,找出每个点的着火时间,然后再次宽度搜索
如果人走到那一点的时间小于着火时间,才能入队。
*/
#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
using namespace std;
const int M=1002;
char map[M][M];//存放地图
int newmap[M][M];//存放地图上的点的着火时间
int fang[4][2]={0,1,0,-1,1,0,-1,0};//方向数组
struct node
{
       int x;
       int y;
       int step;//记录步数
}J,F,term;
queue<node>q;
int R,C;
int main()
{
       int T,i,j,flag;
       scanf("%d",&T);
       while(T--)
       {
              while(!q.empty())q.pop();
              memset(newmap,0,sizeof(newmap));
              memset(map,0,sizeof(map));
              scanf("%d%d",&R,&C);
              for(i=1;i<=R;i++)
              {
                     for(j=1;j<=C;j++)
                     {
                            cin>>map[i][j];
                            if(map[i][j]=='J')
                            {
                                   J.x=i;
                                   J.y=j;
                                   J.step=0;
                            }
                            if(map[i][j]=='F')
                            {
                                   F.x=i;
                                   F.y=j;
                                   map[i][j]='#';
                                   q.push(F);//着火点不止一个
                            }
                     }
              }
              while(!q.empty())
              {
                     term=q.front();
                     q.pop();
                     for(i=0;i<4;i++)
                     {
                            node t;
                            t.x=term.x+fang[i][0];
                            t.y=term.y+fang[i][1];
                            if(map[t.x][t.y]==0||map[t.x][t.y]=='#'||newmap[t.x][t.y]!=0&&(newmap[t.x][t.y]<=newmap[term.x][term.y]+1))continue;
                            newmap[t.x][t.y]=newmap[term.x][term.y]+1;
                            q.push(t);
                     }
              }
              q.push(J);
              flag=0;
              while(!q.empty())
              {
                     term=q.front();
                     q.pop();
                     for(i=0;i<4;i++)
                     {
                            node t;
                            t.x=term.x+fang[i][0];
                            t.y=term.y+fang[i][1];
                            t.step=term.step+1;
                            if(map[t.x][t.y]==0){flag=1;break;}//逃出
                            if(map[t.x][t.y]=='#'||newmap[t.x][t.y]!=0&&newmap[t.x][t.y]<=t.step)continue;
                            map[t.x][t.y]='#';
                            q.push(t);
                     }
                     if(flag)break;
              }
              if(flag==1)printf("%d\n",term.step+1);
              else printf("IMPOSSIBLE\n");
       }
       return 0;
}

 

 

 

 

 

 

1
8
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics