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 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.

扫描二维码关注公众号,回复: 8620348 查看本文章

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;
}
发布了72 篇原创文章 · 获赞 203 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/weixin_41676881/article/details/89409745
今日推荐