hdu 1240 3维迷宫 求起点到终点的步数 (BFS)

时间:2023-03-08 19:55:15

题意,给出一个N,这是这个三空间的大小,然后给出所有面的状况O为空地,X为墙,再给出起始点的三维坐标和终点的坐标,输出到达的步数

比较坑 z是x,x是y,y是z,
Sample Input
START 1
O
0 0 0
0 0 0
END
START 3
XXX
XXX
XXX
OOO
OOO
OOO
XXX
XXX
XXX
0 0 1
2 2 1
END
START 5
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
0 0 0
4 4 4
END

Sample Output
1 0
3 4
NO ROUTE

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std; char map[][][];
int v[][][];
int n;
int sx,sy,sz;
int ex,ey,ez;
int dx[] = {,-,,,,};
int dy[] = {,,,-,,};
int dz[] = {,,,,,-};
int ans ; struct node
{
int x , y ,z ,step ;
}; int bfs()
{
node now , t ;
int fx , fy , fz ;
queue<node> q ;
now.x = sx ;
now.y = sy ;
now.z = sz ;
now.step = ;
memset(v,,sizeof(v)) ;
q.push(now) ;
v[sz][sx][sy] = ;
while(!q.empty())
{
now = q.front() ;
q.pop() ;
if (now.x == ex && now.y == ey && now.z == ez)
{
ans = now.step ;
return ;
}
for (int i = ; i < ; i++)
{
fx = now.x + dx[i] ;
fy = now.y + dy[i] ;
fz = now.z + dz[i] ;
if (fx< || fy< || fz< || fx>=n || fy>=n || fz>=n || v[fz][fx][fy] == || map[fz][fx][fy] == 'X')
continue ; t.x = fx ;
t.y = fy ;
t.z = fz ;
t.step = now.step + ;
v[fz][fx][fy] = ;
q.push(t) ;
}
} return ;
} int main ()
{
//freopen("in.txt","r",stdin) ;
char s[] ;
while (scanf("%s %d" , s , &n) !=EOF)
{
int i , j , k ;
for (i = ; i < n ; i ++)
for (j = ; j < n ; j ++)
{
scanf("%s" , map[i][j]) ;
}
scanf("%d %d %d" , &sx,&sy,&sz) ;
scanf("%d %d %d" , &ex,&ey,&ez) ;
scanf("%s" , s) ; ans = ;
if (bfs())
printf("%d %d\n" , n , ans) ;
else
printf("NO ROUTE\n") ;
} return ;
}