Dungeon Master POJ - 2251
题目链接:https://vjudge.net/problem/POJ-2251
题意:简单的三维地图
思路:直接上代码。。。
#include <iostream>
#include <string.h>
#include<queue>
#include <algorithm>
using namespace std;
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
const int N = 40;
int mv_x[] = { 0, 0, 0, 0, 1, -1 };
int mv_y[] = { 0, 0, 1, -1, 0, 0 };
int mv_z[] = { 1, -1, 0, 0, 0, 0 };
char mp[N][N][N];
bool vis[N][N][N];
int sx, sy, sz;//入口
int ex, ey, ez;//出口
int xx,yy,zz,ans;
struct node{
int x, y, z, c;
};
void init(){
rep(i, 1, zz) rep(j, 1, xx) rep(z, 1, yy){
vis[i][j][z] = false;
}
}
void input(){
rep(i, 1, zz) rep(j, 1, xx) rep(z, 1, yy){
cin >> mp[i][j][z];
if (mp[i][j][z] == 'S'){
sx = j; sy = z; sz = i;
}
else if (mp[i][j][z] == 'E'){
ex = j; ey = z; ez = i;
}
}
}
inline bool check(int x, int y, int z){
return x >= 1 && x <= xx
&& y >= 1 && y <= yy
&& z >= 1 && z <= zz;
}
bool bfs(){//true为能出去 false不能出去
queue<node> que;
que.push(node{ sx, sy, sz, 0 });
vis[sz][sx][sy] = true;
while (!que.empty()){
node tmp = que.front();
que.pop();
rep(p, 0, 5){
int dx = tmp.x + mv_x[p];
int dy = tmp.y + mv_y[p];
int dz = tmp.z + mv_z[p];
if (check(dx, dy, dz) && mp[dz][dx][dy] != '#' && !vis[dz][dx][dy]){
if (dx == ex && dy == ey && dz == ez){//到了出口
ans = tmp.c + 1;
return true;
}
vis[dz][dx][dy] = true;
que.push(node{ dx, dy, dz ,tmp.c + 1});
}
}
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> zz >> xx >> yy){
if (zz == 0) break;
init();
input();
if (bfs()) cout << "Escaped in " << ans << " minute(s)." << endl;
else cout << "Trapped!" << endl;
}
return 0;
}
本文介绍了一种解决三维迷宫问题的算法,通过广度优先搜索(BFS)找到从入口到出口的最短路径。文章详细展示了算法的实现过程,包括地图数据结构的定义、边界检查、状态记录及搜索策略。
260

被折叠的 条评论
为什么被折叠?



