HDU 1072 Nightmare 题解
Nightmare
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14260 Accepted Submission(s): 6930
Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.
Here are some rules:
1. We can assume the labyrinth is a 2 array.
2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.
3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.
5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.
6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.
Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.
There are five integers which indicate the different type of area in the labyrinth:
0: The area is a wall, Ignatius should not walk on it.
1: The area contains nothing, Ignatius can walk on it.
2: Ignatius' start position, Ignatius starts his escape from this position.
3: The exit of the labyrinth, Ignatius' target position.
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.
3 3
2 1 1
1 1 0
1 1 3
4 8
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
5 8
1 2 1 1 1 1 1 4
1 0 0 0 1 0 0 1
1 4 1 0 1 1 0 1
1 0 0 0 0 3 0 1
1 1 4 1 1 1 1 1
//Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
int n,m,map[][];
int vis[][];
int dir[][]={{-,},{,},{,},{,-}};
struct node
{
int x,y,t,step;
}; int bfs(int a,int b,int x,int y)
{
int i;
queue<node> q;
while(!q.empty())
q.pop();
q.push(node{a,b,,});//开始也能回来再走一次vis保持0
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.x==x&&t.y==y)
return t.step;
if(t.t<=)//会爆炸的情况扔掉
continue;
for(int i=;i<;i++)
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
if(dx>=&&dy>=&&dx<n&&dy<m&&!vis[dx][dy]&&(map[dx][dy]==||map[dx][dy]==)||map[dx][dy]==)//2也有可能重复走过
{
q.push(node{dx,dy,t.t-,t.step+});//此处可以重复走故vis不进行赋值
}
if(dx>=&&dy>=&&dx<n&&dy<m&&!vis[dx][dy]&&map[dx][dy]==)
{
vis[dx][dy]=;
q.push(node{dx,dy,,t.step+});//因为是直接归零,没必要重复走故直接把vis变成1
}
}
}
return -;
}
int main()
{
int t,a,b,x,y;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==)//记录起点
{
a=i;
b=j;
}
if(map[i][j]==)//记录终点
{
x=i;
y=j;
}
}
}
int ans=bfs(a,b,x,y);
printf("%d\n",ans);
}
return ;
}
Notes:思想要求较高
PS:笔者在做这道题的时候把bfs里面的两个判断语句弄反了,WA了好多次,可能这就是菜吧
2018-11-16 05:09:49 Author:LanceYu
HDU 1072 Nightmare 题解的更多相关文章
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
- hdu - 1072 Nightmare(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...
- HDU 1072 Nightmare
Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...
- HDU 1072 Nightmare (广搜)
题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...
- HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- [hdu P3085] Nightmare Ⅱ
[hdu P3085] Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU - 3085 Nightmare Ⅱ
HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...
- HDU 1072 (不一样的入队条件) Nightmare
之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次. 那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间). 为什么呢?我们 ...
- HDU 1072(记忆化BFS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...
随机推荐
- 学习CNN系列一:原理篇
CNN的发展历程: 1962年,卷积神经网络的研究起源于Hubel和Wiesel研究毛脑皮层的发现局部互连网络可以有效降低反馈神经网络的复杂性. 1980年,CNN的第一个实现网络:Fukushima ...
- Selenium库详解
Selenium 自动化测试工具,支持多种浏览器 爬虫中解决JS渲染问题
- Apache(基于主机名)
1.配置hosts文件 (1).hosts文件作用是定义IP地址与主机名的映射关系,即强制将某个主机名地址解析到指定的IP地址. (2)输入命令“vi /etc/hosts”,打开hosts文件,输入 ...
- ssd训练之bug:Invalid JPEG data or crop window, data size 565248
bug信息 tensorflow.python.framework.errors_impl.InvalidArgumentError: Invalid JPEG data or crop window ...
- Eclipse中如何安装Git插件
现在的Eclipse一般都自带Git插件. 检查Eclipse是否有Git插件: 方法一:Help—>About Eclipse,出现下面的图标,说明Eclipse中已有Git插件,就不用安装了 ...
- DRF--介绍和安装
前后端不分离 在前后端不分离的应用模式中,前端页面看到的效果都是由后端控制,由后端渲染页面或重定向,也就是后端需要控制前端的展示,前端与后端的耦合度很高.这种应用模式比较适合纯网页应用,但是当后端对接 ...
- Nginx 安装与部署配置
下载 官方网站:https://nginx.org/en/download.html Windows下安装 安装 下载后解压(切记不能含有中文路径!!),文件结构如图(我解压的路径就有中文,记得拷贝放 ...
- Redis与python
一.Redis介绍 Redis是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库(非关系型数据库). 本质:将数据保存在内存中. 用途:缓存.消息队列. 1.Redis的特点 R ...
- 从项目中理解let和const为什么如此重要
变量声明 变量声明方式 伴随js诞生的var // 语法 var varName = value var a = 1 // 这样子你就得到了一个变量 var缺陷场景分析 var specialUser ...
- 9.27 csp-s模拟测试53 u+v+w
T1 u 拿到题感觉他很水,但到死没想到正解,只会骗部分分(我太弱了) 考虑用两个差分数组维护,不同的是最后更新答案是$a[i][j]+=a[i-1][j-1]$,首先考虑在斜着加的起点,就是竖着的直 ...