bfs ZOJ Asteroids!

三维迷宫 bfs

注意输入,先输入 col, row,slice, 即这样输入y, x ,z,y在x前面, 

开始没注意到WA一次

 

Asteroids!
   
     
1 #include < iostream >
2 #include < queue >
3 #include < string >
4   using namespace std;
5
6   struct point
7 {
8 int x;
9 int y;
10 int z;
11 int step;
12 };
13
14 int oper[ 6 ][ 1 ][ 3 ] = {
15 { 0 , 0 , 1 },
16 { 0 , 0 , - 1 },
17 { 1 , 0 , 0 },
18 { - 1 , 0 , 0 },
19 { 0 , 1 , 0 },
20 { 0 , - 1 , 0 }
21 };
22 bool isvisit[ 15 ][ 15 ][ 15 ];
23 point a[ 15 ][ 15 ][ 15 ];
24 point P;
25 point N;
26 point goal;
27 queue < point > Q;
28 int n;
29 void bfs();
30
31 int main()
32 {
33 string s;
34 char q;
35 freopen( " test.txt " , " r " , stdin);
36 while (cin >> s)
37 {
38 cin >> n;
39 for ( int i = 0 ; i < n; i ++ )
40 {
41 for ( int j = 0 ; j < n; j ++ )
42 {
43 for ( int k = 0 ; k < n; k ++ )
44 {
45 cin >> q;
46 if (q == ' O ' )
47 isvisit[i][j][k] = false ;
48 if (q == ' X ' )
49 isvisit[i][j][k] = true ;
50 a[i][j][k].x = j;
51 a[i][j][k].y = k;
52 a[i][j][k].z = i;
53 a[i][j][k].step = 0 ;
54 }
55 }
56 }
57
58 cin >> P.y >> P.x >> P.z;
59 P.step = 0 ;
60 cin >> goal.y >> goal.x >> goal.z;
61 bfs();
62 cin >> s;
63 }
64 return 0 ;
65 }
66
67 void bfs()
68 {
69 while ( ! Q.empty())
70 Q.pop();
71
72 Q.push(P);
73 isvisit[P.z][P.x][P.y] = true ;
74
75 while ( ! Q.empty())
76 {
77 N = Q.front();
78 Q.pop();
79
80 if (N.x == goal.x && N.y == goal.y && N.z == goal.z)
81 {
82 cout << n << " " << N.step << endl;
83 return ;
84 }
85
86 for ( int i = 0 ; i < 6 ; i ++ )
87 {
88 P.x = N.x + oper[i][ 0 ][ 0 ];
89 P.y = N.y + oper[i][ 0 ][ 1 ];
90 P.z = N.z + oper[i][ 0 ][ 2 ];
91 P.step = N.step + 1 ;
92
93 if (P.x >= 0 && P.x < n && P.y >= 0 && P.y < n && P.z >= 0 && P.z < n && ! isvisit[P.z][P.x][P.y])
94 {
95 isvisit[P.z][P.x][P.y] = true ;
96 Q.push(P);
97 }
98 }
99 }
100
101 cout << " NO ROUTE " << endl;
102
103 }

 

你可能感兴趣的:(bfs)