hdoj 1072 Nightmare
Nightmare
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8480 Accepted Submission(s):
4069
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.
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.
labyrinth, you should output the minimum time he needs, else you should just
output -1.
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int map[10][10];
int n,m;
int x1,y1,x2,y2;
struct node
{
int x,y;
int time;
int step;
friend bool operator < (node a,node b)
{
return a.step>b.step;
}
};
void bfs()
{
int i,j;
priority_queue<node>q;
int move[4][2]={0,1,0,-1,-1,0,1,0};
node beg,end;
beg.x=x1;
beg.y=y1;
beg.time=6;
beg.step=0;
q.push(beg);
while(!q.empty())
{
end=q.top();
q.pop();
if(end.x==x2&&end.y==y2)
{
printf("%d\n",end.step);
return ;
}
if(end.time==1)
continue;
for(i=0;i<4;i++)
{
beg.x=end.x+move[i][0];
beg.y=end.y+move[i][1];
if(beg.x>=0&&beg.x<n&&beg.y>=0&&beg.y<m&&map[beg.x][beg.y]!=0)
{
if(map[beg.x][beg.y]==4)
{
beg.time=6;
map[beg.x][beg.y]=1;
}
else
beg.time=end.time-1;
beg.step=end.step+1;
q.push(beg);
}
}
}
printf("-1\n");
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==2)
{
x1=i;y1=j;
}
else if(map[i][j]==3)
{
x2=i;y2=j;
}
}
}
bfs();
}
return 0;
}
hdoj 1072 Nightmare的更多相关文章
- 【HDOJ】1072 Nightmare
bfs,使用ttl进行剪枝. #include <iostream> #include <cstdio> #include <cstring> #include & ...
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
- 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 - 1072 Nightmare(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...
- HDU 1072 Nightmare 题解
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- BFS(双向) HDOJ 3085 Nightmare Ⅱ
题目传送门 题意:一个人去救女朋友,两个人都在运动,还有鬼在"扩散",问最少几秒救到女朋友 分析:开两个队列来表示两个人走过的路,一个人走到的地方另一个人已经vis了,那么就是相遇 ...
- 步步为营(十六)搜索(二)BFS 广度优先搜索
上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...
- HDU 1072 (不一样的入队条件) Nightmare
之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次. 那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间). 为什么呢?我们 ...
随机推荐
- OpenCV Tricks[OpenCV 笔记3]
官方例程 事例程序位于opencv-3.1.0/samples/cpp/ 目录下,可以通过编译整个工程,编译所有的Sample Code 显示当前使用的OpenCV版本 CV_VERSION为标识当前 ...
- 从ZOJ2114(Transportation Network)到Link-cut-tree(LCT)
[热烈庆祝ZOJ回归] [首先声明:LCT≠动态树,前者是一种数据结构,而后者是一类问题,即:LCT—解决—>动态树] Link-cut-tree(下文统称LCT)是一种强大的数据结构,不仅可以 ...
- Codeforces 549B Looksery Party
Looksery Party Solution: 仔细分析一下会发现每个人都会发一条消息给自己这个条件非常重要! 这个条件保证了一定会有解,而且解法也要从这里入手. 当我们拿到一个猜测的答案序列的时候 ...
- Shortcut Collapse project or projects in the Solution Explorer Microsoft Visual Studio 2008
The standard windows keyboard shortcuts for expanding and collapsing treeviews are: Numeric Keypad * ...
- Junit4拓展工具JCategory与Testng的Group功能比较
前言 笔者前段时间分享过一遍文章,关于如何通过引入新注解来扩展Junit4,以解决Process上的问题: JUnit扩展:引入新注解Annotation 最近在跟外面的同事聊的时候,得知Testng ...
- 使用OPCDAAuto.dll编写C# OPC采集程序
在一台新机器上运行使用OPC自动化接口编写的C#程序报错如下: 索 COM 类工厂中 CLSID 为 {28E68F9A-8D75-11D1-8DC3-3C302A000000} 的组件失败,原因是出 ...
- bzoj 2209: [Jsoi2011]括号序列 splay
2209: [Jsoi2011]括号序列 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 833 Solved: 392[Submit][Status ...
- Node.js V0.12新特性之性能优化
v0.12悠长的开发周期(已经过去九个月了,并且还在继续,是有史以来最长的一次)让核心团队和贡献者们有充分的机会对性能做一些优化.本文会介绍其中最值得注意的几个. 支持塞住模式的可写流 现在可写流可以 ...
- Django自身的CBV列表
慢慢就有感觉了.... 这个是可以快速开发很多东东,不过,类视图要求的积累还是有一些的.. ~~~~~~~~~~~~~ CBVs在功能上的可扩展性, 牺牲的是简单性, 一个CBV最多的时候拥有8个im ...
- Maven学习(1) - Maven入门
home index:http://maven.apache.org/ download:http://maven.apache.org/download.cgi install: http://ma ...