首页 > 代码库 > uva 11624 Fire!(BFS)

uva 11624 Fire!(BFS)

Fire!
Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Submit Status

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


题意:
有多个火源F,J和F一次只能向上下左右移动一格,问J逃出的最小时间,不能逃出则输出“IMPOSSIBLE”。
题解:
BFS,火源先入队列,并标记为有火,然后J入队,标记无火,到边缘计科逃出。
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos ( -1.0 );
const double E = 2.718281828;
typedef long long ll;

const int INF = 1000010;

using namespace std;

int n,m;
bool vis[1010][1010];
char mp[1010][1010];
int xx[4]= {-1,0,1,0};
int yy[4]= {0,1,0,-1};
int Fx,Fy,Jx,Jy;
int flag;
int len;

struct node
{
    int x,y;
    int num;
    int fire;
};
node q[10000];
queue<node>que;

void bfs()
{
    while(que.size())
        que.pop();
    memset(vis,0,sizeof vis);
    node a,t;
    for(int i=0; i<len; i++)
    {
        que.push(q[i]);
        vis[q[i].x][q[i].y]=1;
    }
    a.x=Jx,a.y=Jy,a.num=0,a.fire=0;
    que.push(a);
    vis[Jx][Jy]=1;
    while(que.size())
    {
        a=que.front();
        que.pop();
        if((a.x==1||a.x==n||a.y==1||a.y==m)&&a.fire==0)
        {
            cout<<a.num+1<<endl;
            flag=0;
            return;
        }
        for(int i=0; i<4; i++)
        {
            t.x=xx[i]+a.x;
            t.y=yy[i]+a.y;
            if(t.x>=1&&t.x<=n&&t.y>=1&&t.y<=m&&!vis[t.x][t.y]&&mp[t.x][t.y]!='#')
            {
                t.num=a.num+1;
                t.fire=a.fire;
                que.push(t);
                vis[t.x][t.y]=1;
            }
        }
    }
}

int main()
{
    int T;
    cin>>T;
    {
        while(T--)
        {
            scanf("%d%d",&n,&m);
            getchar();
            len=0;
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    scanf("%c",&mp[i][j]);
                    if(mp[i][j]=='J')
                    {
                        Jx=i,Jy=j;
                    }
                    if(mp[i][j]=='F')
                    {
                        q[len].x=i,q[len].y=j,q[len].num=0,q[len++].fire=1;
                    }
                }
                getchar();
            }
            flag=1;
            bfs();
            if(flag)
                cout<<"IMPOSSIBLE\n";
        }
    }
    return 0;
}


uva 11624 Fire!(BFS)