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;
}
时间: 2024-08-03 03:16:52

uva 11624 Fire!(BFS)的相关文章

UVA - 439 - Knight Moves (BFS)

UVA - 439 Knight Moves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knigh

uva 10651 Pebble Solitaire (BFS)

uva 10651 Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board with an arrangement of small cavities, initially all but one occupied by a pebble each. The aim of the game is to remove as many pebbles as

FZU 2150 Fire Game(BFS)

Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is cons

uva 532 Dungeon Master(BFS)

uva 532 Dungeon Master You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. Y

UVA 10047 - The Monocycle(BFS)

题目链接:点击打开链接 题意:从起点到终点,每秒可以选择前进.向左.向右转, 每前进一格轮子转到下一个颜色, 一共5中颜色, 开始的时候绿色接触地面,朝北, 要求最后也绿色接触地面,求能否到达目标点以及最短时间. 思路:和普通BFS相比,多了两个附加条件,所以要将状态表示全面,也要对应加两维. 水题. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream&g

UVA - 10047 The Monocycle (BFS)

题目大意:有一个n*m的网格,网格上面有的地方有障碍物 现在有一个人,骑着独轮车,要求从一个地方到达另一个地方,骑独轮车时,只能直走,或者左拐,右拐,不能向后走 独轮车的轮子被分成了5部分,每部分都有对应的颜色,刚开始时是绿色向下,当经过一个格子时,颜色就会变换 问从起点出发到终点,到终点时独轮车的绿色颜色向下,需要多久 解题思路:暴力BFS #include <cstdio> #include <cstring> #include <algorithm> #inclu

UVa 11624 Fire!(着火了!)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New Roman"; font-size: 10.5000pt } h3 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 1

uva 11624 Fire!(多源BFS)

uva 11624 Fire! 题目大意:J在迷宫里工作,有一天迷宫起火了,火源有多处.每过一秒,火源都会向四个方向蔓延,J也会向四个方向移动,问J能不能跑出去,能的话输出他跑出去的最短时间,否则输出"IMPOSSIBLE" 解题思路:先进行一次BFS,找出每一点火焰蔓延到该处的最短时间.然后根据这张"火势图",对J的行进路线进行BFS.注意J在边缘的时候,以及没有火源的时候. #include <cstdio> #include <cstring

UVA 11624 - Fire!(BFS)

UVA 11624 - Fire! 题目链接 题意:一个迷宫,一些格子着火了,火每秒向周围蔓延,现在J在一个位置,问他能走出迷宫的最小距离 思路:BFS2次,第一次预处理每个位置着火时间,第二次根据这个再BFS一次 代码: #include <cstdio> #include <cstring> #include <queue> using namespace std; const int d[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; co