uva11624 Fire!

Fire!

Crawling failed
Description
Download as PDF
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


题意:走出一个大火弥漫的迷宫,每分钟可以往上往下往左往右走(火也可以走),输出走出迷宫的最短时间,不能走出输出IMPOSSIBLE.

分析:如果没有火的话,这就是一个迷宫了,直接BFS就行,所以我们要先处理一下火,先对火进行一次BFS(BFS的时候将所有有火标记的点加入队列BFS),求出火弥漫到点的时间,然后再对人进行BFS,当人和火都能到达一个点是,就比较人和火到达这个点的时间,人到达的时间如果不小于火到达的就说明不能到达了,因为火已经到了,那个点已经不能走了。。


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=1010;
const int INF=9999999;
char mat[MAXN][MAXN];
int n,m,dis[MAXN][MAXN],fir[MAXN][MAXN];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m)
        return 1;
    return 0;
}
bool ok(int x,int y)
{
    if(x==0||x==n-1||y==0||y==m-1)
        return 1;
    return 0;
}
void bfs1()
{
    int i,j;
    queue<int> q;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(mat[i][j]=='F')
            {
                q.push(i*m+j);
                fir[i][j]=0;
            }
        }
    }
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        int x=temp/m;
        int y=temp%m;
        for(i=0;i<4;i++)
        {
            int nx=x+dir[i][0];
            int ny=y+dir[i][1];
            if(!judge(nx,ny))
                continue;
            if(mat[nx][ny]=='#')
                continue;
            if(fir[x][y]+1<fir[nx][ny])
            {
                fir[nx][ny]=fir[x][y]+1;
                q.push(nx*m+ny);
            }
        }
    }
}
void bfs2(int sx,int sy)
{
    int i,j;
    queue<int> q;
    memset(dis,-1,sizeof(dis));
    dis[sx][sy]=0;
    q.push(sx*m+sy);
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        int x=temp/m;
        int y=temp%m;
        if(ok(x,y))
        {
            printf("%d\n",dis[x][y]+1);
            return;
        }
        for(i=0;i<4;i++)
        {
            int nx=x+dir[i][0];
            int ny=y+dir[i][1];
            if(!judge(nx,ny))
                continue;
            if(dis[nx][ny]!=-1)
                continue;
            if(mat[nx][ny]=='#'||mat[nx][ny]=='F')
                continue;
            if(dis[x][y]+1>=fir[nx][ny])
                continue;
            dis[nx][ny]=dis[x][y]+1;
            q.push(nx*m+ny);
        }
    }
    printf("IMPOSSIBLE\n");
}
int main()
{
    int t,i,j,x,y,flag;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
            scanf("%s",mat[i]);
        flag=1;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                fir[i][j]=INF;
                if(mat[i][j]=='J')
                {
                    x=i,y=j;
                    flag=0;
                }
            }
        }
        if(flag)
        {
            printf("1\n");
            continue;
        }
        bfs1();
        bfs2(x,y);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值