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

题意:有一个迷宫,那么又是逃离了,现在知道Joe的初始位置,也知道火种的初始位置,Joe和火种是可以同时移动的(上下左右),这次要逃离的标准是Joe比火种先到边界,任何一个边界都可以,就算逃离成功,那么我们只需要分别统计Joe和火种达到每一点需要的步数(时间),然后在边界位置进行比较就可以了,两个BFS就行了(关键是火种可能不止一处有,也可能一处都没有)。

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

const int N=1100;
const int INF=0x3f3f3f3f;

struct node
{
    int x, y;
}no[N]; ///no结构体记录每个火种的初始位置
char Map[N][N];
int k, m, n, X, Y, visJ[N][N], visF[N][N]; ///k表示火种的个数,X和Y记录Joe的初始位置,visJ数组保存Joe到达每一点需要的时间,visF数组保存火种到达每一点需要的时间
int dir[4][2] = { {1,0}, {-1,0}, {0,1}, {0,-1} };

void Init()
{
    int i, j;

    for (i = 0; i <= m; i++)
    {
        for (j = 0; j <= n; j++)
        {
            visJ[i][j] = 0;
            visF[i][j] = INF; ///这里如果和visJ一样初始化为0,那么当没有火种时,visJ显然是不能小于visF的,那不就不能逃离了,其实这种情况下只要边界不是'#',就可以逃离,所以初始化为INF
        }
    }

    k = 0;
}

void BFSF() ///计算火种到达每一点的时间
{
    int i, x, y;
    node next, now;
    queue<node>Q;

    for (i = 0; i < k; i++)
    {
        now.x = no[i].x; now.y = no[i].y;
        visF[now.x][now.y] = 1;
        Q.push(now);
    }

    while (!Q.empty())
    {
        now = Q.front(); Q.pop();

        for (i = 0; i < 4; i++)
        {
            next.x = x = now.x + dir[i][0];
            next.y = y = now.y + dir[i][1];

            if (x >= 0 && x < m && y >= 0 && y < n && Map[x][y] != '#' && visF[x][y] == INF)
            {
                visF[x][y] = visF[now.x][now.y] + 1;
                Q.push(next);
            }
        }
    }
}

int BFSJ() ///计算Joe到达每一点的时间,并在边界与火种比较
{
    int i, x, y;
    node next, now;
    queue<node>Q;

    now.x = X; now.y = Y;
    Q.push(now);
    visJ[X][Y] = 1;

    while (!Q.empty())
    {
        now = Q.front(); Q.pop();

        if ((now.x == 0 || now.x == m-1 || now.y == 0 || now.y == n-1) && visJ[now.x][now.y] < visF[now.x][now.y])
            return visJ[now.x][now.y];

        for (i = 0; i < 4; i++)
        {
            next.x = x = now.x + dir[i][0];
            next.y = y = now.y + dir[i][1];
            if (x >= 0 && x < m && y >= 0 && y < n && Map[x][y] != '#' && !visJ[x][y])
            {
                visJ[x][y] = visJ[now.x][now.y] + 1;
                Q.push(next);
            }
        }
    }

    return -1;
}

int main ()
{
    int T, i, j, ans;

    scanf("%d", &T);

    while (T--)
    {
        scanf("%d%d", &m, &n);
        for (i = 0; i < m; i++)
            scanf("%s", Map[i]);

        Init();

        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                if (Map[i][j] == 'J')
                {
                    X = i;
                    Y = j;
                }
                else if (Map[i][j] == 'F') ///火种可能不止一处
                {
                    no[k].x = i;
                    no[k++].y = j;
                }
            }
        }

        BFSF();
        ans = BFSJ();

        if (ans == -1) printf("IMPOSSIBLE\n");
        else printf("%d\n", ans);
    }

    return 0;
}
posted @ 2015-09-26 10:54  搁浅の记忆  阅读(134)  评论(0编辑  收藏  举报