UVA11624 Fire! BFS

最短路处理每个点的起火时间。然后BFS

如果一个点起火时间大于当前的行走时间,那么该点入队。



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

[]   [Go Back]   [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 (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

Malcolm Sharpe, Ondřej Lhoták


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

using namespace std;

#define INF 0x3FFFFF

struct node
{
    int x,y;
    int t;
};
bool vis[1100][1100];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char g[1100][1100];
int times[1100][1100],r,c,sx,sy;

void spfa()
{
    node a,k;
	queue<node> q;
	for(int i=0;i<r;i++)
        for(int j=0;j<c;j++)
        {
           if(times[i][j]==0)
           {
               a.x=i,a.y=j;
               q.push(a);
               vis[i][j]=1;
           }
           else
            vis[i][j]=0;
        }
	while(!q.empty())
	{
		a=q.front();
		vis[a.x][a.y]=0;
		q.pop();
		for(int i=0;i<4;i++)
        {
            int x=a.x+dir[i][0];
            int y=a.y+dir[i][1];
            if(x<0||x>=r||y<0||y>=c) continue;
            if(times[a.x][a.y]+1<times[x][y])
            {
                times[x][y]=times[a.x][a.y]+1;
                if(!vis[x][y])
                {
                    k.x=x;
                    k.y=y;
                    q.push(k);
                    vis[x][y]=1;
                }
            }
        }
	}
}

bool jud(node x)
{
  if(x.t>=times[x.x][x.y] || g[x.x][x.y]==0)
        return false;
    return true;
}

int bfs()
{
    queue<node> q;
    node a,k;
    a.x=sx,a.y=sy,a.t=0;
    g[sx][sy]=0;
    q.push(a);
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            k.x=a.x+dir[i][0];
            k.y=a.y+dir[i][1];
            k.t=a.t+1;
            if(k.x<0||k.x>=r||k.y<0||k.y>=c)
                return k.t;
            if(jud(k))
            {
                q.push(k);
                g[k.x][k.y]=0;
            }
        }
    }
    return -1;
}

int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&r,&c);
        gets(g[0]);
        for(int i=0;i<r;i++)
            gets(g[i]);
        for(int i=0;i<r;i++)
            for(int j=0;j<c;j++)
            {
                if(g[i][j]=='#')
                    times[i][j]=-1;
                if(g[i][j]=='.')
                    times[i][j]=INF;
                if(g[i][j]=='J')
                {
                    times[i][j]=INF;
                    sx=i;
                    sy=j;
                }
                if(g[i][j]=='F')
                    times[i][j]=0;
            }
        spfa();
        int ans=bfs();
        if(ans==-1)
            printf("IMPOSSIBLE\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}


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

[]   [Go Back]   [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 (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

Malcolm Sharpe, Ondřej Lhoták
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值