UVA 11624.Fire(广搜)

来源:互联网 发布:lnmp一键 安装php扩展 编辑:程序博客网 时间:2024/06/02 01:05

一道刘汝佳的大白书例题,这个月就搞这本书的第5章了。

Description

Download as PDF

Problem B: 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 Specification

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

Sample Input

24 4#####JF##..##..#3 3####J.#.F

Output Specification

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.

Output for Sample Input

3IMPOSSIBLE

Malcolm Sharpe, Ondřej Lhoták

题意书上写的比较明确,火每分钟会向上下左右各蔓延一个格,人每分钟只能向上下左右各蔓延一格。求他能跑到边界的最短时间,如果不能跑出去,输出IMPOSSIBLE。

如书中所说,先遍历某地被火烧后的位置,记录下时间,然后让人从起点开始跑,如果到达某地点的时间>=火烧的时间,则不能跑到此点。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace std;#define INF 1e9int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};struct node {    int x, y, step;    node(int x, int y, int step):x(x), y(y), step(step) {}    //构造函数,初始化匿名,作用仅仅是使代码简洁};char map[1010][1010];int R, C, t, sx, sy;int vis[1010][1010], times[1010][1010];int BFS() {    queue<node> q;    map[sx][sy] = 0;//作用相当于一个vis数组,记录人走的位置不重复    q.push(node(sx, sy, 0));    while(!q.empty()) {        node a = q.front(); q.pop();        for(int i = 0; i < 4; i++) {            int nx = a.x + dir[i][0], ny = a.y + dir[i][1], nt = a.step + 1;            if(nx < 0 || nx >= R || ny < 0 || ny >= C) return nt;            if(nt < times[nx][ny] && map[nx][ny] != 0) {                q.push(node(nx, ny, nt));                map[nx][ny] = 0;            }        }    }    return -1;}int main() {    scanf("%d", &t);    while(t--) {        queue<node> Fq;        scanf("%d%d", &R, &C);        memset(vis, 0, sizeof(vis));        for(int i = 0; i < R; i++) {            scanf("%s", map[i]);            for(int j = 0; j < C; j++) {                if(map[i][j] == 'J') {sx = i, sy = j, times[i][j] = INF;}                else if(map[i][j] == '.') times[i][j] = INF;                else if(map[i][j] == '#') times[i][j] = -1;                else if(map[i][j] == 'F') {times[i][j] = 0; Fq.push(node(i, j, 0)); vis[i][j] = 1;}            }        }        while(!Fq.empty()) {//遍历某地被火烧后的位置,记录下时间            node a = Fq.front(); Fq.pop();            for(int i = 0; i < 4; i++) {                int nx = a.x + dir[i][0], ny = a.y + dir[i][1];                if(nx >= 0 && nx < R && ny >= 0 && ny < C && times[a.x][a.y] + 1 < times[nx][ny]) {                    times[nx][ny] = a.step + 1;                    if(!vis[nx][ny]) {Fq.push(node(nx, ny, a.step + 1)); vis[nx][ny] = 1;}                }            }        }        int ans = BFS();        if(ans == -1) printf("IMPOSSIBLE\n");        else printf("%d\n",ans);    }    return 0;}


0 0