hdoj 1072 Nightmare
2015-10-29 17:33
176 查看
Nightmare
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8776 Accepted Submission(s): 4220
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
4
-1
13注意:时间为零时,即使所在点是时间也不能继续走!,只能在拥有最后一个时间点时,寻找时间重置! 代码:
#include<stdio.h> #include<iostream> #include<queue> #include<algorithm> #include<string.h> using namespace std; #define N 10 int dx[4]={1,-1,0,0}; int dy[4]={0,0,-1,1}; int map ; struct node { int x,y,step,time; }bg,en; int X,Y; int judge(int x,int y) { return x>=0&&x<X&&y>=0&&y<Y&&map[x][y]!=0; } int bfs() { int i,j; queue<node> Q; bg.step =0; bg.time =6; Q.push(bg); node q,p; while(!Q.empty() ) { q=Q.front() ; Q.pop(); for(i=0;i<4;i++) { p.x =q.x+dx[i]; p.y =q.y+dy[i]; p.time=q.time-1; p.step=q.step +1; if(p.time<=0)//判断当前的时间是否结束,并搜索其他方向! continue; if(judge(p.x,p.y))//判断是否出界或是墙壁! { int t=map[p.x][p.y]; if(t==4) { p.time=6; map[p.x ][p.y]=1;//置为路! } if(t==3) { return p.step ; } Q.push(p); } } } return -1; } int main() { int i,j; int n; scanf("%d",&n); while(n--) { int x,y,z; scanf("%d%d",&X,&Y); for(i=0;i<X;i++) { for(j=0;j<Y;j++) { scanf("%d",&map[i][j]); z=map[i][j]; if(z==2) { bg.x =i; bg.y =j; } if(z==3) { en.y =j; en.x =i; } } } printf("%d\n",bfs()); } return 0; }
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- JavaScript中数组的排序、乱序和搜索实现代码
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- mysql 模糊搜索的方法介绍
- C#搜索文字在文件及文件夹中出现位置的方法
- 基于ASP.NET的lucene.net全文搜索实现步骤
- 做个自己站内搜索引擎
- PHP查找与搜索数组元素方法总结