给出一个三维的迷宫以及起点和终点,求能否到大终点,若果能输出最短步数

三维的问题无非就是变成了6个搜索方向

最后强调一下xyz的顺序,从输入数据来看,读入的顺序是map[z][x][y]

总之,这是很基础的一道题

 

HDU 1240 (简单三维广搜) Asteroids!_#ifdefHDU 1240 (简单三维广搜) Asteroids!_数据_02
 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <queue>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 struct Point
10 {
11     char type;
12     int x, y, z;
13     int steps;
14 }start, end;
15 
16 char map[12][12][12];
17 int dir[6][3] = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};
18 char buff[10];
19 int n;
20 
21 bool islegal(int x, int y, int z)
22 {
23     return (x>=0 && x<n && y>=0 && y<n && z>=0 && z<n && map[z][x][y]!='X');
24 }
25 
26 void BFS(void)
27 {
28     queue<Point> qu;
29     start.steps = 0;
30     qu.push(start);
31     while(!qu.empty())
32     {
33         Point now = qu.front();
34         if(now.x==end.x && now.y==end.y && now.z==end.z)
35         {
36             printf("%d %d\n", n, now.steps);
37             return;
38         }
39         for(int i = 0; i < 6; ++i)
40         {
41             int xx = now.x + dir[i][0];
42             int yy = now.y + dir[i][1];
43             int zz = now.z + dir[i][2];
44             if(islegal(xx, yy, zz))
45             {
46                 Point next;
47                 next.x = xx, next.y = yy, next.z = zz, next.steps = now.steps + 1;
48                 map[zz][xx][yy] = 'X';
49                 qu.push(next);
50             }
51         }
52         qu.pop();
53     }
54     printf("NO ROUTE\n");
55 }
56 
57 int main(void)
58 {
59     #ifdef LOCAL
60         freopen("1240in.txt", "r", stdin);
61     #endif
62 
63     while(cin >> buff >> n)
64     {
65         for(int i = 0; i < n; ++i)
66             for(int j = 0; j < n; ++j)
67                 cin >> map[i][j];
68 
69         cin >> start.x >> start.y >> start.z;
70         cin >> end.x >> end.y >> end.z;
71         cin >> buff;
72         BFS();
73     }
74     return 0;
75 }
代码君