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表示上次经过该点时剩余的时间). 为什么呢?我们 ...
随机推荐
- 24种设计模式--适配器模式【Adapter Pattern】
今天讲适配器模式,这个模式也很简单,你笔记本上的那个拖在外面的黑盒子就是个适配器,一般你在中国能用,在日本也能用,虽然两个国家的的电源电压不同,中国是 220V,日本是 110V,但是这个适配器能够把 ...
- android入门到熟练(二)----活动
1.活动创建对于每一个后端java类(继承至Activity或者ActionBarActivity)代码都有一个方法需要被重写[onCreate], 在此方法中可以加载界面资源文件或者绑定元素事件. ...
- 引入的iframe是跨域的, 如何控制其高度
前提是你有编辑这个跨域的iframe的权限!! 1. main-domain.html (main display HTML) <!DOCTYPE html> <html> & ...
- Dice (III) 概率dp
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> ...
- php中的NOTICE 的错误解决方法
PHP新手NOTICE错误,特此写给那些遇到和我一样错误的朋友. 刚学习PHP,不久 最近在整留言板,刚才遇到个问题. 页面中,好多类似 Notice: in D:\wamp\www\study\ ...
- SQL的四种语言和数据库范式
1. SQL的四种语言 DDL(Data Definition Language)数据库定义语言 CREATE ALTER DROP TRUNCATE COMMENT RENAME DML(Data ...
- OC语言-02面向对象的三大特性
01封装 #import <Foundation/Foundation.h> @interface Student : NSObject { //@public 成员变量尽量不使用 int ...
- C语言实现五子棋简单功能
/******************************************************************** C-4.29-1: 实现五子棋游戏 操作说明:用方向键或者& ...
- scala 安装
http://www.scala-lang.org/download/install.html http://zh.scala-tour.com/#/hello-wolrd scala指南 To ru ...
- 使用Style自定义ListView快速滑动图标
一.显示ListView快速滑动块图标 设想这样一个场景,当ListView的内容有大于100页的情况下,如果想滑动到第80页,用手指滑动到指定位置,无疑是一件很费时的事情,如果想快速滑动到指定的位置 ...