Dungeon Master

来源:互联网 发布:大数据比赛 编辑:程序博客网 时间:2024/06/05 11:22

题目链接: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;}


原创粉丝点击