题目大意
三维的迷宫题目,其中用‘.’表示空地,‘#’表示障碍物,‘S’表示起点,‘E’表示终点,求从起点到终点的最小移动次数。
广搜——与二维广搜相似,只不过换为三维:6个方向
map[][][]来标记 路障,起点s,终点e。
v[][][],三维标记数组
使用队列
易错点:
(卡了好久找不到原因——浪费好长时间。)
main()输入map[][][],x-l ,y-r ,z-c 与 dfs()中一一对应,
否则细节很难找出bug在哪。
//三维——三维广搜——6个方向
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 35
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG(i) pf("Hi %d\n", i)
typedef long long ll;
using namespace std;
struct node{
int x, y, z;
int step;
};
node s, e;
int mapn[MAXN][MAXN][MAXN];
int v[MAXN][MAXN][MAXN];
int d[6][3] = {{0, 0, 1},{0, 1, 0}, {1, 0, 0}, {0, 0, -1}, {0, -1, 0}, {-1, 0, 0}}; //六个方向——处理办法
int l, r, c;
queue<node> p;
int bfs();
int main(){
char ch;
while(scanf("%d%d%d", &l, &r, &c)== 3 && l && r && c){
memset(mapn,0,sizeof(mapn));
memset(v,0,sizeof(v));
for(int i = 1;i <= l;i++){ //ijk对应xyz!!!
for(int j = 1;j <= r;j++){
for(int k = 1;k <= c;k++){
char c;
cin>>c;
if(c == 'S'){ //处理地图,标识障碍物,取得起点和终点
s.x = i, s.y = j, s.z = k;
}else if(c == 'E'){
mapn[i][j][k] = 1;
e.x = i, e.y = j, e.z = k;
}else if(c == '.'){
mapn[i][j][k] = 1;
}
}
}
}
int ans = bfs();
if(ans) cout << "Escaped in " << ans <<" minute(s)." << endl;
else cout << "Trapped!" << endl;
}
return 0;
}
int bfs(){
while(!p.empty()){
p.pop();
}
//DBG(1);
v[s.x][s.y][s.z] = 1;
p.push(s); //vector:push_back(); queue、stack:push()
while(!p.empty()){
node head = p.front();
p.pop();
if(head.x == e.x && head.y == e.y && head.z == e.z){ //先判断是否到达终点
return head.step;
}
for(int i = 0; i < 6; i++){
int x = head.x+d[i][0];
int y = head.y+d[i][1];
int z = head.z+d[i][2];
int step = head.step + 1; //后一步比前一步加一
//x-l y-r z-c错因::一一对应:main函数中输入也是
if( x<1 || y<1 || z<1 || x> l || y>r || z>c || !mapn[x][y][z] ||v[x][y][z]){
continue;
}
v[x][y][z] = 1;
node next;
next.x = x; next.y = y; next.z = z; next.step = step;
p.push(next);
}
}
return 0;
}