标签:des style http color os strong io for
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16437 | Accepted: 6386 |
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
bfs过了,dfs超时了
AC代码:
#include<queue> #include<algorithm> #include<iostream> #include<cstring> using namespace std; char G[33][33][33]; int g[33][33][33]; struct Node{ int l,r,c; int t; Node(int L,int R,int C,int T):l(L),r(R),c(C),t(T){} }; struct Node1{ int l,r,c; }b,e; queue <Node> qu; int main(){ int l,r,c; int sucess; while(cin>>l>>r>>c){ if(l==r && l==c && c==0) break; for(int k=1;k<=l;k++) for(int i=1;i<=r;i++) for(int j=1;j<=c;j++){ cin>>G[k][i][j]; if(G[k][i][j]=='S'){ b.l=k; b.r=i; b.c=j; } else if(G[k][i][j]=='E'){ e.l=k; e.r=i; e.c=j; } } while(!qu.empty()) qu.pop(); qu.push(Node(b.l,b.r,b.c,0)); sucess=0; memset(g,0,sizeof(g)); g[b.l][b.r][b.c]=1; while(!qu.empty()){ Node tmp=qu.front(); qu.pop(); if(tmp.l==e.l && tmp.r==e.r && tmp.c==e.c){ sucess=1; cout<<"Escaped in "<<tmp.t<<" minute(s)."<<endl; break; } if(tmp.l+1<=l && !g[tmp.l+1][tmp.r][tmp.c] && (G[tmp.l+1][tmp.r][tmp.c]=='.' || G[tmp.l+1][tmp.r][tmp.c]=='E')){ //up qu.push(Node(tmp.l+1,tmp.r,tmp.c,tmp.t+1)); g[tmp.l+1][tmp.r][tmp.c]=1; } if(tmp.l-1>=1 && !g[tmp.l-1][tmp.r][tmp.c] && (G[tmp.l-1][tmp.r][tmp.c]=='.' || G[tmp.l-1][tmp.r][tmp.c]=='E')){ //down qu.push(Node(tmp.l-1,tmp.r,tmp.c,tmp.t+1)); g[tmp.l-1][tmp.r][tmp.c]=1; } if(tmp.r+1<=r && !g[tmp.l][tmp.r+1][tmp.c] && (G[tmp.l][tmp.r+1][tmp.c]=='.' || G[tmp.l][tmp.r+1][tmp.c]=='E')){ //east qu.push(Node(tmp.l,tmp.r+1,tmp.c,tmp.t+1)); g[tmp.l][tmp.r+1][tmp.c]=1; } if(tmp.r-1>=1 && !g[tmp.l][tmp.r-1][tmp.c] && (G[tmp.l][tmp.r-1][tmp.c]=='.' || G[tmp.l][tmp.r-1][tmp.c]=='E')){ //weat qu.push(Node(tmp.l,tmp.r-1,tmp.c,tmp.t+1)); g[tmp.l][tmp.r-1][tmp.c]=1; } if(tmp.c+1<=c && !g[tmp.l][tmp.r][tmp.c+1] && (G[tmp.l][tmp.r][tmp.c+1]=='.' || G[tmp.l][tmp.r][tmp.c+1]=='E')){ //south qu.push(Node(tmp.l,tmp.r,tmp.c+1,tmp.t+1)); g[tmp.l][tmp.r][tmp.c+1]=1; } if(tmp.c-1>=1 && !g[tmp.l][tmp.r][tmp.c-1] && (G[tmp.l][tmp.r][tmp.c-1]=='.' || G[tmp.l][tmp.r][tmp.c-1]=='E')){ //north qu.push(Node(tmp.l,tmp.r,tmp.c-1,tmp.t+1)); g[tmp.l][tmp.r][tmp.c-1]=1; } } if(!sucess) cout<<"Trapped!"<<endl; } return 0; }
#include<algorithm> #include<iostream> #include<cstring> using namespace std; struct Node1{ int l,r,c; }b; int ans; int l,r,c; int sucess; char G[33][33][33]; int g[33][33][33]; void dfs(int x,int y,int z,int t){ g[x][y][z]=1; if(G[x][y][z]=='E'){ sucess=1; if(t<ans){ ans=t; } return ; } if(x+1<=l && !g[x+1][y][z] && (G[x+1][y][z]=='E' || G[x+1][y][z]=='.')){ dfs(x+1,y,z,t+1); g[x+1][y][z]=0; } if(x-1>=1 && !g[x-1][y][z] && (G[x-1][y][z]=='E' || G[x-1][y][z]=='.')){ dfs(x-1,y,z,t+1); g[x-1][y][z]=0; } if(y+1<=r && !g[x][y+1][z] && (G[x][y+1][z]=='E' || G[x][y+1][z]=='.')){ dfs(x,y+1,z,t+1); g[x][y+1][z]=0; } if(y-1>=1 && !g[x][y-1][z] && (G[x][y-1][z]=='E' || G[x][y-1][z]=='.')){ dfs(x,y-1,z,t+1); g[x][y-1][z]=0; } if(z+1<=c && !g[x][y][z+1] && (G[x][y][z+1]=='E' || G[x][y][z+1]=='.')){ dfs(x,y,z+1,t+1); g[x][y][z+1]=0; } if(z-1>=1 && !g[x][y][z-1] && (G[x][y][z-1]=='E' || G[x][y][z-1]=='.')){ dfs(x,y,z-1,t+1); g[x][y][z-1]=0; } } int main(){ while(cin>>l>>r>>c){ if(l==r && l==c && c==0) break; for(int k=1;k<=l;k++) for(int i=1;i<=r;i++) for(int j=1;j<=c;j++){ cin>>G[k][i][j]; if(G[k][i][j]=='S'){ b.l=k; b.r=i; b.c=j; } } ans=999999; sucess=0; memset(g,0,sizeof(g)); dfs(b.l,b.r,b.c,0); if(!sucess) cout<<"Trapped!"<<endl; else cout<<"Escaped in "<<ans<<" minute(s)."<<endl; } return 0; }
标签:des style http color os strong io for
原文地址:http://blog.csdn.net/my_acm/article/details/38313635