POJ 2251

    xiaoxiao2021-03-25  95

    一个三维的bfs,求S到E的最短路径就行。

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; struct node { int x, y, z; }q[30000]; const int maxn = 35; const int xx[maxn] = {1,-1,0,0,0,0}; const int yy[maxn] = {0,0,1,-1,0,0}; const int zz[maxn] = {0,0,0,0,1,-1}; bool dis[maxn][maxn][maxn]; char mp[maxn][maxn][maxn]; int length[30000]; int l, r, c; int sx, sy, sz; int bfs(){ memset(dis,false,sizeof(dis)); memset(length,0,sizeof(length)); q[0].x = sx,q[0].y = sy,q[0].z = sz; int front = 0, rear = 0; while( front <= rear ) { for(int i=0;i<6;i++) { int dx=q[front].x+xx[i]; int dy=q[front].y+yy[i]; int dz=q[front].z+zz[i]; if(!dis[dx][dy][dz] && (mp[dx][dy][dz]=='.' || mp[dx][dy][dz]=='E') && dx>=0 && dx<l && dy>=0 && dy<r && dz>=0 && dz<c) { dis[dx][dy][dz] = true; q[++rear].x = dx; q[rear].y = dy; q[rear].z = dz; length[rear] = length[front] + 1; if( mp[dx][dy][dz] =='E' ) return length[rear]; } } front++; } return 0; } int main() { int minutetime; while ( scanf ( "%d %d %d ", &l, &r, & c) && l && r && c ) { for(int i = 0; i < l; i++,getchar()) { for(int j = 0; j < r; j++,getchar()) { for(int k = 0; k < c; k++) { scanf("%c",&mp[i][j][k]); if( mp[i][j][k] == 'S' ) { sx=i,sy=j,sz=k; } } } } minutetime = bfs(); if ( minutetime ) printf("Escaped in %d minute(s).\n", minutetime); else printf("Trapped!\n"); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-15141.html

    最新回复(0)