UVa 11624 大火蔓延的迷宫
https://vjudge.net/problem/UVA-11624
题意:
有一个大火蔓延的迷宫,迷宫中有障碍格,而所有着火的格子都会往四周蔓延。求出到达边界格子时的最短时间。
思路:
复杂了一点的迷宫。
在bfs之前,我们首先需要计算出火势蔓延的情况,火势每次向四周蔓延一个格子,所以这也是一个最短路问题,也用一次bfs,计算出每个空白格子着火时的时间。这样,当我们第二次bfs去计算走出迷宫的时间时,对于每一个可走的格子,我们只需要判断在此时该格子是否着火,如果还未着火,则该格子是可以走的。
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std; const int maxn = + ; int map[maxn][maxn];
int fire[maxn][maxn];
int vis[maxn][maxn]; int dx, dy;
int n, m; int sx[] = { , , , - };
int sy[] = { , -, , }; struct node
{
int x, y;
int t;
}; void bfs1()
{
memset(fire, -, sizeof(fire));
queue<node> Q;
for (int i = ; i < n;i++)
for (int j = ; j < m; j++)
{
if (map[i][j] == -)
{
node p;
p.x = i;
p.y = j;
p.t = ;
Q.push(p);
fire[i][j] = ;
}
}
while (!Q.empty())
{
node p = Q.front();
Q.pop();
for (int k = ; k < ; k++)
{
int x = p.x + sx[k];
int y = p.y + sy[k];
if (x < || x >= n || y < || y >= m) continue;
if (map[x][y] != ) continue;
if (fire[x][y] != -) continue;
fire[x][y] = p.t + ;
node u;
u.x = x;
u.y = y;
u.t = p.t + ;
Q.push(u);
}
}
} int bfs2()
{
memset(vis, , sizeof(vis));
queue<node> Q;
node p;
p.x = dx;
p.y = dy;
p.t = ;
Q.push(p);
vis[dx][dy] = ;
while (!Q.empty())
{
node p = Q.front();
Q.pop();
if (p.x == || p.x == n - || p.y == || p.y == m - ) return p.t;
for (int k = ; k < ; k++)
{
int x = p.x + sx[k];
int y = p.y + sy[k];
if (vis[x][y]) continue;
if (x < || x >= n || y < || y >= m) continue;
if (map[x][y] != ) continue;
if (fire[x][y]!=- && p.t + >= fire[x][y]) continue;
node u;
u.x = x;
u.y = y;
u.t = p.t + ;
Q.push(u);
vis[x][y] = ;
}
}
return -;
} int main()
{
ios::sync_with_stdio(false);
//freopen("D:\\txt.txt", "r", stdin);
int T;
char c;
cin >> T;
while (T--)
{
cin >> n >> m;
for (int i = ; i < n;i++)
for (int j = ; j < m; j++)
{
cin >> c;
if (c == '#') map[i][j] = ;
else if (c == 'F') map[i][j] = -;
else if (c == 'J')
{
map[i][j] = ;
dx = i;
dy = j;
}
else map[i][j] = ;
} bfs1();
int ans=bfs2();
if (ans == -) cout << "IMPOSSIBLE" << endl;
else cout << ans << endl;
}
}
UVa 11624 大火蔓延的迷宫的更多相关文章
- UVA11624大火蔓延的迷宫
题意: 给1个n*m的网格,上面有的点能走,有的点不能走(墙),然后有的点是火源,火源和人一样,每次都是上下左右四个方向蔓延,速度一样是1,火也不可以从墙上跨过去,给你人的起点,终点是只要走到 ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...
- 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. ...
- UVa 11624 (BFS) Fire!
也是一个走迷宫的问题,不过又有了点变化. 这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的.问人是否能走出迷宫. 我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间. 第 ...
随机推荐
- Json和List的表示形式
JsonObject和List的表示形式 package payItem.test; import java.util.ArrayList; import java.util.List; import ...
- [django]celery_redis探索
celery+redis能做什么及简单原理 能干嘛: 看这里http://yshblog.com/blog/163 https://segmentfault.com/a/119000001565487 ...
- 【Cocos2dx 3.3】图片裁剪
从一个图片集中裁剪出需要的图片时,采用的坐标是屏幕坐标系: 示例如下: 图片:res/Images/grossini_dance_atlas.png ,每个人物大小为85* ...
- [LeetCode] questions conclustion_Path in Tree
Path in Tree: [LeetCode] 112. Path Sum_Easy tag: DFS input: root, target, return True if exi ...
- Django-made基础
知识预览 ORM 创建表(建立模型) 添加表记录 查询表记录 修改表记录 删除表记录 回到顶部 ORM 映射关系: 表名 <-------> 类名 字段 <-------> 属 ...
- mysql数据库环境配置中部分问题解决办法
注:原文地址:https://www.cnblogs.com/hezhuoheng/p/9366630.html 其中最重要的,是三个原则:命令按顺序输入.删除了ini(这个不是原则,是我解决问题的一 ...
- Linux系统——MySQL基础(三)
### MySQL主从复制实践#### 主从复制实践准备(1)主从复制数据库实战环境准备MySQL主从复制实践对环境的要求比较简单,可以是单机单数据库多实例的环境,也可以是两台服务器,每个机器一个独立 ...
- 做报表需要知道的基本的SQL语句
为客户做报表需要操作数据库,基本的SQL是必须要掌握的,下面介绍做报表最常用的SQL语句. 方法/步骤 1 查询数据:编号表示查询顺序. (8) select (9) distinct (11 ...
- Shell篇(三)TC Shell
Shell脚本的首行一般写为"#!+路径"来告诉系统,以路径所指定的程序来解释此脚本. 可以写为 #! /bin/tcsh -f (-f表示快速启动,不启动~/.tcshrc) S ...
- FRM-92095: Oracle Jnitiator version too low – please install version 1.1.8.2 or higher
打开EBS,系统报:FRM-92095: Oracle JInitiator 版本太旧. 请安装版本1.1.8.2或更高版本 (英文的错误信息是:FRM-92095: Oracle JInitiato ...