UVA-11624 Fire!(bfs)

Problem Description: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 Jo...
摘要由CSDN通过智能技术生成

Problem Description:

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

思路:

遍历整个地图,一定要让火(下称F)占据主动权,因此一定要先将所有的F都入队之后再将人(下称J)入队。这样能够保证每个回合都是F先走,F到过的地方J就不能再到了。

上AC代码:

#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
char Map[1001][1001];
int dp[1001][1001];
bool vis[1001][1001];
int t;
int row,col;
struct pos
{
    int x;
    int y;
    bool isF;
};
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        int i,j;
        scanf("%d%d",&row,&col);
        for(i=0;i<row;i++)
        {
            scanf("%s",Map[i]);
        }
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        queue<pos> que;
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                if(Map[i][j]=='F')
                {
                    pos temp;
                    temp.x=i;
                    temp.y=j;
                    temp.isF=true;
                    vis[i][j]=true;
                    que.push(temp);
                }
            }
        }
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                if(Map[i][j]=='J')
                {
                    pos temp;
                    temp.x=i;
                    temp.y=j;
                    temp.isF=false;
                    vis[i][j]=true;
                    que.push(temp);
                    break;
                }
            }
        }
        bool flag=false;
        int res=0;
        while(!que.empty())
        {
            pos temp=que.front();
            que.pop();
            int dx,dy;
            for(dx=-1;dx<=1;dx++)
            {
                for(dy=-1;dy<=1;dy++)
                {
                    if(!(dx==0||dy==0))
                        continue;
                    //火的路径
                    if(temp.isF)
                    {
                        if(temp.x+dx>=0&&temp.x+dx<row&&temp.y+dy>=0&&temp.y+dy<col&&vis[temp.x+dx][temp.y+dy]==false&&(Map[temp.x+dx][temp.y+dy]=='.'||Map[temp.x+dx][temp.y+dy]=='J'))
                        {
                            pos next;
                            next.isF=true;
                            next.x=temp.x+dx;
                            next.y=temp.y+dy;
                            vis[next.x][next.y]=true;
                            que.push(next);
                        }
                    }
                    //人的路径
                    else
                    {
                        if(temp.x+dx<0||temp.x+dx>=row||temp.y+dy<0||temp.y+dy>=col)
                        {
                            flag=true;
                            res=dp[temp.x][temp.y]+1;
                            break;
                        }
                        if(temp.x+dx>=0&&temp.x+dx<row&&temp.y+dy>=0&&temp.y+dy<col&&vis[temp.x+dx][temp.y+dy]==false&&(Map[temp.x+dx][temp.y+dy]=='.'||Map[temp.x+dx][temp.y+dy]=='J'))
                        {
                            dp[temp.x+dx][temp.y+dy]=dp[temp.x][temp.y]+1;
                            pos next;
                            next.isF=false;
                            next.x=temp.x+dx;
                            next.y=temp.y+dy;
                            vis[next.x][next.y]=true;
                            que.push(next);
                        }
                    }
                }
                if(flag)
                    break;
            }
            if(flag)
                break;
        }
        if(flag)
        {
            printf("%d\n",res);
        }
        else
        {
            printf("IMPOSSIBLE\n");
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值