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.
InputThe 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.
OutputFor 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
题目的大意就是 迷宫4处可以重置炸弹,,0是墙壁,1是空地,2是起点,3是终点,每个地方都可重复走,判断能否到达终点
这里有坑就是每个地方可以重复的走,位置4可以多次重置炸弹(这就是个坑)。。。因为当第二次来同一个位置4重置炸弹时,,OK,,一定构成了循环,,一定走不出去了。所以想要走出去同一个位置4 只能去一次,,所以要标记一下。然后BFS就可以了
#include<iostream> #include<queue> #include<cstring> using namespace std; struct stu{ int x,y,z; }; int step[10][10]; int d[4][3]={{1,0,-1},{0,1,-1},{0,-1,-1},{-1,0,-1}}; int n,m; int flag=0; int start_x,start_y,end_x,end_y; int mark[10][10]; int arr[10][10]; void bfs(){ queue<stu>que; que.push({start_x,start_y,6}); step[start_x][start_y]=0; while(que.size()){ int xx=que.front().x; int yy=que.front().y; int zz=que.front().z; que.pop(); for(int i=0;i<4;i++){ int dx=xx+d[i][0]; int dy=yy+d[i][1]; int dz=zz+d[i][2]; if(dz>0&&dx>=0&&dy>=0&&dx<n&&dy<m&&arr[dx][dy]!=0&&mark[dx][dy]==0) { step[dx][dy]=step[xx][yy]+1; if(arr[dx][dy]==3){ flag=1; return ; } if(arr[dx][dy]==4){ dz=6; mark[dx][dy]=1; } que.push({dx,dy,dz}); } } } } int main() { int t; scanf("%d",&t); while(t--) { memset(mark,0,sizeof(mark)); flag=0; scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ scanf("%d",&arr[i][j]); } } for(int i=0;i<n;i++) for(int j=0;j<m;j++){ if(arr[i][j]==2){ start_x=i; start_y=j; } else if(arr[i][j]==3){ end_x=i; end_y=j; } } bfs(); if(flag) printf("%d\n",step[end_x][end_y]); else puts("-1"); } return 0; }