Problem Description
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The Labyrinth has a exit, Ignatius should get out of the 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, which is to move from one area to the nearest are A (that's, 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) Tak Es 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 The Labyr Inth, if he could, output the minimum time that he had to use to find the exit of the The labyrinth, else output-1.
Here is some rules:
1. We can assume the Labyrinth is a 2 array.
2. Each minute, Ignatius could-only get-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 The Labyrinth.
4. If Ignatius get to the area which contains bomb-rest-equipment then the exploding time turns to 0, he can ' t use the equ Ipment to reset the bomb.
5. A Bomb-reset-equipment can used as many times as you wish, if it's needed, Ignatius can get to any areas in the lab Yrinth as many times as you wish.
6. The time to reset the exploding time can is ignore, in and words, if Ignatius get to a area which contain bomb-rest- Equipment, and the exploding time is larger than 0, the exploding time would being reset to 6.
Input
The input contains several test cases. The first line of the input was a single integer T which is the number of test cases. T test Cases follow.
Each test case is starts with the 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 is five integers which indicate the different type of area in the Labyrinth:
0:the area was 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
Sample Output
4-113
Author
Ignatius.l
Analysis: This is a simple BFS problem, and the previous difference is that the problem can go to repeat the road, I just started thinking is to go to the problem how to solve, later on the internet to read other people's blog, just remove the impossible route can be,
My Code:
#include <iostream> #include <stdio.h> #include <queue> #include <string.h>using namespace std; int dx[]={0,1,-1,0};int dy[]={1,0,0,-1};struct node{int x, y; int time; int step;}; int M,n;int vis[1000][1000];inline bool in (node GX) {if (gx.x>=0&&gx.x<n&&gx.y>=0&& GX.Y<M) return true; return false;} int main () {int test; Node GX; cin>>test; while (test--) {cin>>n>>m; for (int i=0;i<n;i++) for (int j=0;j<m;j++) {cin>>vis[i][j]; if (vis[i][j]==2) {gx.x=i; Gx.y=j; gx.time=6; Gx.step=0; }} queue<node> Q; while (!q.empty ()) Q.pop (); Q.push (GX); Node next,tmp; int sp=0; while (!q.empty ()) {Tmp=q.front (), Q.pop (); if (tmp.time==1)//Remove the impossible route continue; for (int i=0;i<4;i++) {next.x=tmp.x+dx[i]; Next.y=tmp.y+dy[i]; if (Vis[next.x][next.y]&&in (next)) {next.time=tmp.time-1; next.step=tmp.step+1; if (vis[next.x][next.y]==4) {next.time=6; vis[next.x][next.y]=0; } q.push (next); } if (vis[next.x][next.y]==3) {sp=next.step; Break }} if (sp>0) break; } if (SP) printf ("%d\n", SP); else printf (" -1\n"); } return 0;}
Hangzhou Electric 1072---nightmare