题目描述
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
分析
本题需要计算从起点到终点的最短时间,所以采用bfs进行解题。
但是不同于其他题的地方就是于虽然也是bfs,但对于走过的路径不能标记,因为可能还要走,注意题目要求:如果可以在爆炸之前到达,就可以走任意多遍。
这就引发了一个问题,如果不缩减搜索范围,怎么可能走得出来呢?应该说这题好就好在不是根据走过的路径来标记,而是根据前后两次踏上同一位置是炸弹距离爆炸的时间长短来标记。简言之,如果第二次踏上一个位置,那么找出路已用的时间肯定是增加了,那为啥还要走上这条路呢?唯一的追求就是炸弹离爆炸的时间增大了。
所以每次经过了加时地点(对应位置为4),就把4标记为已经走过,防止出现反复加时的死循环。
C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 10;
int g[N][N],ex[N][N],T,n,m; //g[][]为整张地图,ex[][]为距离爆炸时间
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct point{
int x,y,time,remain;
}start,en;
int bfs()
{
memset(ex,0,sizeof ex); //刚开始没有走到,所以将其全部赋值为0
int ans=0;
queue<point> q;
q.push(start);
while(q.size())
{
auto t=q.front();
q.pop();
if(t.x==en.x && t.y==en.y && t.remain>=0)
return t.time;
point cur;
for(int i=0;i<4;i++)
{
cur.x=t.x+dx[i],cur.y=t.y+dy[i];
cur.remain=t.remain-1;
cur.time=t.time+1;
if(cur.remain<0) break; //炸弹爆炸,直接跳出循环
if(cur.x>=0 && cur.x<n && cur.y>=0 && cur.y<m && g[cur.x][cur.y]!=0 && ex[cur.x][cur.y]<=cur.remain)
{ //在地图内并且不是墙(0)并且当前点距离爆炸的时间较之前增加了,说明经过了加时点(4)
if(g[cur.x][cur.y]==4) cur.remain=5; //遇到加时点,更新剩余时间
q.push(cur);
ex[cur.x][cur.y]=cur.remain; //将当前剩余时间给ex[][]响应点位赋值
}
}
}
return -1; //到死也没走出去 X(
}
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>g[i][j];
if(g[i][j]==2) //记录起点
{
start.x=i,start.y=j;
start.remain=5;
start.time=0;
}
if(g[i][j]==3) //记录终点
{
en.x=i,en.y=j;
}
}
}
cout<<bfs()<<endl;
}
return 0;
}