F - Nightmare
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
HDU 1072
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
优先队列搜索问题,不明白网上怎么有那么多复杂的方法,还要考虑剪枝和最小步数。
直接上代码吧,要说的都在注释里
#include<stdio.h>
#include<string.h>
#include<string>
#include<stack>
#include<queue>
#include<math.h>
#include<limits.h>
#include<iostream>
#include<algorithm>
using namespace std;
//广度搜索,优先队列
//0 障碍
//1可行点
//2起点
//3终点
//4特殊点,时间重置为6
//初始时间为6
int map[10][10];//地图数据
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; //搜索方向
int m,n;//地图大小
int starx1,stary1;//起点坐标,y1这名字竟然不合法
struct node
{
int x;
int y;
int time;//剩余时间
int step;//实际步数
};
void BFS()
{
queue<node>q;
node star,end;
star.x=starx1;
star.y=stary1;
star.step=0;//初始化步数
star.time=6;//初始化剩余时间
q.push(star);
while(!q.empty())
{
star=q.front();
q.pop();
for(int i=0; i<4; i++) //四个方向搜索
{
end.x=star.x+dir[i][0];//获取新的搜索坐标
end.y=star.y+dir[i][1];
end.time=star.time-1;//剩余时间减一
end.step=star.step+1;//实际步数加一
if(end.x>=0&&end.x<m&&end.y>=0&&end.y<n&&map[end.x][end.y]!=0&&end.time>0)
{
//坐标合法,当前坐标位置不是0,有剩余时间
if(map[end.x][end.y]==3)//终于遇到终点了
{
printf("%d\n",end.step);
return;
}
else if(map[end.x][end.y]==4)
{
end.time=6;
map[end.x][end.y]=0;
}
q.push(end);//不管结果如何,只要合法,此结点最后都要入队
}
}
}
printf("-1\n");
}
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",&map[i][j]);
if(map[i][j]==2)//如果遇到起点
{
starx1=i;//记录起点坐标
stary1=j;
map[i][j]=0;//标记起点已搜索
}
}
}
BFS();
}
return 0;
}
这样一步步写下来,也不用剪枝,得到的就已经是最小步数了。