uva 11624 - Fire!(Bfs)

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的相当丑陋
模仿着自己写了一遍,用了两次bfs,作者的是归在一起写的,相对简洁了很多

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

int R,C;
const int maxr = 1000 + 5;
const int maxc = 1000 + 5;
char map[maxr][maxc];

struct Node{
    int r,c,t;
    Node(int r,int c,int t):r(r),c(c),t(t){}
};
int vis[maxr][maxc];
int time[maxr][maxc];
int ans[maxr][maxc];
const int dr[] = {-1,1,0,0};
const int dc[] = {0,0,1,-1};
const int INF = 10000000;
queue<Node> q;

void init(){
    while(!q.empty())
        q.pop();
    for(int i = 0;i < R;i++){
        for(int j = 0;j < C;j++){
            time[i][j] = INF;
            ans[i][j] = INF;
            vis[i][j] = 0;
        }
    }
}

void init_time(){
    while(!q.empty()){
        int r = q.front().r;
        int c = q.front().c;
        int t = q.front().t;
        q.pop();
        for(int i = 0;i < 4;i++){
            int nr = r+dr[i];
            int nc = c+dc[i];
            if(nr >= 0 && nr < R && nc >= 0 && nc < C && !vis[nr][nc] && map[nr][nc] == '.'){
                q.push(Node(nr,nc,t+1));
                time[nr][nc] = t+1;
                vis[nr][nc] = 1;
            }
        }
    }
}

void Bfs(){
    while(!q.empty()){
        int r = q.front().r;
        int c = q.front().c;
        int t = q.front().t;
        q.pop();
        for(int i = 0;i < 4;i++){
            int nr = r + dr[i];
            int nc = c + dc[i];
            if(nr >= 0 && nr < R && nc >= 0 && nc < C && !vis[nr][nc] && map[nr][nc] == '.' && (t+1) < time[nr][nc]){
                q.push(Node(nr,nc,t+1));
                ans[nr][nc] = t+1;
                vis[nr][nc] = 1;
            }
        }
    }
}

int main(){
    int t,jr,jc;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&R,&C);
        init();
        for(int i = 0;i < R;i++){
            scanf("%s",map[i]);
            for(int j = 0 ;j < C;j++){
                if(map[i][j] == 'F'){
                    q.push(Node(i,j,0));
                    time[i][j] = 0;
                    vis[i][j] = 1;
                }
                else if(map[i][j] == 'J'){
                    jr = i;
                    jc = j;
                    ans[i][j] = 0;
                }
            }
        }
        init_time();
        memset(vis,0,sizeof(vis));
        vis[jr][jc] = 1;
        while(!q.empty())
            q.pop();
        q.push(Node(jr,jc,0));
        Bfs();

        int Ans = INF;
        for(int i = 0;i < R;i++){
            Ans = min(Ans,ans[i][0]);
            Ans = min(Ans,ans[i][C-1]);
        }
        for(int i = 0;i < C;i++){
            Ans = min(Ans,ans[0][i]);
            Ans = min(Ans,ans[R-1][i]);
        }
        if(Ans == INF)
            printf("IMPOSSIBLE\n");
        else
            printf("%d\n",Ans+1);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值