​ Dungeon Master​

题意:给你一个三维的图,让你找到从S到E最短的路径。

做法:直接套bfs模板,只不过要把数组改为三维的。

代码:

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <limits.h>
using namespace std;
const int N = 30 + 10;
int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}}; //方向数组,6个方向

char a[N][N][N];
int vis[N][N][N];
int l, r, c;
int L, R, C;
int ans;
struct node{
int x;
int y;
int z;
int step;
node(int x, int y, int z, int step):x(x),y(y),z(z),step(step){}
};
queue<node> s;
bool f(int x, int y, int z)
{
if(x < 0 || x == l || y < 0 || y == r || z < 0 || z == c ){
return false;
}
return true;
}
int bfs()
{
while(!s.empty()){
node cnt = s.front();
s.pop();
if (a[cnt.x][cnt.y][cnt.z] == 'E')
return cnt.step;
for (int i = 0; i < 6; i++){
int tx = cnt.x + dir[i][0];
int ty = cnt.y + dir[i][1];
int tz = cnt.z + dir[i][2];
if (f(tx, ty, tz) && !vis[tx][ty][tz] && a[tx][ty][tz] != '#'){
vis[tx][ty][tz] = 1;
s.push(node(tx, ty, tz, cnt.step + 1));
}
}
}
return 0;
}
int main()
{
while(cin >> l >> r >> c){
if (l == 0)
break;
for (int i = 0; i < l; i++){
for (int j = 0; j < r; j++){
for (int k = 0; k < c; k++){
cin >> a[i][j][k];
if (a[i][j][k] == 'S'){
L = i;
R = j;
C = k;
}
}
}
char cc = getchar();
}
ans = 0;
while(!s.empty()){
s.pop();
}
memset(vis, 0, sizeof(vis));
s.push(node(L, R, C, 0));
ans = bfs();
if (ans == 0){
printf("Trapped!\n");
}else{
printf("Escaped in %d minute(s).\n", ans);
}
}
return 0;
}


// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);