UVA 11624 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
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

题目大意:
J代表人,F代表火,#代表墙,.代表可以走的路,人可以走上下左右,火会向上下左右蔓延,蔓延后继续向上下左右蔓延,问人能否从火中逃出,若能,最少需要几步,若不能,输出IMPOSSIBLE

思路:
用两个数组分别代表火和人标记走过的路,若火的标记覆盖了人的标记,代表不可以走,会被烧到

代码:

/*************************************************************************
    > File Name: s5_D.cpp
    > Author: SIU
    > Created Time: 20161105日 星期六 112954************************************************************************/

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct node
{
    int x;
    int y;
    char value;
    int count;
}que[1000050];
int visitj[1050][1050];
int visitf[1050][1050];
char map[1050][1050];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(visitj,0,sizeof(visitj));
        memset(visitf,0,sizeof(visitf));
        int jx,jy;
        int fx[1000]={0};
        int fy[1000]={0};
        int len=0;
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%s",&map[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(map[i][j]=='J')
                {
                    jx=i;
                    jy=j;
                }
                if(map[i][j]=='F')
                {
                    fx[len]=i;
                    fy[len]=j;
                    len++;
                }
            }
        }
        int head=1;
        int tail=1;
        que[tail].x=jx;
        que[tail].y=jy;
        que[tail].value='J';
        que[tail].count=0;
        visitj[jx][jy]=1;
        tail++;
        for(int i=0;i<len;i++)
        {
            que[tail].x=fx[i];
            que[tail].y=fy[i];
            que[tail].value='F';
            que[tail].count=0;
            visitf[fx[i]][fy[i]]=1;
            tail++;
        }
        int tx;
        int ty;
        int flag=0;
        int next[4][2]=
        {
            {1,0},
            {0,1},
            {-1,0},
            {0,-1}
        };
        while(head<tail)
        {
            for(int i=0;i<4;i++)
            {
                if(visitf[que[head].x][que[head].y]==1&&que[head].value=='J')
                    break;
                tx=que[head].x+next[i][0];
                ty=que[head].y+next[i][1];
                if((tx<0||tx>=n||ty<0||ty>=m)&&que[head].value=='F')
                    continue;
                if((tx<0||tx>=n||ty<0||ty>=m)&&que[head].value=='J')
                {
                    que[tail].x=tx;
                    que[tail].y=ty;
                    que[tail].count=que[head].count+1;
                    que[tail].value='J';
                    //printf("%d %d %c %d\n",tx,ty,que[tail].value,que[tail].count);
                    flag=1;
                    break;
                }
                if(map[tx][ty]=='#')
                    continue;
                if(que[head].value=='J'&&visitj[tx][ty]==0&&visitf[tx][ty]==0)
                {
                    que[tail].x=tx;
                    que[tail].y=ty;
                    que[tail].value='J';
                    que[tail].count=que[head].count+1;
                    //printf("%d %d %c %d\n",tx,ty,que[tail].value,que[tail].count);
                    visitj[tx][ty]=1;
                    tail++;
                }
                if(que[head].value=='F'&&visitf[tx][ty]==0)
                {
                    que[tail].x=tx;
                    que[tail].y=ty;
                    que[tail].value='F';
                    que[tail].count=que[head].count+1;
                    //printf("%d %d %c %d\n",tx,ty,que[tail].value,que[tail].count);
                    visitf[tx][ty]=1;
                    tail++;
                }
            }
            head++;
            if(flag==1)
                break;
        }
        if(flag==1)
            printf("%d\n",que[tail].count);
        else
            printf("IMPOSSIBLE\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值