原题地址:
http://acm.hit.edu.cn/hojx/showproblem/1448/
这题没什么特别的,区别就在于这个是多层的,有了思路特别好写的~;
/*
Judge Status: Accepted
PID : 1448
Time : 0.01s
Memory : 2184K
Language : G++
Code Len. : 2548
Author : Belieber_J
*/
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int l,r,c,i,j,k,sl,sr,sc,tl,tr,tc;
char s[40][40][40];
int vis[40][40][40];
struct Node
{
int l,r,c,step;//! level row column
};
int dl[6] = {0,0,0,0,1,-1};
int dr[6] = {1,-1,0,0,0,0};
int dc[6] = {0,0,1,-1,0,0};
int direct;
queue<Node>q;
int bfs(int sl,int sr,int sc)
{
while (!q.empty()) q.pop();
memset(vis,0,sizeof(vis));
Node st;
st.l = sl;
st.r = sr;
st.c = sc;
st.step = 0;
vis[sl][sr][sc] = 1;
q.push(st);
while (!q.empty())
{
Node cur = q.front();
q.pop();
for (direct = 0; direct < 6; direct++)
{
Node nx;
nx.l = cur.l + dl[direct];
nx.r = cur.r + dr[direct];
nx.c = cur.c + dc[direct];
nx.step = cur.step + 1;
if (nx.l == tl && nx.r == tr && nx.c == tc) return nx.step;
if (nx.l < 0 || nx.l >= l || nx.r < 0 || nx.r >= r || nx.c < 0 || nx.c >= c) continue;
else if(vis[nx.l][nx.r][nx.c] || s[nx.l][nx.r][nx.c] == '#') continue;
else
{
vis[nx.l][nx.r][nx.c] = 1;
q.push(nx);
}
}
}
return -1;
}
int main()
{
int cou;
while (scanf("%d%d%d",&l,&r,&c) == 3 && (l || r || c))
{
for (i = 0; i < l; i++)
{
for (j = 0; j < r; j++)
{
scanf("%s",s[i][j]);
for (k = 0; k < c; k++)
{
if (s[i][j][k] == 'S')
{
sl = i;
sr = j;
sc = k;
}
if (s[i][j][k] == 'E')
{
tl = i;
tr = j;
tc = k;
}
}
}
}
cou = bfs(sl,sr,sc);
if (cou == -1)
printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",cou);
}
return 0;
}