Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17264 | Accepted: 6722 |
Description
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
Source
题意:
给你n多层楼,求S到E的最小时间。
题解:
bfs,只不过是加了上下两个搜索方向。
CODE:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<algorithm> #include<cstdlib> #include<set> #include<queue> #include<stack> #include<vector> #include<map> #define N 100010 #define Mod 10000007 #define lson l,mid,idx<<1 #define rson mid+1,r,idx<<1|1 #define lc idx<<1 #define rc idx<<1|1 const double EPS = 1e-11; const double PI = acos(-1.0); typedef long long ll; const int INF=1000010; using namespace std; struct node { int x,y,z; int num; } ; node s,e; int n,m,c,ans; char mp[33][33][33]; bool vis[33][33][33]; queue<node>a; int x[6]= {-1,0,1,0,0,0}; int y[6]= {0,1,0,-1,0,0}; int z[6]= {0,0,0,0,1,-1}; int bfs() { memset(vis,0,sizeof vis); a.push(s); node t; while(a.size()) { t=a.front(); node tt; a.pop(); if(t.x==e.x&&t.y==e.y&&t.z==e.z) return t.num; for(int i=0; i<6; i++) { tt.x=x[i]+t.x; tt.y=y[i]+t.y; tt.z=z[i]+t.z; if(tt.x>=0&&tt.x<c&&tt.y>=0&&tt.y<n&&tt.z<m&&tt.z>=0&&!vis[tt.x][tt.y][tt.z]&&mp[tt.x][tt.y][tt.z]!='#') { tt.num=t.num+1; a.push(tt); vis[tt.x][tt.y][tt.z]=1; } } } return -1; } int main() { //freopen("test.in","r",stdin); while(cin>>c>>n>>m) { if(n==0&&m==0&&c==0) break; getchar(); for(int i=0; i<c; i++) { for(int j=0; j<n; j++) { for(int k=0; k<m; k++) { scanf(" %c",&mp[i][j][k]); if(mp[i][j][k]=='S') { s.x=i,s.y=j,s.z=k,s.num=0; } if(mp[i][j][k]=='E') { e.x=i,e.y=j,e.z=k; } } getchar(); } } while(a.size()) a.pop(); int num=bfs(); if(num!=-1) printf("Escaped in %d minute(s).\n",num); else printf("Trapped!\n"); } return 0; }
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/42342029