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,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间. 第 ...
随机推荐
- 011-spring cloud gateway-使用
一.pom增加 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- vertx 从Tcp服务端和客户端开始翻译
写TCP 服务器和客户端 vert.x能够使你很容易写出非阻塞的TCP客户端和服务器 创建一个TCP服务 最简单的创建TCP服务的方法是使用默认的配置:如下 NetServer server = ve ...
- break continue练习
break :跳出 1.当break单独存在时,下面不要定义其他语句,因为执行不到. 2.如果出现了循环嵌套,break响应跳出指定的循环,可以通过标号来完成 例如: continue:要么是swit ...
- (Power Strings)sdutoj2475
#include <stdio.h>#include <string.h>#include <stdlib.h>char a[1000001];int next[1 ...
- openstack部署心得
官方文档:https://docs.openstack.org/ 个别版本有中文 不要轻易尝试最新版本 新版本刚推出一般存在不少BUG或者文档没有更新,按照文档配置就是不能成功.推荐尝试最新版本的上一 ...
- 计划评审技术PERT
概念 编辑 PERT(Program Evaluation and Review Technique)即 [2] 计划评审技术,最早是由美国海军在计划和控制北极星导弹的研制时发展起来的.PERT技术 ...
- CentOS7更改Docker默认镜像和容器存储位置
图片出处:https://bobcares.com/wp-content/uploads/docker-change-directory.jpg 一.Why? 通常,当你开始使用docker时,我们并 ...
- 用Anaconda安装TensorFlow+keras
检测目前安装了哪些环境变量:conda info --envs 查看当前有哪些可以使用的tensorflow版本:conda search --full -name tensorflow 查看ten ...
- Django中的admin组件分析
admin的使用介绍 django-admin的使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.可以在项目的 setting ...
- linux脚本-判断进程是否存在,从而可以做预警处理..
count=`ps -ef | grep Seeyon | grep -v "grep" | wc -l` echo $count if [ $count -gt 0 ]; the ...