BFS-UVA-11624-Fire!

来源:互联网 发布:windows phone软件下载 编辑:程序博客网 时间:2024/05/17 03:52

Problem : 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 <= 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.

Sample Input

2
4 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

3
IMPOSSIBLE

一看到迷宫,然后又要按照时间找出路,那么考虑BFS,同时题中由于要考虑在行走中不能通过已经燃烧的地方,所以先让火入队,这样同时间的情况下都是火先出队然后更改地图为F,人就永远不会走到火里,走到边缘时直接可以输出结果,无路可走时就是IMPOSSIBLE了。

////  main.cpp//  简单搜索-J-Fire!////  Created by 袁子涵 on 15/8/13.//  Copyright (c) 2015年 袁子涵. All rights reserved.//#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;int T;char maze[1001][1001];bool visit[1001][1001];int R,C;int flag=1;unsigned long long int sum;int x;int y;typedef struct plz{    int x;    int y;    long long int step;    bool fire;}Plz;typedef struct queue{    long long int head,tail;    Plz data[2000010];}Queue;Queue q;void initqueue(){    q.tail=0;    q.head=0;}bool emptyqueue(){    if (q.head==q.tail) {        return 0;    }    return 1;}void bfs(){    Plz t,k;    t.x=x;    t.y=y;    t.step=0;    t.fire=0;    q.data[q.tail++]=t;    while (emptyqueue()) {        t=q.data[q.head++];        if (t.fire) {            //火出队            maze[t.x][t.y]='F';            //上下左右            if (t.x>0 && maze[t.x-1][t.y]!='#' && maze[t.x-1][t.y]!='F') {                k=t;                k.x--;                k.step++;                maze[k.x][k.y]='F';                q.data[q.tail++]=k;            }            if (t.x<R-1 && maze[t.x+1][t.y]!='#' && maze[t.x+1][t.y]!='F') {                k=t;                k.x++;                k.step++;                maze[k.x][k.y]='F';                q.data[q.tail++]=k;            }            if (t.y>0 && maze[t.x][t.y-1]!='#' && maze[t.x][t.y-1]!='F') {                k=t;                k.y--;                k.step++;                maze[k.x][k.y]='F';                q.data[q.tail++]=k;            }            if (t.y<C-1 && maze[t.x][t.y+1]!='#' && maze[t.x][t.y+1]!='F') {                k=t;                k.y++;                k.step++;                maze[k.x][k.y]='F';                q.data[q.tail++]=k;            }        }        else        {            //如果已经到边缘了            if (t.x==0 || t.y==0 || t.x==R-1 || t.y==C-1) {                sum=t.step+1;                flag=1;                return;            }            //上下左右            if (t.x>0 && maze[t.x-1][t.y]=='.' && visit[t.x-1][t.y]==0) {                k=t;                k.step++;                k.x--;                visit[k.x][k.y]=1;                q.data[q.tail++]=k;            }            if (t.x<R-1 && maze[t.x+1][t.y]=='.' && visit[t.x+1][t.y]==0) {                k=t;                k.step++;                k.x++;                visit[k.x][k.y]=1;                q.data[q.tail++]=k;            }            if (t.y>0 && maze[t.x][t.y-1]=='.' && visit[t.x][t.y-1]==0) {                k=t;                k.step++;                k.y--;                visit[k.x][k.y]=1;                q.data[q.tail++]=k;            }            if (t.y<C-1 && maze[t.x][t.y+1]=='.' && visit[t.x][t.y+1]==0) {                k=t;                k.step++;                k.y++;                visit[k.x][k.y]=1;                q.data[q.tail++]=k;            }        }    }}int main(int argc, const char * argv[]) {    cin >> T;    while (T--) {        cin >> R >> C;        memset(maze, 0, sizeof(maze));        memset(visit, 0, sizeof(visit));        flag=0;        Plz t;        initqueue();        for (int i=0; i<R; i++) {            for (int j=0; j<C; j++) {                cin >> maze[i][j];                if (maze[i][j]=='F') {                    t.x=i;                    t.y=j;                    t.step=0;                    t.fire=1;                    q.data[q.tail++]=t;                }                if (maze[i][j]=='J') {                    x=i;                    y=j;                    visit[i][j]=1;                }            }        }        bfs();        if (flag) {            cout << sum << endl;        }        else            cout << "IMPOSSIBLE" << endl;    }    return 0;}
0 0
原创粉丝点击