UVA - 532 Dungeon Master

来源:互联网 发布:《算法》读书笔记 编辑:程序博客网 时间:2024/06/12 00:48

题目大意:给你一个三维迷宫,起点是“S”,终点是“E”,求是否能从S走到E,如果可以的话,请输出最少的时间

解题思路:用六个全局变量来记录起点和终点的各个坐标,然后去BFS搜索,再判断所在的点是否符合

#include<cstdio>#include<cstring>#include<queue>using namespace std;int move[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{-1,0,0},{1,0,0}};int cur[200][200][200];int sx,sy,sz,ex,ey,ez;int L,R,C;struct step{int x, y,z,number;};void BFS(int x, int y, int z) {queue<step> q;step t,p;t.x = x;t.y = y;t.z = z;t.number = 0;q.push(t);while(!q.empty()) {t = q.front();q.pop();if(t.x == ex && t.y == ey && t.z == ez) {printf("Escaped in %d minute(s).\n",t.number);return ;}int X,Y,Z;for(int i = 0; i < 6; i++) {X = t.x + move[i][0];Y = t.y + move[i][1];Z = t.z + move[i][2];if(cur[Z][Y][X] && Z >= 1 && Z <=L && Y >= 1 && Y <= R && X >= 1 &&  X <= C) {p.x = X ;   p.y = Y;   p.z = Z; p.number = t.number + 1;cur[Z][Y][X] = 0;q.push(p);}}}printf("Trapped!\n");}int main() {char c;char str[100];while(scanf("%d%d%d",&L,&R,&C)!= EOF){if(L == 0 && R == 0 && C == 0)break;getchar();memset(cur,0,sizeof(cur));for(int i = 1; i <= L; i++) {for(int j = 1; j <= R; j++) {gets(str);for(int k = 1; k <= C; k++) {if(str[k-1]== 'S') {sx = k;sy = j;sz = i;cur[i][j][k] = 1;}else if(str[k-1] == 'E') {ex = k;ey = j;ez = i;cur[i][j][k] = 1;}else if(str[k-1]== '.') {cur[i][j][k] = 1;}else cur[i][j][k] = 0;}}gets(str);}BFS(sx,sy,sz);}return 0;}


0 0
原创粉丝点击