每日一题 POJ-2251 三维迷宫

#include<iostream>
#include<string>
#include<queue>

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<node> 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值