Escape time limit: 1000 MS | memory limit: 65535 kb difficulty: 4
-
Description
-
Once upon a time there was a knight named hck. In order to save our beautiful princess, we had to dive into the old nest of the devil. It was a hero. But the hero is not so good. This poor baby was captured by the devil, suffering, and life and death. One day the devil went out for a date, which was a rare opportunity to escape. Your current task is to determine whether the child of the failed hero can escape from the castle of the Devil before the devil returns, successfully escape, and finally marry our beautiful princess.
The Devil lives in a castle. The castle is a cube of a * B * C, which can be expressed as a matrix of B * C. At first, hck was shut down (0, 0, 0) location, exit the castle door in (A-1, B-1, C-1) location, now know that the devil will return to the castle in T minutes, each minute, the hck can go from one coordinate to one of the adjacent six coordinates. now, I will give you a map of the castle. Please calculate whether the hck can leave the castle before the devil returns, if the devil just came back at the exit, it would be a success). If you can, please output how many minutes to leave. If not, output-1.
The top left corner of the 0th block in the input data is the place where hck is shut down, and the bottom right corner of the A-1 block is the exit of the castle. Move each layer in the direction of the Red Arrow to form the entire castle.
-
Input
-
The first line of the input data is a positive integer k, indicating the number of test data. the first row of each group of test data is four positive integers A, B, C, and T (1 <= A, B, C <= 50,1 <= T <= 1000 ), they represent the size of the castle and the time when the devil came back.
Then there is a piece of input data (first 0th, then 1st, 2nd ......), each input data has B rows, and each row has c positive integers, representing the layout of the maze. 0 represents the path, and 1 represents the wall.
(If you are not clear about the input description, you can refer to the above description of the maze, which represents the maze)
-
Output
-
For each group of test data, if the hck can leave the castle before the devil returns, output the minimum number of minutes; otherwise, output-1.
-
Sample Input
-
23 2 2 100 10 01 11 00 00 13 3 4 200 1 1 10 0 1 10 1 1 11 1 1 11 0 0 10 1 1 10 0 0 00 1 1 00 1 1 0
-
Sample output
-
-111
Simple 3D search
Code:
#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>using namespace std;#define M 55struct node{ int x, y, z; int step;};node st, en;int map[M][M][M];bool vis[M][M][M];int a, b, c, t;const int dx[] = {0, 0, 0, 0, 1, -1};const int dy[] = {0, 0, 1, -1, 0, 0};const int dz[] = {1, -1, 0, 0, 0, 0};int limit(node s){ return (s.x>=0&&s.x<a&&s.y>=0&&s.y<b&&s.z>=0&&s.z<c&&map[s.x][s.y][s.z] == 0);}int match(node a, node b){ return (a.x==b.x&&a.y==b.y&&a.z==b.z);}int bfs(){ queue<node> q; int i, res = 0x3f3f3f3f; vis[0][0][0] = 1; q.push(st); while(!q.empty()){ node cur = q.front(); q.pop(); for(i = 0; i < 6; i ++){ node temp = cur; temp.x += dx[i]; temp.y+=dy[i]; temp.z += dz[i]; temp.step++; if(match(temp, en)){ res = min(res, temp.step); continue; } if(limit(temp)&&!vis[temp.x][temp.y][temp.z]){ q.push(temp); vis[temp.x][temp.y][temp.z] = 1; } } } if(res > t) return -1; return res;}int main(){ int T; scanf("%d", &T); while(T --){ scanf("%d%d%d%d", &a, &b, &c, &t); memset(vis, 0, sizeof(vis)); memset(map, -1, sizeof(map)); st.x = st.y = st.z = st.step = 0; en.x = a-1, en.y = b-1, en.z = c-1; int i, j, k; for(i = 0; i < a; i ++) for(j = 0; j < b; j ++) for(k = 0; k < c; k ++) scanf("%d", &map[i][j][k]); if(match(st, en)){ printf("0\n"); continue; } if(map[a-1][b-1][c-1] == 1){ printf("-1\n"); continue; } int ans = bfs(); printf("%d\n", ans); } return 0;}
Nyoj 523 escape [BFS]