hdu 1240 3维迷宫 求起点到终点的步数 (BFS)
题意,给出一个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
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 using namespace std; 6 7 char map[20][20][20]; 8 int v[20][20][20]; 9 int n; 10 int sx,sy,sz; 11 int ex,ey,ez; 12 int dx[] = {1,-1,0,0,0,0}; 13 int dy[] = {0,0,1,-1,0,0}; 14 int dz[] = {0,0,0,0,1,-1}; 15 int ans ; 16 17 struct node 18 { 19 int x , y ,z ,step ; 20 }; 21 22 int bfs() 23 { 24 node now , t ; 25 int fx , fy , fz ; 26 queue<node> q ; 27 now.x = sx ; 28 now.y = sy ; 29 now.z = sz ; 30 now.step = 0 ; 31 memset(v,0,sizeof(v)) ; 32 q.push(now) ; 33 v[sz][sx][sy] = 1 ; 34 while(!q.empty()) 35 { 36 now = q.front() ; 37 q.pop() ; 38 if (now.x == ex && now.y == ey && now.z == ez) 39 { 40 ans = now.step ; 41 return 1 ; 42 } 43 for (int i = 0 ; i < 6 ; i++) 44 { 45 fx = now.x + dx[i] ; 46 fy = now.y + dy[i] ; 47 fz = now.z + dz[i] ; 48 if (fx<0 || fy<0 || fz<0 || fx>=n || fy>=n || fz>=n || v[fz][fx][fy] == 1 || map[fz][fx][fy] == 'X') 49 continue ; 50 51 t.x = fx ; 52 t.y = fy ; 53 t.z = fz ; 54 t.step = now.step + 1 ; 55 v[fz][fx][fy] = 1 ; 56 q.push(t) ; 57 } 58 } 59 60 return 0 ; 61 } 62 63 int main () 64 { 65 //freopen("in.txt","r",stdin) ; 66 char s[10] ; 67 while (scanf("%s %d" , s , &n) !=EOF) 68 { 69 int i , j , k ; 70 for (i = 0 ; i < n ; i ++) 71 for (j = 0 ; j < n ; j ++) 72 { 73 scanf("%s" , map[i][j]) ; 74 } 75 scanf("%d %d %d" , &sx,&sy,&sz) ; 76 scanf("%d %d %d" , &ex,&ey,&ez) ; 77 scanf("%s" , s) ; 78 79 ans = 0 ; 80 if (bfs()) 81 printf("%d %d\n" , n , ans) ; 82 else 83 printf("NO ROUTE\n") ; 84 } 85 86 return 0 ; 87 }