标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21242 | Accepted: 8265 |
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!
三维BFS 注意xyz别弄混了
/* *********************************************** Author :pk29 Created Time :2015/8/19 18:40:40 File Name :4.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 10000+10 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; struct node{ int x,y,z; int dist; }; bool cmp(int a,int b){ return a>b; } int l,r,c; char mp[50][50][50]; int vis[50][50][50]; int sx,sy,sz,ex,ey,ez; int dir[6][3]={0,1,0,0,0,1,0,0,-1,1,0,0,-1,0,0,0,-1,0}; void bfs(){ node u,v; u.x=sx,u.y=sy,u.z=sz, u.dist=0; queue<node>q; q.push(u); int mark=0; vis[u.x][u.y][u.z]=1; while(!q.empty()){ u=q.front(),q.pop(); if(u.x==ex&&u.y==ey&&u.z==ez){ mark=1;printf("Escaped in %d minute(s).\n",u.dist);break; } for(int i=0;i<6;i++){ int nx=u.x+dir[i][0]; int ny=u.y+dir[i][1]; int nz=u.z+dir[i][2]; if(!vis[nx][ny][nz]&&mp[nx][ny][nz]!=‘#‘&&nx>=1&&ny>=1&&nz>=1&&nx<=r&&ny<=c&&nz<=l){ v.x=nx,v.y=ny,v.z=nz; v.dist=u.dist+1; q.push(v); vis[nx][ny][nz]=1; } } } if(!mark)printf("Trapped!\n"); } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); while(cin>>l>>r>>c){ if(l==0&&c==0&&r==0)break; for(int i=1;i<=l;i++) for(int j=1;j<=r;j++) for(int k=1;k<=c;k++){ cin>>mp[j][k][i]; if(mp[j][k][i]==‘S‘)sx=j,sy=k,sz=i; if(mp[j][k][i]==‘E‘)ex=j,ey=k,ez=i; } cle(vis); //cout<<sx<<sy<<sz<<endl; //cout<<ex<<ey<<ez<<endl; bfs(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/pk28/p/4743157.html