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 1Sample Output
4 -1 13
由于多了一个时间变量,在不同时间,即使在同一位置也会造成不同的结果。
因此每种状态由位置(x,y)和时间(t)构成
构造一个结构体,来存储每种状态,遍历所有情况, 找到最优解
在状态更新时,只更新剩余时间更长的情况即可
struct point { int x, y; int time; int dis; point(int a, int b, int c, int d) { x = a; y = b; time = c; dis = d; } };
最初使用了三维数组,然而超时……找不到问题,最后重写了遍
1 /* 2 By:OhYee 3 Github:OhYee 4 Email:oyohyee@oyohyee.com 5 Blog:http://www.cnblogs.com/ohyee/ 6 7 かしこいかわいい? 8 エリーチカ! 9 要写出来Хорошо的代码哦~ 10 */ 11 #include <cstdio> 12 #include <algorithm> 13 #include <cstring> 14 #include <cmath> 15 #include <string> 16 #include <iostream> 17 #include <vector> 18 #include <list> 19 #include <queue> 20 #include <stack> 21 using namespace std; 22 23 //DEBUG MODE 24 #define debug 0 25 26 //循环 27 #define REP(n) for(int o=0;o<n;o++) 28 29 30 31 const int maxn = 10; 32 int n, m; 33 int Map[maxn][maxn]; 34 35 36 const int delta[4] = { 1,-1,0,0 }; 37 38 struct point { 39 int x, y; 40 int time; 41 int dis; 42 point(int a, int b, int c, int d) { 43 x = a; 44 y = b; 45 time = c; 46 dis = d; 47 } 48 }; 49 50 int BFS(int s1, int s2, int v1, int v2) { 51 queue<point> Q; 52 int mark[maxn][maxn]; 53 memset(mark, -1, sizeof(mark)); 54 int ans = -1; 55 56 Q.push(point(s1, s2, 6, 0)); 57 mark[s1][s2] = 6; 58 while (!Q.empty()) { 59 point temp = Q.front(); 60 Q.pop(); 61 62 int x = temp.x; 63 int y = temp.y; 64 int time = temp.time; 65 int dis = temp.dis; 66 67 if (x == v1 && y == v2) { 68 ans = ((ans == -1) ? (dis) : min(ans, dis)); 69 //printf(" (%d,%d)%d %d ", x,y,time,dis); 70 } 71 72 //** 73 REP(4) { 74 int xx = x + delta[o]; 75 int yy = y + delta[3-o]; 76 int tt = time - 1; 77 int dd = dis + 1; 78 79 if (xx < 0 || xx >= n || yy < 0 || yy >= m 80 || Map[xx][yy] == 0 || tt == 0) 81 continue; 82 if (Map[xx][yy] == 4) 83 tt = 6; 84 if (mark[xx][yy] < tt) { 85 mark[xx][yy] = tt; 86 //printf("%d %d %d %d ", xx, yy, tt, dd); 87 Q.push(point(xx, yy, tt, dd)); 88 } 89 } 90 } 91 return ans; 92 } 93 94 void Do() { 95 char s1, v1; 96 int s2, v2; 97 scanf("%d%d ", &n, &m); 98 for (int i = 0; i < n; i++) 99 for (int j = 0; j < m; j++) { 100 scanf("%d", &Map[i][j]); 101 if (Map[i][j] == 2) { 102 s1 = i; 103 s2 = j; 104 Map[i][j] = 1; 105 } 106 if (Map[i][j] == 3) { 107 v1 = i; 108 v2 = j; 109 Map[i][j] = 1; 110 } 111 } 112 printf("%d ", BFS(s1, s2, v1, v2)); 113 } 114 115 116 int main() { 117 int T; 118 scanf("%d", &T); 119 while (T--) 120 Do(); 121 return 0; 122 }