Nightmare

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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
思路:bfs+贪心。
    time[][]记录时间,ti[][]记录距离爆炸时间,
      重复走的地方除非离爆炸时间变长了,才压入栈中。
         重置时间的位置走过之后可以变为0,因为以后再走,结果相同。
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<deque>
#include<cstring>
using namespace std; int dr[][]={{,},{,},{-,},{,-} };
int n,m,i,j,sx,sy,flag,ans,t,tx,ty;
int mp[][],vis[][],time[][],ti[][];
struct node
{
int x,y;
};
deque<node> s;
int check(int x,int y)
{
if (x> && x<=n && y> && y<=m && mp[x][y]!=) return ;
else return ;
}
void bfs()
{
node p;
s.clear();
p.x=sx;
p.y=sy;
vis[sx][sy]=;
s.push_back(p);
while(!s.empty())
{
node q=s.front();
for(int i=;i<;i++)
{
int xx=q.x+dr[i][];
int yy=q.y+dr[i][];
if(check(xx,yy))
{
if(vis[xx][yy])
{
if (ti[xx][yy]<ti[q.x][q.y]-)
{
ti[xx][yy]=ti[q.x][q.y]-;
time[xx][yy]=time[q.x][q.y]+;
p.x=xx;
p.y=yy;
s.push_back(p);
}
} else
{
if(ti[q.x][q.y]->)
{
p.x=xx;
p.y=yy;
s.push_back(p);
vis[xx][yy]=;
time[xx][yy]=time[q.x][q.y]+;
ti[xx][yy]=ti[q.x][q.y]-;
if(mp[xx][yy]==)
{
ti[xx][yy]=;
mp[xx][yy]=;
}
if (mp[xx][yy]==) return;
}
} }
}
s.pop_front();
}
return;
} int main()
{
scanf("%d",&t);
for(;t>;t--)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
{
scanf("%d",&mp[i][j]);
if (mp[i][j]==) sx=i,sy=j;
if (mp[i][j]==) tx=i,ty=j;
}
ans=INT_MAX;
flag=;
memset(vis,,sizeof(vis));
memset(ti,,sizeof(ti));
memset(time,-,sizeof(time));
ti[sx][sy]=;
time[sx][sy]=;
bfs();
/*if(flag) printf("%d\n",ans);
else printf("-1\n");*/
printf("%d\n",time[tx][ty]);
/* printf("------------------\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
printf("%d ",ti[i][j]);
printf("\n");
}*/
}
return ;
}

HDU-1072 Nightmare (bfs+贪心)的更多相关文章

  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 1072 Nightmare 题解

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  6. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

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

  7. [hdu P3085] Nightmare Ⅱ

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

  8. HDU - 3085 Nightmare Ⅱ

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

  9. UVA12130 Summits(BFS + 贪心)

    UVA12130 Summits(BFS + 贪心) 题目链接 题目大意: 给你一个h ∗ w 的矩阵,矩阵的每一个元素都有一个值,代表这个位置的高度. 题目要求你找出这个图中有多少个位置是峰值点.从 ...

随机推荐

  1. linux上发布网站遇到的问题

    在linux上部署mono之后,就自己使用vs写了个简单的网站上传到linux下的apache目录下的htdocs下,就使用主机的浏览器访问地址,但是很残酷的报错了!百思不得其解.搞了半天才想到是因为 ...

  2. 使用Cookie来统计浏览次数,当天重复刷新不增加

    这是一种不严谨的做法,在浏览量不是很重要的时候可以使用 var oldCookie = Request.Cookies["newsCookie"]; if (oldCookie = ...

  3. cmusphinx格式问题

    在windows下.lm和.dict同时为ANSI编码,输出正确,否则输出乱码或不输出结果.

  4. Threading

    new System.Threading.Thread(new System.Threading.ThreadStart(ReadState)).Start();

  5. protected-broadcast的作用

    protected-broadcast(暂时从网上搜到这么多,也不知对错,先留着) “保护性广播”,在一些AndroidManifest.xml中的一级标记<protected-broadcas ...

  6. 转:JMeter基础--逻辑控制器Logic Controller

    1.ForEach控制器 ForEach控制器在用户自定义变量中读取一系列相关的变量.该控制器下的采样器或控制器都会被执行一次或多次,每次读取不同的变量值.所以ForEach总是和User Defin ...

  7. svn hooks的使用demo

    我是理论家: svn server端提供了Hooks Script.所谓钩子实际上是一种时间触发机制,是指当系统执行到某个特殊的事件时,触发我们预定义的动作,可以让我们在某些特定状态发生的时候做我们想 ...

  8. 第十四节,基本数据类型,列表list

    创建列表  #!/usr/bin/env python # -*- coding:utf-8 -*- a = ['alex', 'seven', 'eric'] #或 a = list(['alex' ...

  9. hdu_4046_Panda(树状数组)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 题意:一大堆篇幅介绍,跳过直奔主题,让你寻找给定区间的“wbw”的个数. 题解:直接上树状数组, ...

  10. HDU2539:点球大战

    Problem Description 在足球比赛中,有不少赛事,例如世界杯淘汰赛和欧洲冠军联赛淘汰赛中,当比赛双方经过正规比赛和加时赛之后仍然不分胜负时,需要进行点球大战来决定谁能够获得最终的胜利. ...