(简单) UVA 11624 Fire! ,BFS。

  Description

  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.

  题目就是迷宫问题,然后加了火,不过这个题被坑了,原来火不只是一个,可以有很多个。。。

  这个题先从火BFS一遍,这样就可以知道火多长时间后烧到某一块,然后从人BFS,当走到这时火已经烧过来的话就算不能通过就好了。

代码如下:

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

using namespace std;

bool map1[1005][1005];
int rem[1005][1005];
int Frem[1005][1005];
int N,M;
int Si,Sj,Fi[1000006],Fj[1000006];
int cou;

bool judge(int x,int y,int temp)
{
    if(x<=0||y<=0||x>N||y>M)
        return 0;

    if(!map1[x][y])
        return 0;

    if(rem[x][y])
        return 0;

    if(Frem[x][y]>-1&&temp>=Frem[x][y])
        return 0;

    return 1;
}

bool judge1(int x,int y)
{
    if(x<=0||y<=0||x>N||y>M)
        return 0;

    if(!map1[x][y])
        return 0;

    if(Frem[x][y]!=-1)
        return 0;

    return 1;
}

int slove()
{
    queue <int> que;
    int temp,t1,t2;

    que.push(Si*1010+Sj);
    rem[Si][Sj]=1;

    while(!que.empty())
    {
        temp=que.front();
        que.pop();

        t1=temp/1010;
        t2=temp%1010;
        temp=rem[t1][t2];

        if(t1==1||t2==1||t1==N||t2==M)
            return temp;

        --t1;
        if(judge(t1,t2,temp))
        {
            que.push(t1*1010+t2);
            rem[t1][t2]=temp+1;
        }
        t1+=2;
        if(judge(t1,t2,temp))
        {
            que.push(t1*1010+t2);
            rem[t1][t2]=temp+1;
        }
        --t1;
        --t2;
        if(judge(t1,t2,temp))
        {
            que.push(t1*1010+t2);
            rem[t1][t2]=temp+1;
        }
        t2+=2;
        if(judge(t1,t2,temp))
        {
            que.push(t1*1010+t2);
            rem[t1][t2]=temp+1;
        }
    }

    return -1;
}

void init()
{
    queue <int> que;
    int temp,t1,t2;
    for(int i=0;i<cou;++i)
    {
        que.push(Fi[i]*1010+Fj[i]);
        Frem[Fi[i]][Fj[i]]=0;
    }

    while(!que.empty())
    {
        temp=que.front();
        que.pop();

        t1=temp/1010;
        t2=temp%1010;
        temp=Frem[t1][t2];

        --t1;
        if(judge1(t1,t2))
        {
            que.push(t1*1010+t2);
            Frem[t1][t2]=temp+1;
        }
        t1+=2;
        if(judge1(t1,t2))
        {
            que.push(t1*1010+t2);
            Frem[t1][t2]=temp+1;
        }
        --t1;
        --t2;
        if(judge1(t1,t2))
        {
            que.push(t1*1010+t2);
            Frem[t1][t2]=temp+1;
        }
        t2+=2;
        if(judge1(t1,t2))
        {
            que.push(t1*1010+t2);
            Frem[t1][t2]=temp+1;
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);

    int T;
    int ans;
    char c;
    cin>>T;

    while(T--)
    {
        cou=0;
        cin>>N>>M;
        for(int i=1;i<=N;++i)
            for(int j=1;j<=M;++j)
            {
                rem[i][j]=0;
                Frem[i][j]=-1;
                cin>>c;
                if(c==‘#‘||c==‘F‘)
                    map1[i][j]=0;
                else
                    map1[i][j]=1;

                if(c==‘J‘)
                    Si=i,Sj=j;
                if(c==‘F‘)
                {
                    Fi[cou]=i;
                    Fj[cou++]=j;
                }
            }

        init();

        ans=slove();

        if(ans==-1)
            cout<<"IMPOSSIBLE\n";
        else
            cout<<ans<<endl;
    }

    return 0;
}

时间: 2024-10-26 21:02:02

(简单) UVA 11624 Fire! ,BFS。的相关文章

UVa 11624 Fire!(BFS 逃离火灾)

题意   n*m的迷宫中有一些着火点  每个着火点上下左右相邻的非墙点下一秒也将成为一个着火点  Joe每秒能向相邻的点移动一步  给你所有着火点的位置和Joe的位置  问Joe逃离这个迷宫所需的最小时间 可以先一遍bfs把每个点的最早着火时间存起来   只有Joe到达该点的时间小于这个时间Joe才能走这个点   只需要对Joe所在的点为起点再来一次bfs就行了   需要注意的是开始可能有多个着火点  我开始以为只有一个着火点被坑了好久 v[i][j]第一遍bfs记录点i,j的最早着火时间  第

uva 11624 Fire!(多源BFS)

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

BFS(两点搜索) UVA 11624 Fire!

题目传送门 1 /* 2 BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-4 8:11:54 7 File Name :UVA_11624.cpp 8 ****************************************

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

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的应用)

A - Fire! Time Limit:1000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu SubmitStatus 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

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 f

BFS [uva 11624] Fire!

J - Fire! Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu 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

uva 11624 Fire! 【 BFS 】

按白书上说的,先用一次bfs,求出每个点起火的时间 再bfs一次求出是否能够走出迷宫 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 #include<vector> 7 using namespace std; 8 9 const int maxn = 1005; 10 cons