您的位置:首页 > 其它

Uva 11624-Fire!

2015-07-25 16:26 405 查看
题目描述:

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.
输入格式:
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.

输出格式:

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.

样例出入:

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

样例输出:

3
IMPOSSIBLE

思路分析:

一开始想到先扩展一层所有的F,然后再扩展J能走的格子,F的扩展条件漏泄了F经过J的情况,WA一次,改掉这个BUG之后,还是WA,经过查找UVA的数据,知道了开始的程序对J和K的扩展速度没控制好,导致错误。正确的情况应该是扩展一层F,再扩展一层J,结果我写成了扩展几层F才扩展一层C,导致F的扩展速度比J快了不少。解决方法是设置一个J的扩展层节点计数变量num,当num为0时,表示当前层的J扩展完毕。另外对判定边界的方法还可以改进,比如可以先初始化MAPT为-1然后,从(1,1)开始才是真正的MAZE,当走到值为-1的点的时候,即已经走出了边界,另外Vis数组可以完全代替dist数组的功能。

源代码如下:

//注意控制F和J的扩展速度
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define N 1000 + 10

struct Node {
    int x, y;
};
char MAPT

;
int R, C, vis

, dist

;
int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};
queue<Node> f;

void Fire() {
    int i, num = f.size();
    Node now, pre;
    while (num--) {
        pre = f.front(); f.pop();
        for (i = 0; i < 4; i++) {
            now.x = pre.x + dir[i][0];
            now.y = pre.y + dir[i][1];
            if (now.x >= 0 && now.x < R && now.y >= 0 && now.y < C && (MAPT[now.x][now.y] == '.' || MAPT[now.x][now.y] == 'J')) {
                f.push(now);
                MAPT[now.x][now.y] = 'F';
            }
        }
    }
}
int BFS(int xx, int yy) {
    queue<Node> q;
    Node now, pre;
    int i, j;
    now.x = xx; now.y = yy;
    q.push(now);
    vis[now.x][now.y] = 1;
    while (!q.empty()) {
        int num = q.size();
        Fire();
        while (num--) {
            pre = q.front(); q.pop();
            for (i = 0; i < 4; i++) {
                now.x = pre.x + dir[i][0];
                now.y = pre.y + dir[i][1];
                if (now.x < 0 || now.x >= R || now.y < 0 || now.y >= C) {
                    return dist[pre.x][pre.y] + 1;
                }
                if (!vis[now.x][now.y] && MAPT[now.x][now.y] == '.') {
                    q.push(now);
                    vis[now.x][now.y] = 1;
                    dist[now.x][now.y] = dist[pre.x][pre.y] + 1;
                }
            }
        }
    }
    return -1;
}
int main() {
    //FILE *p = freopen("test.txt", "r", stdin);
    int T, i, j;
    while (scanf("%d", &T) != EOF) {
        while (T--) {
            scanf("%d%d", &R, &C);
            for (i = 0; i < R; i++) {
                scanf("%s", MAPT[i]);
            }
            memset(vis, 0, sizeof(vis));
            memset(dist, 0, sizeof(dist));
            int sx = 0, sy = 0;
            for (i = 0; i < R; i++) {
                for (j = 0; j < C; j++) {
                    if (MAPT[i][j] == 'J') {
                        sx = i; sy = j; vis[i][j] = 1;
                    }
                    if (MAPT[i][j] == 'F') {
                        Node temp = {i, j};
                        f.push(temp);
                    }
                }
            }
            int ans = BFS(sx, sy);
            if (ans >= 0) printf("%d\n", ans);
            else printf("IMPOSSIBLE\n");
            while (!f.empty()) f.pop();
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: