UVa 11624 Fire! bfs

http://vjudge.net/contest/view.action?cid=48251#problem/A

Description

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

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
题目大意:你的任务是帮助joe走出一个大火蔓延的迷宫,joe每分钟可以走到上下左右四个方向的相邻格之一,而所有着火的格子都会向四周蔓延(即如果走到某个空格与着火的格有公共边则下一分钟这个空格将着火)。迷宫中有一些障碍格,joe和火都无法进入。当joe走到一个迷宫的边界格子时,我们认为他已经走出了迷宫。

大体思路:由于初始时刻有火的空格不只有一个,我们只需要预处理每个格子起火的时间,在bfs扩展节点的时候加一个判断,当达到新节点时该格子没有着火才真的吧这个新节点加到队列中。最后bfs起点开始搜索,当Joe到达空格的时间比火到达的时间早时,则Joe可以进入这个节点。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
const int N=1033;
const int INF=99999999;
struct note
{
    int x,y,tep;
    note(){}
    note(int a,int b ,int c)
    {
        x=a;
        y=b;
        tep=c;
    }
};
int n,m;
char a[N][N];
int num[N][N];
int ok(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m)
        return 0;
    return 1;
}
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
int sx,sy;
void init()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        cin >> a[i];
    bool b[N][N];
    memset(b,0,sizeof(b));
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            num[i][j]=INF;
    queue <note> q;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(a[i][j]=='F')
            {
                b[i][j]=1;
                num[i][j]=0;
                q.push(note(i,j,0));
            }
            if(a[i][j]=='J')
                sx=i,sy=j;
        }
    while(!q.empty())
    {
        note c=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int x=c.x+dx[i];
            int y=c.y+dy[i];
            if(ok(x,y)&&num[x][y]==INF&&a[x][y]=='.')
            {
                num[x][y]=c.tep+1;
                q.push(note(x,y,c.tep+1));
            }
        }
    }
}
void bfs()
{
    queue<note>p;
    p.push(note(sx,sy,0));
    bool b[N][N];
    memset(b,0,sizeof(b));
    b[sx][sy]=1;
    while(!p.empty())
    {
        note c=p.front();
        p.pop();
        if(!ok(c.x,c.y))//考虑刚开始Joe就在迷宫边缘的情况
        {
            printf("%d\n",c.tep);
            return;
        }
        for(int i=0;i<4;i++)
        {
            int x=c.x+dx[i];
            int y=c.y+dy[i];
            if(!ok(x,y))
            {
                printf("%d\n",c.tep+1);
                return;
            }
            if(num[x][y]>c.tep+1&&b[x][y]==0&&a[x][y]=='.')
            {
                b[x][y]=1;
                p.push(note(x,y,c.tep+1));
            }
        }
    }
    puts("IMPOSSIBLE");
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        bfs();
    }
}


你可能感兴趣的:(UVa 11624 Fire! bfs)