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,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间. 第 ...
随机推荐
- 论存储IOPS和Throughput吞吐量之间的关系
论存储IOPS和Throughput吞吐量之间的关系 http://www.csdn.net/article/2015-01-14/2823552 IOPS和Throughput吞吐量两个参数是衡量存 ...
- MySQL · 功能分析 · 5.6 并行复制实现分析
背景 我们知道MySQL的主备同步是通过binlog在备库重放进行的,IO线程把主库binlog拉过去存入relaylog,然后SQL线程重放 relaylog 中的event,然而这种模式有一个问题 ...
- node初识——node中的require方法与require.js的区别
出处:http://blog.csdn.net/u013613428/article/details/51966500 作为一个前端的新手,总是诧异于js的模块载入方式,看到了通过requireJs提 ...
- JS中的对象数组
<html> <head> <title>对象数组的字符串表示</title> <script type="text/javascrip ...
- 登陆跳板机每天只输入一次token的方法——ssh clone session
自从跳板机升级后,无所不在的token让小PE很是恼火,于是有了这篇文章@_@ Linux or Mac篇 在Fedora或者Mac下很简单,修改~/.ssh/config文件,没有的话,就新建一个( ...
- cmd重启服务器,有时不想去机房,并且远程桌面连接登录不上了
有时不想去机房,并且远程桌面连接登录不上了,需要远程重启服务器的,这时可以使用命令行方式远程重启.在cmd命令行状态下输入:shutdown -r -m \\192.168.1.10 -t 0 -f ...
- NPOI 导出excel 分表
/// <summary> /// 由DataTable导出Excel[超出65536自动分表] /// </summary> /// <param name=" ...
- webapi 返回json及route设置
1.返回json 修改App_Start/webapiconfig public static void Register(HttpConfiguration config) { // Web API ...
- 55. Jump Game(贪心)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- MAX_STATEMENT_TIME uses confusing syntax
From https://bugs.mysql.com/bug.php?id=72540 [5 May 2014 18:46] Morgan Tocker Description: Via C ...