一开始用DFS怎么也没过,后来用了BFS终于国了,以后求最短路就用BFS;
#include<stdio.h>
#include<string.h>
struct list
{int l;int r;int c;int depth;
}first,last,qun[30000],pan;
int min;
int map[50][50][50];
int get(char c)
{if(c=='.')return 1;if(c=='S')return 2;if(c=='E')return 3;return 0;
}
int bfs(int x,int y,int z)
{int head,tail;int visit[50][50][50];memset(visit,1,sizeof(visit));head=0;tail=0;qun[0].l=x;qun[0].r=y;qun[0].c=z;qun[0].depth=1;tail++;visit[x][y][z]=0;while(tail>head){pan=qun[head];head++;if(pan.l==last.l&&pan.r==last.r&&pan.c==last.c){min=pan.depth;return 1;}if(map[pan.l-1][pan.r][pan.c]&&visit[pan.l-1][pan.r][pan.c]){visit[pan.l-1][pan.r][pan.c]=0;qun[tail].l=pan.l-1;qun[tail].r=pan.r;qun[tail].c=pan.c;qun[tail].depth=pan.depth+1;tail++;}if(map[pan.l][pan.r-1][pan.c]&&visit[pan.l][pan.r-1][pan.c]){visit[pan.l][pan.r-1][pan.c]=0;qun[tail].l=pan.l;qun[tail].r=pan.r-1;qun[tail].c=pan.c;qun[tail].depth=pan.depth+1;tail++;}if(map[pan.l][pan.r][pan.c-1]&&visit[pan.l][pan.r][pan.c-1]){visit[pan.l][pan.r][pan.c-1]=0;qun[tail].l=pan.l;qun[tail].r=pan.r;qun[tail].c=pan.c-1;qun[tail].depth=pan.depth+1;tail++;}if(map[pan.l+1][pan.r][pan.c]&&visit[pan.l+1][pan.r][pan.c]){visit[pan.l+1][pan.r][pan.c]=0;qun[tail].l=pan.l+1;qun[tail].r=pan.r;qun[tail].c=pan.c;qun[tail].depth=pan.depth+1;tail++;}if(map[pan.l][pan.r+1][pan.c]&&visit[pan.l][pan.r+1][pan.c]){visit[pan.l][pan.r+1][pan.c]=0;qun[tail].l=pan.l;qun[tail].r=pan.r+1;qun[tail].c=pan.c;qun[tail].depth=pan.depth+1;tail++;}if(map[pan.l][pan.r][pan.c+1]&&visit[pan.l][pan.r][pan.c+1]){visit[pan.l][pan.r][pan.c+1]=0;qun[tail].l=pan.l;qun[tail].r=pan.r;qun[tail].c=pan.c+1;qun[tail].depth=pan.depth+1;tail++;}}return 0;
}
int main()
{int l,r,c,t,i,j,s;char str[50];while(scanf("%d%d%d%*c",&l,&r,&c)&&(l||r||c)){memset(map,0,sizeof(map));for(t=1;t<=l;t++){for(i=1;i<=r;i++){gets(str);for(j=1;j<=c;j++){s=get(str[j-1]);if(s)map[t][i][j]=1;if(s==2){first.l=t;first.r=i;first.c=j;}if(s==3){last.l=t;last.r=i;last.c=j;}}}if(t!=l)getchar();}if(bfs(first.l,first.r,first.c)){printf("Escaped in %d minute(s).\n",min-1);}else{printf("Trapped!\n");}}return 0;
}