标签:down dia tail blank highlight ... 组成 move single
题目描述
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
输入
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#‘ and empty cells are represented by a ‘.‘. Your starting position is indicated by ‘S‘ and the exit by the letter ‘E‘. There‘s a single blank line after each level. Input is terminated by three zeroes for L, R and C.
输出
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
题目大意
你被困在一个3D地牢,需要找到最快的出路!地牢是由单位立方体组成,可以或不可以用岩石填充。将一个单位向北,南,东,西,上或下移动需要一分钟。你不能对角地移动,迷宫被四周的坚实岩石包围。 是逃脱可能吗?如果是,需要多长时间?
这是三维的bfs,只需要在二维bfs基础上增加一维就可以轻松解决,遇到的坑是定义队列的时候需要足够大,不然WA,在这里队列长度定义为30 000
1 #include <cstring> 2 #include <cstdio> 3 #include <iostream> 4 using namespace std; 5 struct node { 6 int x; 7 int y; 8 int z; 9 }; 10 int main(){ 11 char map[35][35][35]; 12 struct node que[30000]; 13 int l, r, c; 14 int i, j, k, p; 15 int x, y, z, tx, ty, tz, book[35][35][35] = {0}; 16 int sum[30000]; 17 int head, tail; 18 int xx[6] = {-1,1,0,0,0,0}; 19 int yy[6] = {0,0,-1,1,0,0}; 20 int zz[6] = {0,0,0,0,-1,1}; 21 22 while (scanf("%d %d %d", &l, &r, &c) != EOF){ 23 if(l==0 && r==0 && c==0) 24 break; 25 //读入3d地图 26 for (i = 0; i < l; i++) 27 for (j = 0; j < r; j++) 28 for (k = 0; k < c; k++){ 29 cin >> map[i][j][k]; 30 if (map[i][j][k] == ‘S‘){ 31 x = i; 32 y = j; 33 z = k; 34 } 35 } 36 memset(que, 0, sizeof(que)); 37 memset(sum, 0, sizeof(sum)); 38 memset(book, 0, sizeof(book)); 39 int flag = 0; 40 //队列初始化 41 que[0].x = x; 42 que[0].y = y; 43 que[0].z = z; 44 head = 0; 45 tail = 0; 46 47 while(head <= tail){ 48 for(p = 0; p < 6; p++){ 49 tx = que[head].x + xx[p]; 50 ty = que[head].y + yy[p]; 51 tz = que[head].z + zz[p]; 52 53 if (tx < 0 || tx >= l || ty < 0 || ty >= r || tz < 0 || tz >= c) 54 continue; 55 if ((map[tx][ty][tz] == ‘.‘ || map[tx][ty][tz] == ‘E‘) && book[tx][ty][tz] == 0){ 56 book[tx][ty][tz] = 1; 57 tail++; 58 que[tail].x = tx; 59 que[tail].y = ty; 60 que[tail].z = tz; 61 //重要,此处的时间应该为出队节点加1 62 sum[tail] = sum[head] + 1; 63 } 64 if (map[tx][ty][tz] == ‘E‘){ 65 flag = 1; 66 break; 67 } 68 } 69 if (flag==1) 70 break; 71 head++; 72 } 73 if (sum[tail]) 74 printf("Escaped in %d minute(s).\n", sum[tail]); 75 else 76 printf("Trapped!\n"); 77 } 78 return 0; 79 }
标签:down dia tail blank highlight ... 组成 move single
原文地址:http://www.cnblogs.com/huangjiaming/p/6501070.html