标签:mes return pen scanf 维数 min ios name tor
考察:bfs+三维数组
思路:
没什么好说的,就是三维走迷宫,比二维多两个方向就是了
POJ请不要用C++11的特性
最短路还是用bfs比较好,一开始用dfs结果貌似栈溢出了
#include <iostream> #include <cstring> #include <queue> using namespace std; const int N = 31; int h,r,c; char mp[N][N][N]; int dist[N][N][N]; struct Node{ int x,y,z; bool operator==(Node m) { if(m.x==this->x&&m.y==this->y&&m.z==this->z) return true; else return false; } }Nodes[2]; int bfs(Node st) { int xx[6] = {-1,1,0,0,0,0},yy[6] = {0,0,-1,1,0,0},zz[6] = {0,0,0,0,-1,1}; queue<Node> q; q.push(st); memset(dist,-1,sizeof(dist)); dist[Nodes[0].z][Nodes[0].x][Nodes[0].y]=0; while(!q.empty()) { Node it = q.front(); q.pop(); int d = dist[it.z][it.x][it.y]; if(it==Nodes[1]) return d; for(int i=0;i<6;i++){ int dx = it.x+xx[i],dy = it.y+yy[i],dz=it.z+zz[i]; if(dx>=0&&dx<r&&dy>=0&&dy<c&&dz>=0&&dz<h&&mp[dz][dx][dy]!=‘#‘&&dist[dz][dx][dy]==-1) { dist[dz][dx][dy] = d+1; Node p; p.x = dx; p.y = dy; p.z = dz; q.push(p); } } } return -1; } int main() { // freopen("in.txt","r",stdin); while(scanf("%d%d%d",&h,&r,&c)!=EOF&&(h!=0||r!=0||c!=0)) { memset(mp,0,sizeof(mp)); for(int k=0;k<h;k++){ for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ cin>>mp[k][i][j]; if(mp[k][i][j]==‘S‘) Nodes[0].x=i,Nodes[0].y=j,Nodes[0].z=k; else if(mp[k][i][j]==‘E‘) Nodes[1].x=i,Nodes[1].y=j,Nodes[1].z=k; } } } int di = bfs(Nodes[0]); if(di==-1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",di); } }
标签:mes return pen scanf 维数 min ios name tor
原文地址:https://www.cnblogs.com/newblg/p/14250829.html