Fire! UVA - 11624 (两步bfs)
题意
人要从迷宫走出去,火会向四个方向同时扩散
分析
两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能在某地当且仅当所到时间小于火到达时间
代码
#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/65481630, https://blog.csdn.net/ko_tin/article/details/52947059
Fire! UVA - 11624 (两步bfs)的更多相关文章
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
		E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ... 
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
		很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ... 
- J - Fire!---UVA 11624
		题目链接 题意:J代表Joe的位置,F代表火的起点,下一刻火将会向四周扩散,求Joe逃离的最短时间,如果不能逃离输出IMPOSSIBLE; 注意火的起点可能不止一处 可以用两次bfs分别求出人到达某个 ... 
- kuangbin专题 专题一 简单搜索 Fire! UVA - 11624
		题目链接:https://vjudge.net/problem/UVA-11624 题意:一个迷宫,可能有一个或者多个地方着火了,每过1个时间消耗,火会向四周蔓延,问Joe能不能逃出迷宫,只要走出迷宫 ... 
- Fire uva 11624
		题目连接:http://acm.hust.edu.cn/vjudge/problem/28833 /* 首先对整个图bfs一次得到火焰燃烧的时刻表 之后在bfs搜路径时加一个火烧表的判断 坑点在于:如 ... 
- UVA - 11624 Fire!  bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
		UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ... 
- UVA 11624  Fire!(两次BFS+记录最小着火时间)
		题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ... 
- UVa 11624,两次BFS
		题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ... 
- BFS(两点搜索) UVA 11624 Fire!
		题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ... 
随机推荐
- 机器学习: DeepDreaming with TensorFlow (一)
			在TensorFlow 的官网上,有一个很有趣的教程,就是用 TensorFlow 以及训练好的深度卷积神经(GoogleNet)网络去生成一些有趣的pattern,通过这些pattern,可以更加深 ... 
- EPPlus简单使用
			在使用之前需要在项目中添加对EEPULS.dll的引用 1,创建excel 2,创建sheet 3,添加内容 4,修改 5,保存 FileInfo newFile = new FileInfo(fil ... 
- js调查server
			<script type="text/javascript"> function showUnreadNews() { $(document).ready(functi ... 
- C/S和B/S两种架构区别与优缺点分析
			C/S和B/S,是再普通不过的两种软件架构方式,都可以进行同样的业务处理,甚至也可以用相同的方式实现共同的逻辑.既然如此,为何还要区分彼此呢?那我们就来看看二者的区别和联系. 一.C/S 架构 1. ... 
- 利用属性中设置、查看DataContext Command等
			DataContext 1 2 3 示例 1 2 xaml代码自动生成 3 
- strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l, setlocale(LC_CTYPE, "Japanese_Japan")(MSDN的官方示例)
			// crt_strlen.c // Determine the length of a string. For the multi-byte character // example to work ... 
- C#开发奇技淫巧一:调试windows系统服务
			原文:C#开发奇技淫巧一:调试windows系统服务 windows系统服务不能直接运行,只能在安装完服务之后启动.暂停.继续.停止服务,导致服务的调试不能使用一般的断点调试. 要调试系统服务,可以采 ... 
- WPF中类似使用tab键功能,可以向上向下定位
			原文:WPF中类似使用tab键功能,可以向上向下定位 private void tbYyrs_KeyUp(object sender, KeyEventArgs e) { UIElement elem ... 
- python3. 报“ImportError: No module named ‘MySQLdb'”
			需要安装PyMysql pip install PyMysql 然后在程序中引入 import pymysql pymysql.install_as_MySQLdb() app.config['SQL ... 
- 图像滤镜艺术---微软自拍APP滤镜实现合集DEMO
			原文:图像滤镜艺术---微软自拍APP滤镜实现合集DEMO 微软最近推出了自家的美颜app,大家有兴趣可以在苹果商店中下载一下看一看,今天,我们要说的便是这款app中的所有滤镜实现问题. 这款app中 ... 
