NightmareTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total Submission (s): 7367 Accepted Submission (s): 3530
Problem Descriptionignatius 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.
Inputthe 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.
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
Sample Output
4-113
Authorignatius.lTest Instructions: Given an n-row m-column map, where 1 for the open space, 2 for the entrance, 3 for the exit, 4 for the bomb time Reset point, the bomb time to initialize is 6 seconds, each move can only be up and down adjacent lattice, each time takes 1 seconds, 0 seconds when the bomb exploded immediately, even if the exit or reset point is useless , it takes the least time to reach the exit from the inlet, if it does not output-1. solution: With wide search, due to the small amount of data, do not need to repeat the status of the mark, but in order to avoid the death cycle, the time reset point can only use 1 times, after used to mark it into a vacant space.
#include <stdio.h> #include <string.h> #include <queue> #define MAXN 10int G[MAXN][MAXN], N, m;struct Node {int x, Y, Time, lefttime;} s;const int mov[][2] = {0, 1, 0,-1, 1, 0,-1, 0};void Getmap () {scanf ("%d%d", &n, &m); int i, j;for (i = 1; I <= N ++i) for (j = 1; j <= m; ++j) {scanf ("%d", &g[i][j]), if (g[i][j] = = 2) {s.x = i; S.y = j; S.time = 0; S.lefttime = 6;}}} BOOL Check (int x, int y) {if (x < 1 | | x > N | | y < 1 | | y > m) return false;if (g[x][y] = = 0) return False;retur n true;} int BFS () {int I, j, X, y;std::queue<node> Q; Q.push (S); Node now, Tmp;while (! Q.empty ()) {now = Q.front (); Q.pop (); for (i = 0; i < 4; ++i) {x = mov[i][0] + now.x;y = mov[i][1] + now.y;if (check (x, y)) {tmp = now; ++tmp.time;if (- -tmp.lefttime) {if (g[x][y] = = 4) {tmp.lefttime = 6; G[x][y] = 1;} else if (g[x][y] = = 3) return tmp.time;tmp.x = x; Tmp.y = y; Q.push (TMP);}}} return-1;} void Solve () {printf ("%d\n", BFS ());} int main () {//Freopen ("Stdin.txt", "R", stdin);T t;scanf ("%d", &t), while (t--) {getmap (); Solve ();} return 0;}
solution with Vis mark:
#include <stdio.h> #include <string.h> #include <queue> #define MAXN 10int G[MAXN][MAXN], N, m;struct Node {int x, Y, Time, lefttime;} s;const int mov[][2] = {0, 1, 0,-1, 1, 0,-1, 0};bool vis[maxn][maxn][8];void Getmap () {scanf ("%d%d", &n, &m); int I, j;for (i = 1; I <= n; ++i) for (j = 1; j <= m; ++j) {scanf ("%d", &g[i][j]); if (g[i][j] = = 2) {s.x = i; S.y = j; S.time = 0; S.lefttime = 6;}}} BOOL Check (int x, int y) {if (x < 1 | | x > N | | y < 1 | | y > m) return false;if (g[x][y] = = 0) return False;retur n true;} int BFS () {int I, j, X, y;std::queue<node> Q; Q.push (S); Node now, Tmp;memset (VIS, 0, sizeof (VIS)); Vis[s.x][s.y][s.lefttime] = 1;while (! Q.empty ()) {now = Q.front (); Q.pop (); for (i = 0; i < 4; ++i) {x = mov[i][0] + now.x;y = mov[i][1] + now.y;tmp = now;if (check (x, y) &&!vis[x] [Y] [--tmp.lefttime]) {++tmp.time;if (Tmp.lefttime) {if (g[x][y] = = 4) {tmp.lefttime = 6; G[x][y] = 1;} else if (g[x][y] = = 3) return tmp.time;tmp.x = x; TMP.Y = Y;vis[x][y][tmp.lefttime] = 1; Q.push (TMP);}}} return-1;} void Solve () {printf ("%d\n", BFS ());} int main () {//Freopen ("Stdin.txt", "R", stdin), int t;scanf ("%d", &t), while (t--) {getmap (); Solve ();} return 0;}
HDU1072 Nightmare "BFS"