Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 36879 | Accepted: 14080 |
Descriptionc++
Inputapp
Outputide
Escaped in x minute(s).
Trapped!
Sample Inputui
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Outputspa
Escaped in 11 minute(s). Trapped!
Sourcecode
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 using namespace std; 5 char tmp[33][33][33]; 6 bool map[33][33][33],vis[33][33][33]; 7 int L,R,C,step; 8 struct Point{ 9 int l,r,c,step; 10 }S,E,now,next; 11 int dl[6]={-1,+1,0,0,0,0}; 12 int dr[6]={0,0,0,0,-1,+1}; 13 int dc[6]={0,0,-1,+1,0,0}; 14 int bfs() 15 { 16 queue<Point> q; 17 step=0; 18 q.push(S); 19 vis[S.l][S.r][S.c]=1; 20 while(!q.empty()) 21 { 22 now=q.front(); q.pop(); 23 if(now.l==E.l && now.r==E.r && now.c==E.c) return now.step; 24 for(int i=0;i<6;i++) 25 { 26 next.l=now.l+dl[i], next.r=now.r+dr[i], next.c=now.c+dc[i], next.step=now.step+1; 27 if(!map[next.l][next.r][next.c] && !vis[next.l][next.r][next.c]){ 28 q.push(next); 29 vis[next.l][next.r][next.c]=1; 30 } 31 } 32 } 33 return -1; 34 } 35 int main() 36 { 37 while(scanf("%d%d%d",&L,&R,&C) && L!=0) 38 { 39 memset(map,1,sizeof(map)); 40 memset(vis,0,sizeof(vis)); 41 for(int l=1;l<=L;l++){ 42 for(int r=1;r<=R;r++){ 43 scanf("%s",tmp[l][r]+1); 44 } 45 } 46 for(int l=1;l<=L;l++){ 47 for(int r=1;r<=R;r++){ 48 for(int c=1;c<=C;c++){ 49 map[l][r][c]=(tmp[l][r][c]=='#'); 50 if(tmp[l][r][c]=='S') S.l=l, S.r=r, S.c=c, S.step=0; 51 if(tmp[l][r][c]=='E') E.l=l, E.r=r, E.c=c; 52 } 53 } 54 } 55 int ans=bfs(); 56 if(ans==-1) printf("Trapped!\n"); 57 else printf("Escaped in %d minute(s).\n",ans); 58 } 59 }
必定要在push(next)的時候就進行標記,不然隊列裏會有許多重複的點,會MLE。orm