HDU-1240 Asteroids! (BFS)这里是一个三维空间,用一个6*3二维数组储存6个不同方向

HDU-1240    Asteroids! (BFS)这里是一个三维空间,用一个6*3二维数组储存6个不同方向

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2664    Accepted Submission(s): 1794
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 0
3 4
NO ROUTE
  1 #include<stdio.h>
  2 
  3 #include<queue>
  4 
  5 using namespace std;
  6 
  7 typedef struct
  8 
  9 {
 10 
 11     int x,y,z,steps;
 12 
 13 }point;
 14 
 15 point start,end;
 16 
 17 int n;
 18 
 19 char map[11][11][11];
 20 
 21 int dir[6][3]={{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};
 22 
 23  
 24 
 25 int bfs(point start)
 26 
 27 {
 28 
 29     queue<point>q;
 30 
 31     int i;
 32 
 33     point cur,next;
 34 
 35     if(start.x==end.x&&start.y==end.y&&start.z==end.z)//考虑起点和终点相同的情况
 36 
 37     {
 38 
 39        return 0;
 40 
 41     }
 42 
 43     start.steps=0;
 44 
 45     map[start.x][start.y][start.z]='X';
 46 
 47     q.push(start);
 48 
 49     while(!q.empty())
 50 
 51     {
 52 
 53        cur=q.front();//取队首元素
 54 
 55        q.pop();
 56 
 57        for(i=0;i<6;i++) //广度优先搜索
 58 
 59        {
 60 
 61            next.x=cur.x+dir[i][0];
 62 
 63            next.y=cur.y+dir[i][1];
 64 
 65            next.z=cur.z+dir[i][2];
 66 
 67            if(next.x==end.x && next.y==end.y && next.z==end.z) //下一步就是目的地
 68 
 69             {
 70 
 71                 return cur.steps+1;
 72 
 73             }
 74 
 75  
 76 
 77            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<n&&next.z>=0&&next.z<n)//下一步不越界
 78 
 79               if(map[next.x][next.y][next.z]!='X') //下一步不是星星
 80 
 81               {
 82 
 83                   map[next.x][next.y][next.z]='X';
 84 
 85                   next.steps=cur.steps+1;
 86 
 87                   q.push(next);
 88 
 89               }
 90 
 91        }
 92 
 93     }
 94 
 95     return -1;
 96 
 97 }
 98 
 99 int main()
100 
101 {
102 
103     char str[10];
104 
105     int i,j,step;
106 
107     while(scanf("START %d",&n)!=EOF)
108 
109     {
110 
111        for(i=0;i<n;i++)
112 
113            for(j=0;j<n;j++)
114 
115               scanf("%s",map[i][j]);
116 
117        scanf("%d%d%d",&start.x, &start.y, &start.z);
118 
119        scanf("%d%d%d",&end.x, &end.y, &end.z);
120 
121        scanf("%s",str);
122 
123        gets(str);
124 
125  
126 
127        step=bfs(start);
128 
129        if(step>=0)
130 
131           printf("%d %d
",n,step);
132 
133        else
134 
135           printf("NO ROUTE
");
136 
137     }
138 
139     return 0;
140 
141 }
142 
143 
144