题目链接: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 = ;
int mv_x[] = { , , , , , - };
int mv_y[] = { , , , -, , };
int mv_z[] = { , -, , , , };
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, , zz) rep(j, , xx) rep(z, , yy){
vis[i][j][z] = false;
}
} void input(){ rep(i, , zz) rep(j, , xx) rep(z, , 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 >= && x <= xx
&& y >= && y <= yy
&& z >= && z <= zz;
} bool bfs(){//true为能出去 false不能出去 queue<node> que;
que.push(node{ sx, sy, sz, }); vis[sz][sx][sy] = true; while (!que.empty()){ node tmp = que.front();
que.pop(); rep(p, , ){
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 + ;
return true;
} vis[dz][dx][dy] = true;
que.push(node{ dx, dy, dz ,tmp.c + });
}
}
} return false;
} int main(){ ios::sync_with_stdio(false);
cin.tie(); while (cin >> zz >> xx >> yy){
if (zz == ) break; init();
input(); if (bfs()) cout << "Escaped in " << ans << " minute(s)." << endl;
else cout << "Trapped!" << endl;
} return ;
}