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;
}


 

你可能感兴趣的:(uva 11624 - Fire!(Bfs))