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 是火,火每分钟会烧周围四个,火会把人烧死,问你人怎么走才能用最少的时间出去,(注意,墙 # 是火烧不到的地方)
题解:这道坑人的题要弄两个bfs,第一个先把火烧的步骤用一个数组记录下来,第二个才是人走的,并且人走的步骤要快于火烧的步骤;

#include<stdio.h>
#include<string.h>
#include <queue>
#include<algorithm>
using namespace std;

char map[1110][1110];
bool vis[1110][1110];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int fire[1010][1010];//火烧数组
struct node//可以不用优先队列,主要是错多了害怕,用优先队列找心理安慰
{
    int x, y, step;
    friend bool operator <(node t1, node t2)
    {
        return t1.step > t2.step;
    }
};
node ne, no;
int n, m, ex, ey;
bool okf(int x, int y)//对于火烧的判断
{
    if(x < 0 || x >= n || y < 0 || y >= m || fire[x][y] || map[x][y] == '#')//墙是烧不到的
        return false;
    return true;
}
void bfs_fire()
{
    node nf;
    priority_queue<node> q;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(map[i][j] == 'F')
            {
                nf.x = i;
                nf.y = j;
                nf.step = 1;
                fire[i][j] = 1;
                q.push(nf);
            }
        }
    }
    while(!q.empty())
    {
        nf = q.top();
        q.pop();
        for(int i=0; i<4; i++)
        {
            ne.x = nf.x + dx[i];
            ne.y = nf.y + dy[i];
            if(okf(ne.x, ne.y))
            {
                ne.step = nf.step + 1;
                fire[ne.x][ne.y] = ne.step;//记录火烧位置的步骤
                q.push(ne);
            }
        }
    }
}
int bfs()
{
    priority_queue<node> q;
    q.push(no);
    while(!q.empty())
    {
        no = q.top();
        q.pop();
        for(int i=0; i<4; i++)
        {
            ne.x = no.x + dx[i];
            ne.y = no.y + dy[i];
            if(ne.x < 0 || ne.x >= n || ne.y < 0 || ne.y >= m)
                return no.step + 1;
            if(!vis[ne.x][ne.y] && (fire[ne.x][ne.y] > no.step + 2 || fire[ne.x][ne.y] == 0) && map[ne.x][ne.y] != '#')//不用忽略了有的点是火烧不到的
            {
                vis[ne.x][ne.y] = true;
                ne.step = no.step + 1;
                q.push(ne);
            }
        }
    }

    return -1;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        memset(fire, 0, sizeof(fire));
        for(int i=0; i<n; i++)
        {
            scanf("%s", map[i]);
            for(int j=0; j<m; j++)
            {
                if(map[i][j] == 'J')
                {
                    no.x = i;
                    no.y = j;
                    no.step = 0;
                }
            }
        }
        bfs_fire();
        memset(vis, false, sizeof(vis));
        vis[no.x][no.y] = true;
        int ans = bfs();
        if(ans == -1)
            printf("IMPOSSIBLE\n");
        else
            printf("%d\n", ans);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值