好吧,BFS用模版还是比较好理解的。这代码能AC- -
Problem Description
You're in space.
You want to get home.
There are asteroids.
You don't want to hit them.
Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.
A single data set has 5 components:
Start line - A single line, "START N", where 1 <= N <= 10.
Slice list - A series of N slices. Each slice is an N x N matrix representing a horizontal slice through the asteroid field. Each position in the matrix will be one of two values:
'O' - (the letter "oh") Empty space
'X' - (upper-case) Asteroid present
Starting Position - A single line, "A B C", denoting the <A,B,C> coordinates of your craft's starting position. The coordinate values will be integers separated by individual spaces.
Target Position - A single line, "D E F", denoting the <D,E,F> coordinates of your target's position. The coordinate values will be integers separated by individual spaces.
End line - A single line, "END"
The origin of the coordinate system is <0,0,0>. Therefore, each component of each coordinate vector will be an integer between 0 and N-1, inclusive.
The first coordinate in a set indicates the column. Left column = 0.
The second coordinate in a set indicates the row. Top row = 0.
The third coordinate in a set indicates the slice. First slice = 0.
Both the Starting Position and the Target Position will be in empty space.
Output
For each data set, there will be exactly one output set, and there will be no blank lines separating output sets.
A single output set consists of a single line. If a route exists, the line will be in the format "X Y", where X is the same as N from the corresponding input data set and Y is the least number of moves necessary to get your ship from the starting position to the target position. If there is no route from the starting position to the target position, the line will be "NO ROUTE" instead.
A move can only be in one of the six basic directions: up, down, left, right, forward, back. Phrased more precisely, a move will either increment or decrement a single component of your current position vector by 1.
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 #include <cstdio>
2 #include <cstring>
3 #include <queue>
4 #define MAX 11
5 using namespace std;
6
7 typedef struct node
8 {
9 int x,y,z;
10 int move;
11 }point;
12 point start,end;
13
14 const int dir[7][3]={{0,0,0},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
15 int N;
16 char str[MAX],map[MAX][MAX][MAX];
17
18 void Bfs(void);
19 int main()
20 {
21 int i,j,k;
22 while(scanf("%s",str)!=EOF)
23 {
24 scanf("%d",&N);
25 memset(map,0,sizeof(map));
26 for(i=0;i<N;i++)
27 for(j=0;j<N;j++)
28 scanf("%s",map[i][j]);
29 start.move=0;
30 scanf("%d %d %d",&start.x,&start.y,&start.z);
31 scanf("%d %d %d",&end.x,&end.y,&end.z);
32 scanf("%s",str);
33 Bfs();
34 }
35 return 0;
36 }
37
38 void Bfs()
39 {
40 queue<point>p,p_free;
41 p.push(start);
42 point temp,next;
43 while(!p.empty())
44 {
45 temp=p.front();
46 p.pop();
47 for(int i=0;i<7;i++)
48 {
49 next.x=temp.x+dir[i][0];
50 next.y=temp.y+dir[i][1];
51 next.z=temp.z+dir[i][2];
52 if(i==0)
53 next.move=temp.move;
54 else
55 next.move=temp.move+1;
56 if(next.x>=0&&next.x<N && next.y>=0&&next.y<N && next.z>=0&&next.z<N)
57 if(map[next.z][next.y][next.x]=='O')
58 {
59 map[next.z][next.y][next.x]='@';
60 p.push(next);
61 if(next.x==end.x && next.y==end.y && next.z==end.z)
62 {
63 printf("%d %d\n",N,next.move);
64 return;
65 }
66 }
67 }
68 }
69 printf("NO ROUTE\n");
70 return;
71 }
72 /*
73 START 3
74 XXX
75 XXX
76 XXX
77 OOO
78 OOO
79 OOO
80 XXX
81 XXX
82 XXX
83 0 0 1
84 2 2 1
85 END
86 */