Fire! UVA - 11624

题目链接:Fire! UVA - 11624

===================================================

Fire

Time Limit: 1000MS

Description

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

===================================================

算法:BFS

思路:

  • 火要比人先进队列 易错点在于 开头不止一个火苗

===================================================

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string.h>
#include <queue>

using namespace std;

int n, m , u ,v;
int xx[4] = { 0, -1, 0 ,1 };
int yy[4] = { 1, 0, -1, 0 };
char sm[1005][1005];
struct node{
    int x,y,step,f;
    node(int xx,int yy,int sstep,int ff):x(xx),y(yy),step(sstep),f(ff) {};
};

bool bfs(){
    queue<node> q;
    for(int i=0;i<n;i++)
        for(int j= 0;j<m;j++)
            if(sm[i][j]=='F') sm[i][j] = '#' , q.push(node(i , j, 1, 0));
            else if(sm[i][j]=='J') sm[i][j] = '#' , u = i , v = j;
    q.push(node(u , v, 1, 1));
    while(!q.empty()){
        node e = q.front();q.pop();
        int x , y , s , f;x = e.x , y = e.y ,s = e.step , f = e.f;
        //cout<< x << " " << y <<" "<< s << " "<< f<<endl;
        if(f == 1&&(x==0||y==0||x==n-1||y==m-1)) {printf("%d\n",s);return true;}
        for(int i = 0;i< 4;i ++){
            int a,b;
            a = x + xx[i] , b = y + yy[i];
            if(a<0||b<0||a>=n||b>=m||sm[a][b]=='#') continue;
            //cout<< a << " " << b <<" "<< s+1 << " "<< f<<endl;
            q.push(node(a,b,s+1,f));
            sm[a][b]='#';
        }
    }
    return false;
}

int main()
{
    int _;scanf("%d",&_);
    while(_--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++) scanf("%s",sm[i]);
        if(!bfs()) puts("IMPOSSIBLE");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盐太郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值