标签:
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,直接代码:
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<map> #include<set> #include<ctime> #define eps 1e-6 #define MAX 100005 #define INF 0x3f3f3f3f #define LL long long #define pii pair<int,int> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z) ///map<int,int>mmap; ///map<int,int >::iterator it; using namespace std; int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}}; int level,row,col; char mmap[35][35][35]; bool vis[35][35][35]; struct Pos { int l; int r; int c; int coun; Pos(){} Pos(int l,int r,int c,int coun) { this->l=l,this->r=r,this->c=c,this->coun = coun; } }start,eend; bool Ok(int l,int r,int c){ return (mmap[l][r][c]=='.' && l>=0 && r>=0 && c>=0 && l<level && r<row && c<col && vis[l][r][c]==false); } int main () { while(~scanf("%d%d%d",&level,&row,&col)&&level!=0&&row!=0&&col!=0) { memset(vis,0,sizeof(vis)); for(int i=0; i<level; i++) for(int j=0; j<row; j++) { scanf("%s",mmap[i][j]); for(int k=0; k<col; k++) { if(mmap[i][j][k] == 'S') start.l=i,start.r = j,start.c = k,start.coun=0; if(mmap[i][j][k] == 'E') eend.l=i,eend.r = j,eend.c = k,mmap[i][j][k] = '.'; } } queue<Pos> que; que.push(start); vis[start.l][start.r-1][start.c]=true; int coun = -1,come = 0; while(!que.empty()) { Pos a=que.front(); que.pop(); if(a.l==eend.l&&a.r==eend.r&&a.c==eend.c) { come = 1; coun = a.coun; break; } for(int i=0;i<6;i++){ int l=a.l+dir[i][0],r=a.r+dir[i][1],c=a.c+dir[i][2]; if( Ok(l,r,c) ) { que.push(Pos(l,r,c,a.coun+1)); vis[l][r][c]=1; } } } if(come) printf("Escaped in %d minute(s).\n",coun); else printf("Trapped!\n"); } return 0; }
标签:
原文地址:http://blog.csdn.net/u014665013/article/details/51329174