每日一题 POJ-2251 三维迷宫

#include
#include
#include

using namespace std;
const int maxz = 31;
int mov[6][3] = { {1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1} };
int L, R,C;
char map[maxz][maxz][maxz];
int sx, sy, sz, ex, ey, ez;
struct node {
	int x, y, z;
	int step;
}S,E;

int is_right(int x, int y, int z) {
	return x >= 0 && x < L && y >= 0 && y < R && z >= 0 && z < C && map[x][y][z] != '#';
}

int  BFS() {
	queue q;
	q.push(S);
	while (!q.empty()) {
		node now, next;
		now = q.front();
		q.pop();
		
		for (int i = 0; i < 6; i++) {
			next.x = now.x + mov[i][0];
			next.y = now.y + mov[i][1];
			next.z = now.z + mov[i][2];
			if(is_right(next.x, next.y, next.z)) {
				next.step = now.step + 1;
				if (next.x == E.x && next.y == E.y && next.z == E.z) {
					E.step = next.step;
					return next.step;
				}
				q.push(next);
			}
			
		} 
	}
	return -1;
}

int main() {
	while (cin >> L >> R >> C) {
		memset(map, 0, sizeof(map));
		for (int i = 0; i < L; i++) {
			for (int j = 0; j < R; j++) {
				for (int k = 0; k < C; k++) {
					cin >> map[i][j][k];
					if (map[i][j][k] == 'S') {
						S.x = i;
						S.y = j;
						S.z = k;
						S.step = 0;
					}
					if (map[i][j][k] == 'E') {
						E.x = i;
						E.y= j;
						E.z= k;
					}
				}
			}
		}
		if (BFS() == -1)
			printf("Trapped!\n");
		else
			printf("Escaped in %d minute(s).\n", E.step);

	}
	return 0;
}

 

你可能感兴趣的:(算法学习,kuangbin带你飞)