Fire!

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
f代表火源,向周围蔓延,然后问这个人能不能跑出去。这个题一下子就想起来上次福州校赛的那个搜索题,一模一样。现在看之后,每个坐标设置两个属性,一个是火到的时间,另一个是人到的时间,如果人到的时间小于火,那就能到达这个地方,继续搜,思路很明了,哦也!
然而,首先,火源不止一个……wa×1;
其次,输入输出最好用scanf,换行用\n;wa×1;
最后,火源要一口气全入队,我傻逼的一个一个入队搜索了……wa×1;
读题,操作技巧,处理方法都不过关。

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
int r, c, leap[1005][1005];
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
int min(int a, int b)
{
    return a < b ? a : b;
}
struct Node{
    int x;
    int y;
    char c;
    int ans1;
    int ans2;
}node[1005][1005];
Node now[1000007];
queue<Node>p;
void bfs(int k)
{
    int x2, y2, i, j, tempx, tempy;
    Node temp;
    for (i = 1; i <= r; i++)
    for (j = 1; j <= c; j++)
        leap[i][j] = 0;
    for (i = 1; i <= k; i++)
    {
        temp = now[i];
        leap[temp.x][temp.y] = 1;
        p.push(temp);
    }
    while (p.size())
    {
        temp = p.front();
        for (i = 0; i < 4; i++)
        {
            tempx = temp.x + dir[i][0];
            tempy = temp.y + dir[i][1];
            if (tempx >= 1 && tempx <= r&&tempy >= 1 && tempy <= c)
            {
                if (!leap[tempx][tempy] && node[tempx][tempy].c != '#')
                {
                    leap[tempx][tempy] = 1;
                    node[tempx][tempy].ans1 = min(node[temp.x][temp.y].ans1 + 1, node[tempx][tempy].ans1);
                    p.push(node[tempx][tempy]);
                }
            }
        }
        p.pop();
    }
}
int main()
{
    int t, i, j, xo, yo, x2, y2, tempx, tempy, time, k;
    Node temp;
    cin >> t;
    while(t--)
    {
        cin >> r >> c;
        getchar();
        k = 0;
        for (i = 1; i <= r; i++)
        {
            for (j = 1; j <= c; j++)
            {
                scanf("%c", &node[i][j].c);
                node[i][j].x = i;
                node[i][j].y = j;
                node[i][j].ans1 = node[i][j].ans2 = 1000007;
                leap[i][j] = 0;
                if (node[i][j].c == 'J')
                {
                    xo = i; yo = j;
                    node[xo][yo].ans2 = 0;
                }
                if (node[i][j].c == 'F')
                {
                    k++;
                    x2 = i; y2 = j;
                    node[x2][y2].ans1 = 0;
                    now[k] = node[i][j];
                }
            }
            getchar();
        }
        bfs(k);

        for (i = 1; i <= r;i++)
        for (j = 1; j <= c; j++)
            leap[i][j] = 0;
        leap[xo][yo] = 1;
        p.push(node[xo][yo]);
        while (p.size())
        {
            temp = p.front();
            for (i = 0; i < 4; i++)
            {
                tempx = temp.x + dir[i][0];
                tempy = temp.y + dir[i][1];
                if (tempx >= 1 && tempx <= r&&tempy >= 1 && tempy <= c)
                {
                    if (!leap[tempx][tempy] && node[tempx][tempy].c != '#')
                    {
                        if (temp.ans2 + 1<node[tempx][tempy].ans1)
                        {
                            node[tempx][tempy].ans2 = temp.ans2 + 1;
                            leap[tempx][tempy] = 1;
                            p.push(node[tempx][tempy]);
                        }
                    }
                }
            }
            p.pop();
        }
        //for (i = 1; i <= r; i++)
        //{
        //  for (j = 1; j <= c; j++)
        //      cout << node[i][j].ans1 << " ";
        //  cout << endl;
        //}
        time = 1000007;
        for (i = 1; i <= c; i++)
        {
            if (node[1][i].c != '#'&&node[1][i].ans2>=0)
                time = min(time, node[1][i].ans2);
            if (node[r][i].c != '#'&&node[r][i].ans2 >= 0)
                time = min(time, node[r][i].ans2);
        }
        for (i = 1; i <= r; i++)
        {
            if (node[i][1].c != '#'&&node[i][1].ans2 >= 0)
                time = min(time, node[i][1].ans2);
            if (node[i][c].c != '#'&&node[i][c].ans2 >= 0)
                time = min(time, node[i][c].ans2);
        }
        if (time == 1000007)
            cout << "IMPOSSIBLE" << "\n";
        else
            cout << time + 1 << "\n";
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值