uva 11624 Fire!

A - Fire!
Time Limit:1000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu
Submit Status

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 (notdiagonally). The fire spreads all four directions from each square that is onfire. 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, separatedby spaces, with 1 <= R, C <= 1000. The following R lines of the test caseeach 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 beforethe fire reaches him, or an integer giving the earliest time Joe can safely exit themaze, in minutes.

Output for Sample Input

3
IMPOSSIBLE



代码:

#include
#include
#include
using namespace std;
const int maxx=1005;
int g[maxx][maxx];
bool visit[maxx][maxx];
int r,c;
int mintime;
int jx,jy,fx[1005],fy[1005];
int cnt;
struct node
{
    int x,y;
    int step;
} s,t;
void bfsF()
{
    queue p;
    struct node cur;
    for(int i=0;i=0&&g[xx-1][yy]==-1&&!visit[xx-1][yy])
        {
            visit[xx-1][yy]=true;
            s.x=xx-1;
            s.y=yy;
            s.step=cur.step+1;
            p.push(s);
        }
        if(xx+1=0&&g[xx][yy-1]==-1&&!visit[xx][yy-1])
        {
            visit[xx][yy-1]=true;
            s.x=xx;
            s.y=yy-1;
            s.step=cur.step+1;
            p.push(s);
        }
        if(yy+1 q;
    s.x=x;
    s.y=y;
    s.step=0;
    q.push(s);
    while(!q.empty())
    {
        struct node cur=q.front();
        q.pop();
        int xx=cur.x;
        int yy=cur.y;
        if(xx-1<0||xx+1>=r||yy-1<0||yy+1>=c)
        {
            return cur.step+1;
        }
        if((g[xx-1][yy]==-1||g[xx-1][yy]-cur.step>=2)&&!visit[xx-1][yy])
        {
            visit[xx-1][yy]=true;
            s.x=xx-1;
            s.y=yy;
            s.step=cur.step+1;
            q.push(s);
        }
        if((g[xx+1][yy]==-1||g[xx+1][yy]-cur.step>=2)&&!visit[xx+1][yy])
        {
            visit[xx+1][yy]=true;
            s.x=xx+1;
            s.y=yy;
            s.step=cur.step+1;
            q.push(s);
        }
        if((g[xx][yy-1]==-1||g[xx][yy-1]-cur.step>=2)&&!visit[xx][yy-1])
        {
            visit[xx][yy-1]=true;
            s.x=xx;
            s.y=yy-1;
            s.step=cur.step+1;
            q.push(s);
        }
        if((g[xx][yy+1]==-1||g[xx][yy+1]-cur.step>=2)&&!visit[xx][yy+1])
        {
            visit[xx][yy+1]=true;
            s.x=xx;
            s.y=yy+1;
            s.step=cur.step+1;
            q.push(s);
        }
    }
    return -1;
}
int main()
{
    int T;
    char ch;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&r,&c);
        getchar();
        cnt=0;
        for(int i=0; i


你可能感兴趣的:(图论基础)