UVA 11624 Fire!

混合的BFS。。。

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

[]   [Go Back]   [Status]  

Description

UVA 11624 Fire!

Problem B: Fire!

UVA 11624 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

Malcolm Sharpe, Ond?ej Lhoták

[]   [Go Back]   [Status]  

UVA 11624 Fire!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

int dir_x[4]={1,-1,0,0};
int dir_y[4]={0,0,1,-1};

struct node
{
    int x,y,f,t;
};

char MP[1200][1200];
bool vis[1200][1200];
int T,n,m;
bool isINSIDE(node a)
{
    if((a.x>=0&&a.x<n)&&(a.y>=0&&a.y<m)) return true;
    return false;
}

int main()
{
    scanf("%d",&T);
while(T--)
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%s",MP+i);
    queue<node> q;
    node Joe;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(MP[i][j]==‘#‘)
            {
                vis[i][j]=1;
            }
            else if(MP[i][j]==‘F‘)
            {
                vis[i][j]=1;
                q.push((node){i,j,1,0});
            }
            else if(MP[i][j]==‘J‘)
            {
                vis[i][j]=1;
                Joe.x=i;Joe.y=j;Joe.f=0;Joe.t=0;
            }
        }
    }
    q.push(Joe);
    int ans=-1;
    while(!q.empty())
    {
        node u,v;
        u=q.front(); q.pop();
        for(int i=0;i<4;i++)
        {
            v=u;
            v.t++;
            v.x+=dir_x[i],v.y+=dir_y[i];
            if(isINSIDE(v))
            {
                if(vis[v.x][v.y]) continue;
                vis[v.x][v.y]=1;
                q.push(v);
            }
            else if(v.f==0)
            {
                ans=v.t;
                break;
            }
        }
        if(ans!=-1) break;
    }

    if(ans==-1) puts("IMPOSSIBLE");
    else printf("%d\n",ans);
}
    return 0;
}





UVA 11624 Fire!

上一篇:[开心IT面试题] 求字符串长度


下一篇:poj 2391 Ombrophobic Bovines 二分+拆点建图+最大流