This topic, three-dimensional space on the BFS, give you the starting point and end point, see if you can find a way, O said to go, x means you can not go! ~
Understand the problem, you can use the queue to implement BFS to solve.
The following is the code for the AC:
#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; Class Data{public:int xyz;int count;}; Char map[11][11][11];bool flag[11][11][11];char str[10];int xyz[6][3] = {0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0}; Three-dimensional six directions int n;int SX, SY, SZ; Start int ex, EY, Ez;<span style= "White-space:pre" ></span>//Endpoint int BFS ()//bfs{queue< ;d Ata>q;while (! Q.empty ()) Q.pop (); if (sx = = Ex && sy = = ey && Sz = ez && Map[sx][sy][sz] = = ' O ' && Map[ex] [EY] [EZ] = = ' O ') return 0;data temp, te;int x, Y, z, temp1;temp.xyz = SX * + sy * + sz;temp.count = 0;flag[sx][sy][sz] = True Q.push (temp); while (! Q.empty ()) {temp = Q.front (); Q.pop (); for (int i = 0; i < 6; i++) {Temp1 = Temp.xyz;x = temp1/100 + xyz[i][0];temp1%= 100;y = TEMP1/10 + xyz[i][1] ; Temp1%= 10;z = Temp1 + xyz[i][2];if (x = = Ex && y = = ey && z = EZ) return Temp.count + 1;if (x >= 0 && x < n && y >= 0 && y < n && z >= 0 && z < n && map[x] [Y] [Z] = ' x ' &&!flag[x][y][z]) {te.xyz = X * + y * + z;te.count = temp.count + 1; Q.push (TE); flag[x][y][z] = true;}}} return-1;} int main () {//freopen ("Data.txt", "R", stdin); int I, J, K;while (scanf ("%s%d", str, &n)! = EOF)//input processing! More trouble {GetChar (); for (i = 0; i < n; i++) {for (j = 0; J < N; j + +) {for (k = 0; k < n; k++) scanf ("%c", &map[i][j][k]) ; GetChar ();}} scanf ("%d%d%d", &sx, &sy, &sz); scanf ("%d%d%d", &ex, &ey, &ez); GetChar (); scanf ("%s", str); GetChar (); memset (flag, false, sizeof (flag)), int ans = BFS (), if (ans < 0) printf ("NO route\n"); elseprintf ("%d%d\n", N, a NS);} return 0;}
Hangzhou Electric acm1240--asteroids!~~ Simple BFS