前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Fire! UVA - 11624 (两步bfs)

Fire! UVA - 11624 (两步bfs)

作者头像
_DIY
发布2019-09-11 17:25:54
4140
发布2019-09-11 17:25:54
举报

题目链接

题意

人要从迷宫走出去,火会向四个方向同时扩散

分析

两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能在某地当且仅当所到时间小于火到达时间

代码

代码语言:javascript
复制
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 1000+10;
const int INF = 0x3f3f3f3f;
int R, C, tcases;
int begx, begy;
char maze[maxn][maxn];
int fire_time[maxn][maxn];
int per_time[maxn][maxn];
int vis[maxn][maxn];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
typedef pair<int, int> P;
queue<P> pos;
bool op(int a, int b)
{
    if(a >= 0 && a < R && b >= 0 && b < C)
        return true;
    return false;
}
void init()
{
    while(!pos.empty())
        pos.pop();
    memset(vis, 0, sizeof(vis));
    bool is_fire = false;       //看是否有火
    for(int i = 0; i < R; i++)
    {
        for(int j = 0; j < C; j++)
        {
            per_time[i][j] = INF;
            fire_time[i][j] = INF;
            if(maze[i][j] == 'J')
            {
                begx = i;
                begy = j;
                per_time[i][j] = 0;
            }
            if(maze[i][j] == 'F')
            {
                fire_time[i][j] = 0;
                vis[i][j] = 1;
                is_fire = true;
                pos.push(P(i, j));      //将初始时F的位置插入到队列中
            }
        }
    }
    if(!is_fire)
        return ;

    while(!pos.empty())
    {
        P p = pos.front();
        pos.pop();  //该点周围已遍历,故删掉
        for(int i = 0; i < 4; i++)
        {
            int curx = p.first + dx[i], cury = p.second + dy[i];
            if(op(curx, cury) && !vis[curx][cury] && maze[curx][cury] != '#')
            {
                vis[curx][cury] = 1;
                fire_time[curx][cury] = fire_time[p.first][p.second] + 1;
                pos.push(P(curx, cury));
            }
        }
    }
}
int bfs()
{
    while(!pos.empty())
        pos.pop();
    memset(vis, 0, sizeof(vis));
    pos.push(P(begx, begy));    //把J的初始位置插入
    vis[begx][begy] = 1;
    while(!pos.empty())
    {
        P p = pos.front();
        pos.pop();
        for(int i = 0; i < 4; i++)
        {
            int curx = p.first + dx[i], cury = p.second + dy[i];
            if(!op(curx, cury))
                return per_time[p.first][p.second] + 1;
            if(!vis[curx][cury] && maze[curx][cury] == '.' && per_time[p.first][p.second] + 1 < fire_time[curx][cury])   //人先fire一步到达
            {
                vis[curx][cury] = 1;
                pos.push(P(curx, cury));
                per_time[curx][cury] = per_time[p.first][p.second] + 1;
            }
        }
    }
    return INF;

}
int main()
{
    cin >> tcases;
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while(tcases--)
    {
        cin >> R >> C;
        for(int i = 0; i < R; i++)
            scanf("%s", maze[i]);
        init(); //探寻F到达各个点时的时间,用bfs
        int ans = bfs();
        if(ans != INF)
            cout << ans << endl;
        else
            cout << "IMPOSSIBLE" << endl;
    }
}

注意:一般 freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);这两句不要出现在代码里。

memset不要想当然的认为它可以对int数组赋任意值,实际上它只对-1,0有效,原因见https://blog.csdn.net/lyj2014211626/article/details/65481630https://blog.csdn.net/ko_tin/article/details/52947059

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • 分析
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档