题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=940
分析:三维其实就是六个方向地搜索,思维清晰且细心点,很快就AC了。
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <cstdlib> #include <vector> #include <set> #include <map> #define LL long long #define mod 1000000007 #define inf 0x3f3f3f3f #define N 100010 using namespace std; struct node { int x,y,z,step; }; struct move { int l,r,c; }mo[6]={ {0,0,1},{0,0,-1},{0,1,0}, {0,-1,0},{1,0,0},{-1,0,0} }; char s[35][35][35]; int vis[35][35][35]; int L,R,C; node make_node(int a,int b,int c,int x) { node temp; temp.x=a,temp.y=b; temp.z=c;temp.step=x; return temp; } queue<node>que; bool judge(int a,int b,int c) { return a>=0&&a<L&&b>=0&&b<R&&c>=0&&c<C&&s[a][b][c]!='#'; } int bfs(int x,int y,int z) { memset(vis,0,sizeof(vis)); while(!que.empty())que.pop(); que.push(make_node(x,y,z,0)); vis[x][y][z]=1; while(!que.empty()) { node now=que.front();que.pop(); for(int i=0;i<6;i++) { int a=now.x+mo[i].l,b=now.y+mo[i].r,c=now.z+mo[i].c,x=now.step; if(!vis[a][b][c]&&judge(a,b,c)) { que.push(make_node(a,b,c,x+1)); vis[a][b][c]=1; if(s[a][b][c]=='E') { return x+1; } } } } return -1; } int main() { int ans,x,y,z; while(scanf("%d%d%d",&L,&R,&C)&&L) { for(int i=0;i<L;i++) for(int j=0;j<R;j++) { scanf("%s",s[i][j]); for(int k=0;k<C;k++) { if(s[i][j][k]=='S') { x=i;y=j;z=k; } } } int res=bfs(x,y,z); if(res!=-1)printf("Escaped in %d minute(s). ",res); else puts("Trapped!"); } }