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. Linode和DigitalOcean lnmp一键安装包哪个好?

    Linode和DigitalOcean都是非常棒的VPS厂商,512MB内存的VPS每月低到5美元,搭建wordpress网站,非常方便,甚至可以多人共用,服务器足够强悍,跑几个wordpress博客 ...

  2. winform控件背景透明问题(label..等)

    在使用winform写界面的时候,要想label这种控件实现对背景色透明,并且也懒得每次拖动一次控件都要在后台写一遍label1.Parent=PictureBox1 那么下面方法就介绍了比较简单便捷 ...

  3. 对AppStore中的项目进行评分(转载)

    在ios6.0前跳转到appstore评分一般是直接跳转到appstore评分NSString *evaluateString = [NSString stringWithFormat:@" ...

  4. 关于ios 程序加载百度地图lib,出现链接错误:找不到符号 (null): _OBJC_CLASS_$_BMKMapManager的解决办法

    报告的错误信息 ld: warning: ignoring file /Users/5012/Documents/sphuang/IOS_project/baidu_map/ShareLocation ...

  5. redis数据类型:sorted sets类型及操作

    sorted sets类型及操作: sorted set是set的一个升级版本,它是在set的基础上增加了一个顺序 属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会 自动重新按新的值 ...

  6. Python安装coverage.py

    coverage.py是一个用来统计python程序代码覆盖率的工具.它使用起来非常简单,并且支持最终生成界面友好的html报告.在最新版本中,还提供了分支覆盖的功能. 官方网站: http://ne ...

  7. linux之ls -l|grep "^-"|wc -l命令

    查看某文件夹下文件的个数 ls -l |grep "^-"|wc -l 或 find ./company -type f | wc -l 查看某文件夹下文件的个数,包括子文件夹里的 ...

  8. Python笔记2-20151023

    一.循环 Python的循环有两种,一种是for...in循环,依次吧list或tuple中的每个元素迭代出来. >>>names = ['Michael','Bob','Tracy ...

  9. 计算机网络 NAT

    NAT(Network Address Translation,网络地址转换)是1994年提出的.当在专用网内部的一些主机本来已经分配到了本地IP地址(即仅在本专用网内使用的专用地址),但现在又想和因 ...

  10. touchesBegan: withEvent: <--- with UIScrollView / UIImageView

    touchesBegan: withEvent: / touchesMoved: withEvent: / touchesEnded: withEvent: 等只能被UIView捕获(如有问题请指出对 ...