HDU1072-Nightmare
来源:互联网 发布:华为杭州研究所知乎 编辑:程序博客网 时间:2024/05/18 02:04
Nightmare
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 81 Accepted Submission(s) : 50
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.
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.
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
33 32 1 11 1 01 1 34 82 1 1 0 1 1 1 01 0 4 1 1 0 4 11 0 0 0 0 0 0 11 1 1 4 1 1 1 35 81 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-113
Author
题意:在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器。定时炸弹的时间是6,人走一步所需要的时间是1。每次可以上、下、左、右移动一格。当人走到4时如果炸弹的时间不是0,可以重新设定炸弹的时间为6。如果人走到3而炸弹的时间不为0时,成功走出。求人从2走到3的最短时间。
解题思路:bfs,点可以重复走,但必须是到这个点时炸弹剩余时间会变大
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <queue>#include <vector>#include <set>#include <bitset>#include <stack>#include <map>#include <climits>#include <functional>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;struct node{ int x,y,steps,ltime;}start;int dx[6]={-1,1,0,0};int dy[6]={0,0,-1,1};int visit[10][10];int maps[10][10];int m,n;bool check(node a){ if(a.x>=0&&a.x<m&&a.y>=0&&a.y<n&&maps[a.x][a.y]!=0) return true; else return false;}int bfs(){ memset(visit,0,sizeof visit); queue<node>q; q.push(start); node cur,next; while(!q.empty()) { cur=q.front(); q.pop(); for(int i=0;i<4;i++) { next.x=cur.x+dx[i]; next.y=cur.y+dy[i]; if(maps[next.x][next.y]==3&&cur.ltime>1) return cur.steps+1; if(maps[next.x][next.y]==4&&cur.ltime>1) next.ltime=6; else if(maps[next.x][next.y]==1) next.ltime=cur.ltime-1; if(check(next)&&next.ltime>visit[next.x][next.y]) { visit[next.x][next.y]=next.ltime; next.steps=cur.steps+1; q.push(next); } } } return -1;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d %d",&m,&n); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { scanf("%d",&maps[i][j]); if(maps[i][j]==2) { start.x=i; start.y=j; start.steps=0; start.ltime=6; maps[i][j]=0; } } } printf("%d\n",bfs()); } return 0;}
0 0
- HDU1072 Nightmare
- hdu1072 Nightmare
- hdu1072-Nightmare
- HDU1072 nightmare
- hdu1072 nightmare
- Hdu1072 Nightmare
- HDU1072:Nightmare
- Nightmare hdu1072
- HDU1072-Nightmare
- HDU1072 Nightmare
- hdu1072 Nightmare
- hdu1072 Nightmare
- hdu1072 Nightmare (BFS)
- hdu1072 Nightmare (BFS)
- HDU1072 Nightmare 【BFS】
- hdu1072 Nightmare 广搜
- hdu1072 Nightmare(BFS)
- hdu1072 Nightmare(bfs)
- 【51nod1462】树据结构
- Python数据结构与算法分析学习记录(1)——基于Problem Solving with Algorithms and Data Structures using Python的学习
- 在Keil C51函数中加入ASM代码
- MySQL 中间件汇总比较 .
- 备忘录模式(状态变化)
- HDU1072-Nightmare
- MarkDown笔记(一)
- Python数据挖掘相关机器学习库
- ajax(三)之省市二级菜单联动(从sql获取数据),无刷新翻页
- 1035. 插入与归并(25)
- 不要总埋怨自己记忆不好了,其实跟你的智商关系不大
- Java 后端实战——基于 Dubbo 的分布式系统架构
- SHOW INDEX 你用过吗???
- git克隆