Asteroids!
- 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
刚开始看的时候可能是有点蒙,但其实是一道简单的搜索问题。
- 题面大意是建立一个空间坐标系,输入一系列OX坐标点,用来表示“Empty space”和“Asteroid present”,输入起点和终点,问你能不能不hit asteroid的情况下从起点走到终点,不能的话输出NO ROUTE,如果能输出最少需要多少步。
- 容易想到应该需要一个三维数组来存储这些坐标点,思路是BFS。
- 对于三维数组,我们不要想太多,即设一个数组a[ ][ ][ ],类似于立体几何,第一个方括号里面的即表示空间直角坐标系下横坐标,第二个表示纵坐标,第三个表示竖坐标。
- OK
#include <iostream>
#include <queue>
using namespace std;
const int MAXN=15;
int N,x,y,z,ax,ay,az,tot,flag;
#define Check(a,b,c) (a>=0&&a<N&&b>0&&b<N&&c>0&&c<N)
//宏定义检验移动过程中是否在范围内
struct node{
int x,y,z;int step=0;};
//会出现警告,不让在类内初始化非静态数据成员,不过没大问题。
char Data[MAXN][MAXN][MAXN];
int dir[6][3]={
{
0,0,1},{
0,1,0},{
1,0,0},{
0,0,-1},{
0,-1,0},{
-1,0,0}};
//用dir数组来控制移动方向
void bfs(){
flag=false;queue<node> q;node start,next;start.x=x;start.y=y;start.z=z;q.push(start);while(!q.empty()){
start=q.front();q.pop();if(start.x==ax&&start.y==ay&&start.z==az){
flag=true;tot=next.step;return;}//cout<<start.x<<" "<<start.y<<" "<<start.z<<endl;for(int i=0;i<6;i++){
next.x=start.x+dir[i][0];next.y=start.y+dir[i][1];next.z=start.z+dir[i][2];//cout<<next.x<<" "<<next.y<<" "<<next.z<<endl;if(Check(next.x,next.y,next.z)&&Data[next.x][next.y][next.z]=='O'){
//cout<<next.x<<" "<<next.y<<" "<<next.z<<endl;if(next.x==ax&&next.y==ay&&next.z==az){
flag=true;tot=next.step+1;return;}Data[next.x][next.y][next.z]='X';next.step=start.step+1;q.push(next);}}}
}
int main(){
string s;while(cin>>s>>N){
for(int i=0;i<N;i++)for(int j=0;j<N;j++)for(int k=0;k<N;k++)cin>>Data[k][j][i];cin>>x>>y>>z>>ax>>ay>>az;cin>>s;bfs();if(flag)cout<<N<<" "<<tot<<endl;else cout<<"NO ROUTE"<<endl;}//system("pause");return 0;
}
- 刚开始学习BFS可以练习一下比较经典的hdu1312题,那道题和这个的区别是那道题应该用一个变量去表示可能经过的位置,而这道题是在结构体里面定义一个step存储步长,这样做能够确保找到的是最小值。