uva 11624 - Fire!(bfs,3级)

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

Malcolm Sharpe, Ondřej Lhoták

思路:入门级BFS,加了fire,其实还是一样,只要预处理把所有fire点入队,然后再把起始点入队,进行BFS,搜到边界就有解。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int mm=1000+9;
const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};
bool vis[mm][mm];
class node
{
  public:int x,y,fire,step;
};
char g[mm][mm];
int n,m,cas;
node s,t;
queue<node>q;
void get(int n,int m)
{while(!q.empty())q.pop();
 memset(vis,0,sizeof(vis));
 char c;
 for(int i=0;i<n;++i)
 for(int j=0;j<m;++j)
 {  while(1)
    {c=getchar();
     switch(c)
     {
       case '.':g[i][j]=c;goto there;
       case '#':g[i][j]=c;goto there;
       case 'J':s.x=i;s.y=j;s.step=0;s.fire=0;g[i][j]=c;vis[i][j]=1;goto there;
       case 'F':t.x=i;t.y=j;t.fire=1;t.step=0;q.push(t);g[i][j]=c;vis[i][j]=1;goto there;
     }
    }
    there:;
 }
 q.push(s);
}
int bfs()
{ node z;
  while(!q.empty())
  {
    z=q.front();q.pop();
    for(int i=0;i<4;++i)
    {
      t=z;t.x+=dx[i];t.y+=dy[i];t.step++;
      if(z.fire)
      {
        if(t.x<0||t.x>=n||t.y<0||t.y>=m||vis[t.x][t.y]||g[t.x][t.y]=='#')continue;
        q.push(t);vis[t.x][t.y]=1;
      }
      else
      {
        if(t.x<0||t.x>=n||t.y<0||t.y>=m)return t.step;
        if(vis[t.x][t.y]||g[t.x][t.y]=='#')continue;
        q.push(t);vis[t.x][t.y]=1;
      }
    }
  }
  return -1;
}
int main()
{
  while(~scanf("%d",&cas))
  {
    while(cas--)
    {
      scanf("%d%d",&n,&m);
      get(n,m);
      int ans=bfs();
      if(ans<0)printf("IMPOSSIBLE\n");
      else printf("%d\n",ans);
    }
  }
  return 0;
}




转载于:https://www.cnblogs.com/nealgavin/p/3205905.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值