美文网首页
UVA11624——Fire!

UVA11624——Fire!

作者: xz闲语岁月 | 来源:发表于2017-08-03 23:41 被阅读0次

Problem

UVA11624.png

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 <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char maze[1001][1001];
int vis[1001][1001];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int X,Y,sx,sy;
struct Point{
    int x,y,step,isFire;
    Point(int x,int y,int step,int isFire):x(x),y(y),step(step),isFire(isFire){}
};
queue<Point>q;

int BFS(){
    while(!q.empty()){
        Point cur = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            int tx = cur.x + dx[i];
            int ty = cur.y + dy[i];
            if(tx < X && tx >= 0 && ty < Y && ty >= 0){
                if(!vis[tx][ty]){
                    vis[tx][ty] = 1;
                    q.push(Point(tx,ty,cur.step+1,cur.isFire));
                }
            }
            else if(!cur.isFire) return cur.step+1;
        }
    }
    return -1;
}
int main(){
    int k;
    scanf("%d",&k);
    while(k--){
        scanf("%d %d",&X,&Y);
        memset(vis,0,sizeof(vis));
        while(!q.empty()) q.pop();
        for(int i = 0; i < X; ++i){
            scanf("%s",maze[i]);
            for(int j = 0; j < Y; ++j){
                if(maze[i][j] == 'F'){
                    vis[i][j] = 1;q.push(Point(i,j,0,1));
                }
                if(maze[i][j] == 'J') {vis[i][j] = 1;sx=i;sy=j;}
                if(maze[i][j] == '#') vis[i][j] = 1;
            }
        }
        q.push(Point(sx,sy,0,0));
        int res = BFS();
        if(res == -1) printf("IMPOSSIBLE\n");
        else printf("%d\n",res);
    }
    return 0;
}

相关文章

  • UVA11624——Fire!

    Problem Joe works in a maze. Unfortunately, portions of t...

  • Fire! Fire!

    两个人的狂欢 三个人的落寞 千万人的飞蛾扑火 视死如归的决心 燃烧吧,熊熊火焰 Fire! Fire! 耀眼的光芒...

  • Fire and fire!

    写在前面,加入了一个写作训练营,根据每天话题写一篇文章。今日题目:如果发生火灾了,你会选择带走哪五样东西 “最新消...

  • fire

    family game look for the fire Your arm is on fire, pat it...

  • 8/70 豆苗写作:A Fire Drill

    What is a fire drill? A fire drill is to practice what yo...

  • 我们活在火灾现场,无处逃生

    we all in a house in fire no fire department to call no w...

  • 2019-03-09

    fire, it wont burn my heart is studded with dirt fire, it...

  • Using 'fire to fight fire' to co

    Using 'fire to fight fire' to combat disease could make i...

  • Fire and Ice

    Fire and Ice by Robert Frost Fire and Ice Some say the wo...

  • 火与冰的世界

    "Fire and Ice" 《火与冰》 Some say the world will end in fire,...

网友评论

      本文标题:UVA11624——Fire!

      本文链接:https://www.haomeiwen.com/subject/rjvmlxtx.html