ZOJ 1438 Asteroids!

三维的迷宫搜索。

 

题目中的地图是按照层数给定的。

 

题目中给出起点终点时候是先列再行再层数,这点一定要注意,我wa了好多次。。

 

#include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> int dir[6][3] = {1,0,0,-1,0,0, 0,1,0,0,-1,0, 0,0,1,0,0,-1}; struct point { int x,y,z; int ceng; }t,e,from,to; int main(void) { std::queue<struct point> q; int i,j,k; int n,z,y,x; char mat[12][12][12]; int flag[12][12][12]; char s[10]; while( scanf("%s%d",s,&n) ==2 ) { getchar(); for( i = 0; i <= n+1; i++ ) for( j = 0; j <= n+1; j++ ) for( k = 0; k <= n+1; k++ ) mat[i][j][k] = 'X'; for( z = 1; z <= n; z++ ) for( x = 1; x <= n; x ++){ for( y = 1; y <= n; y++ ) mat[x][y][z]=getchar(); getchar(); } scanf("%d%d%d",&from.y,&from.x,&from.z);from.ceng=0; scanf("%d%d%d%s",&to.y,&to.x,&to.z,s); from.x++;from.y++;from.z++; to.x++;to.y++;to.z++; memset( flag,0,sizeof(flag)); flag[ from.x ][ from.y ][ from.z ] = 1; q.push(from); while( !q.empty() ) { e = q.front();q.pop(); if( e.x==to.x && e.y==to.y && e.z==to.z ) break; for( i = 0; i < 6; i++ ) { t = e; t.x += dir[i][0]; t.y += dir[i][1]; t.z += dir[i][2]; t.ceng++; if( !flag[t.x][t.y][t.z] && mat[t.x][t.y][t.z] == 'O') { flag[t.x][t.y][t.z] = 1; q.push(t); } } } while( !q.empty() ) q.pop(); if( e.x==to.x && e.y==to.y && e.z==to.z ) printf("%d %d/n",n,e.ceng); else printf("NO ROUTE/n"); } return 0; }  

你可能感兴趣的:(ZOJ 1438 Asteroids!)