Dungeon Master

题目链接:http://poj.org/problem?id=2251


题意:三维的走迷宫,bfs搜索就行。 做的时候感觉自己还不是很熟练,控制它走的步数时有点问题, 本来很轻松就解决的问题还是花了点时间。


#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 35;

char maze[maxn][maxn][maxn];
struct node {
	int x, y, z, step;
}; 
int dx[] = {1, -1, 0, 0, 0, 0};
int dy[] = {0, 0, 1, -1, 0, 0};
int dz[] = {0, 0, 0, 0, 1, -1};
bool judge[maxn][maxn][maxn];
int length, heigh, width;
void bfs(node start) {
	memset(judge, false, sizeof(judge));
	queue<node>head;
	node temp = start;
	head.push(temp);
	judge[temp.x][temp.y][temp.z] = true;
	while (!head.empty()) {
		node next = head.front();
		head.pop();
		if (maze[next.x][next.y][next.z] == 'E') {
			printf("Escaped in %d minute(s).\n", next.step);
			return;
		}
		temp.step = next.step + 1;
		for (int i = 0; i < 6; i++) {
			temp.x = next.x + dx[i];
			temp.y = next.y + dy[i];
			temp.z = next.z + dz[i];			
			if (maze[temp.x][temp.y][temp.z] == '.' && !judge[temp.x][temp.y][temp.z] 
				&& temp.x >= 0 && temp.x < length && temp.y >= 0 && 
				temp.y < width && temp.z >= 0 && temp.z < heigh || maze[temp.x][temp.y][temp.z] == 'E') {
				head.push(temp);
				judge[temp.x][temp.y][temp.z] = true;
			}
		}
	}
	cout << "Trapped!" << endl;
}
int main()
{
	node start, end;
	while (cin >> length >> width >> heigh && (length && width && heigh)) {
		for (int i = 0; i < length; i++)
			for (int j = 0; j < width; j++)
				for (int k = 0; k < heigh; k++) {
					cin >> maze[i][j][k];
					if (maze[i][j][k] == 'S' ) {
						start.x = i;
						start.y = j;
						start.z = k;
						start.step = 0;
					} 
				}
		bfs(start);	
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值