Nightmare

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14260    Accepted Submission(s): 6930

Problem Description
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.

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.

 
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
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.
 
Output
For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.
 
Sample Input
 
3
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
 
Sample Output
 
4
-1
13
Author  Ignatius.L
 
Recommend  We have carefully selected several similar problems for you:  1026 1016 1010 1175 1180 
-------------------------------------------------------------------------------------------------------------------------------------------------------
这道题目显然是用bfs来做的
但是有几点必须要考虑
因为存在时间归零的情况
所以一个点可能经过多次
又因为时间归零了过后
在这一点会有重复
所以将n=4的点标记为走过即可
这样最后如果到不了就会队列会一直运行直到空
下面拿出我有超多注释
谁都能看懂的代码
-------------------------------------------------------------------------------------------------------------------------------------------------------
 //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 题解的更多相关文章

  1. hdu 1072 Nightmare (bfs+优先队列)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...

  2. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  3. HDU 1072 Nightmare

    Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...

  4. HDU 1072 Nightmare (广搜)

    题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...

  5. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

    HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  6. [hdu P3085] Nightmare Ⅱ

    [hdu P3085] Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...

  7. HDU - 3085 Nightmare Ⅱ

    HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...

  8. HDU 1072 (不一样的入队条件) Nightmare

    之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次. 那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间). 为什么呢?我们 ...

  9. HDU 1072(记忆化BFS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...

随机推荐

  1. CUDA -- 内存分配

    CUDA可以认为是一个由软件和硬件构成的并行计算系统,其依赖于GPU的并行计算单元,CUDA有类C的API,方便程序编写.其依赖于CPU和GPU的异构体系,通过在CPU上串行执行环境初始化.内存分配. ...

  2. Pwn-level1

    题目地址 https://dn.jarvisoj.com/challengefiles/level1.80eacdcd51aca92af7749d96efad7fb5 先看一下文件的类型和保护机制   ...

  3. jmeter录制移动端脚本

    jmeter录制脚本有两种方式,一种借助外部工具badbody,一种是本身的功能,使用代理服务器,介绍下如何使用代理服务器录制脚本.我一般在测app或者移动端H5页面时才会录制,所以此文也针对移动端. ...

  4. CF1195E OpenStreetMap

    题目链接 题意 有一个\(n\times m\)的矩阵,询问其中所有大小为\(a \times b\)的子矩阵的最小值之和. \(1\le n,m \le 3000\) 思路 因为是子矩阵的大小是固定 ...

  5. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

  6. github README.md创建不了

    在项目主页上,点击‘Add a README’按钮,如下图: 进入编辑界面,编辑好内容后,提交按钮的状态为灰化不可点击,如下图: 不知道为什么会出现这种情况,但是我无意中点击了Ctrl+Enter竟然 ...

  7. 家用环境下部署wifidog认证服务器(java版)

    本文所讲的是基于一个java版wifidog认证服务器的开源项目,在windows环境下搭建wifidog认证服务器配合apfree固件实现用户名密码的认证. 大致步骤如下: 一,准备 1.搭建硬件及 ...

  8. 1+x 证书 Web 前端开发 MySQL 知识点梳理

    官方QQ群 1+x 证书 Web 前端开发 MySQL 知识点梳理 http://blog.zh66.club/index.php/archives/199/

  9. JAVA 统计字符串中中文,英文,数字,空格,特殊字符的个数

    引言 可以根据各种字符在Unicode字符编码表中的区间来进行判断,如数字为'0'~'9'之间,英文字母为'a'~'z'或'A'~'Z'等,Java判断一个字符串是否有中文是利用Unicode编码来判 ...

  10. 将服务器时间类型改为UTC(0000)

    方法一: # timedatectl  set-timezone UTC #  timedatectl set-time "YYYY-MM-DD HH:MM:SS" #  time ...