UVA 11624 搜索

给出1000*1000矩阵,含起点‘J’,路‘.‘,墙‘#’,火‘F‘;

火每秒蔓延一格,人每秒走一步

问人是否可以安全走出矩阵,不能被火碰到

先对所有火BFS一遍,记录每个点被烧到的时间

然后对人BFS一遍,若到每点前没被火烧即可走

#include "stdio.h"
#include "string.h"
#include "queue"
using namespace std;

const int dir[4][2]={ {1,0},{-1,0},{0,1},{0,-1} };
char str[1100][1100];
int n,m;
int fire[1100][1100],used[1100][1100];
struct node
{
    int x,y,t;
};

int judge_fire(int x,int y)
{
    if (x<0 || y<0 || x>=n || y>=m) return 0;
    if (used[x][y]==1) return 0;
    if (str[x][y]=='#') return 0;
    return 1;
}

void bfs_fire()
{
    queue<node>q;
    node cur,next;
    int i,j;
    memset(fire,-1,sizeof(fire));
    memset(used,0,sizeof(used));
    for (i=0;i<n;i++)
        for (j=0;j<m;j++)
        if (str[i][j]=='F')
        {
            cur.x=i;
            cur.y=j;
            cur.t=0;
            q.push(cur);
            used[i][j]=1;
            fire[i][j]=0;
        }
    while (!q.empty())
    {
        cur=q.front();
        q.pop();
        for (i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            if (judge_fire(next.x,next.y)==0) continue;
            next.t=cur.t+1;
            used[next.x][next.y]=1;
            fire[next.x][next.y]=next.t;
            q.push(next);
        }
    }
}

int judge_people(node b)
{
    if (str[b.x][b.y]=='#') return 0;
    if (used[b.x][b.y]==1) return 0;
    if (b.t>=fire[b.x][b.y] && fire[b.x][b.y]!=-1) return 0;
    return 1;
}

int bfs_people()
{
    queue<node>q;
    node cur,next;
    int i,j;
    memset(used,0,sizeof(used));
    for (i=0;i<n;i++)
    {
        for (j=0;j<m;j++)
        if(str[i][j]=='J')
        {
            cur.x=i;
            cur.y=j;
            cur.t=0;
            q.push(cur);
            used[i][j]=1;
            break;
        }
        if (j!=m) break;
    }

    while (!q.empty())
    {
        cur=q.front();
        q.pop();
        for (i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            next.t=cur.t+1;
            if (next.x<0 || next.y<0 || next.x>=n || next.y>=m) return next.t;
            if (judge_people(next)==0) continue;
            q.push(next);
            used[next.x][next.y]=1;
        }
    }
    return -1;
}
int main()
{
    int Case,i,ans;
    scanf("%d",&Case);
    while (Case--)
    {
        scanf("%d%d",&n,&m);
        for (i=0;i<n;i++)
            scanf("%s",str[i]);

        bfs_fire();
        ans=bfs_people();
        if (ans==-1)
            printf("IMPOSSIBLE\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}
时间: 2025-01-07 02:24:12

UVA 11624 搜索的相关文章

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 UVA 10047 两道用 BFS进行最短路搜索的题

很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; struct node{ int ft; int sta; }flo[1010][1010]; int vis[1010][1010]; st

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

UVA 11624 Fire!(广度优先搜索)

题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的bfs.但由于火到达的地方人不能抵达,故需先对火进行bfs,标记后若人在火烧到之前抵达即可.最后逃出时间需要加一, 因为当时只是抵达边界,若逃出时间需加一. #include <stdio.h> #include <queue> #include <string.h> #i

UVa 11624 Fire!(BFS 逃离火灾)

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

UVA 572 搜索 初识BFS DFS

题目类型: 搜索 样例输入: 1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0 样例输出: 0 1 2 2 分析: 这一题可以说是搜索中最基础的一题之一. 要找出相连在一起的有多少块, 因此, 依次枚举,遇到@时就进行搜索,用深搜,广搜都行,目的是把相连的@都标记为已访问. 下面给出用DFS(深搜)和BFS(广搜)的代码 DFS  1 :递推 #include <iostream> #i

UVa 11624 大火蔓延的迷宫

https://vjudge.net/problem/UVA-11624 题意:有一个大火蔓延的迷宫,迷宫中有障碍格,而所有着火的格子都会往四周蔓延.求出到达边界格子时的最短时间. 思路:复杂了一点的迷宫. 在bfs之前,我们首先需要计算出火势蔓延的情况,火势每次向四周蔓延一个格子,所以这也是一个最短路问题,也用一次bfs,计算出每个空白格子着火时的时间.这样,当我们第二次bfs去计算走出迷宫的时间时,对于每一个可走的格子,我们只需要判断在此时该格子是否着火,如果还未着火,则该格子是可以走的.