Asteroids!
Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2427 Accepted Submission(s): 1645
Problem Description
You're in space.
You want to get home.
There are asteroids.
You don't want to hit them.
Input
Input to thisproblem will consist of a (non-empty) series of up to 100 data sets. Each dataset will be formatted according to the following description, and there will beno 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 ahorizontal slice through the asteroid field. Each position in the matrix willbe 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 coordinatevalues 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 integersseparated by individual spaces.
End line - A single line, "END"
The origin of the coordinate system is <0,0,0>. Therefore, each componentof 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 linesseparating output sets.
A single output set consists of a single line. If a route exists, the line willbe in the format "X Y", where X is the same as N from thecorresponding input data set and Y is the least number of moves necessary toget your ship from the starting position to the target position. If there is noroute 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 ordecrement 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
题目大意:
你现在在太空中,但是你想回地球,星空中有很多的行星,你要做的就是避开这些行星。本题的难点就在于所给定的是三维空间中行星的分布图,X代表着行星区域 ,大写字母O代表着空白区域,接下来是你的起始点坐标,目的的坐标。当然搜索无外乎这样,需要我们做的就是求出能否到达目的地,如果可以,则需要输出N和最小的步数,否则输出NO ROUTE。
大概思路:
这道题同样是广义搜索的经典题目,不同于走迷宫的是变换成了三维空间结构,类似于题目中所描述的那样,在定义三维数组表示数据分布的时候,可以有两种方法输入。1,用三个变量,即三重循环输入map[i][j][k];2,用两重循环,即第三维表示切片层,输入表示为map[i][j]。方向则由原来的4个变成了6个,即dir[6][3]={{0,0,1},{0,0,-1},{0,-1,0},{0,1,0},{1,0,0},{-1,0,0}};当数据输入后,最重要的依然是BFS算法的定义,不过,有一点需要注意,访问过的位置需要标记为‘X’,或者用两一个Visited[MAX][MAX][MAX]标记。
#include <iostream> #include <stdio.h> #include<memory.h> #include <queue> #define N 9 using namespace std; int dir[6][3]={{0,0,1},{0,0,-1},{0,-1,0},{0,1,0},{1,0,0},{-1,0,0}}; //定义方向,上下左右前后 char map[N][N][N]; //三维数组存储数据分布 int visit[N][N][N]; //记录地图上位置是否被访问过,若访问过则记为1 int stx,sty,stz,edx,edy,edz,flag; //开始与结束位置的坐标 struct node { int x,y,z; int step; }; int BFS(int n) { int i; flag=0; //flag赋初值,若为则表示有路经可到达,否则表示无法到达 node start,cur,next; memset(visit,0,sizeof(visit)); queue<node>q; start.x=stx; start.y=sty; start.z=stz; start.step=0; visit[stx][sty][stz]=1; q.push(start); //初始位置进队列 while(!q.empty()) //队列不空时循环 { cur=q.front(); q.pop(); for(i=0;i<6;i++) //对当前位置的上下左右前后六个方向判断 { next.x=cur.x+dir[i][0]; next.y=cur.y+dir[i][1]; next.z=cur.z+dir[i][2]; next.step=cur.step+1; if(next.x<n&&next.y<n&&next.z<n&&next.x>=0&&next.y>=0&&next.z>=0&&map[next.x][next.y][next.z]=='O'&&visit[next.x][next.y][next.z]==0) { if(next.x==edx&&next.y==edy&&next.z==edz) //如果是出口,返回步数 { flag=1; return next.step; } visit[next.x][next.y][next.z]=1; q.push(next); } } } return 0; } int main() { char strSt[10],strEd[10]; int n,sum,i,j,k; while(cin>>strSt&&cin>>n) { for(k=0;k<n;k++) for(i=0;i<n;i++) for(j=0;j<n;j++) cin>>map[i][j][k]; cin>>stx>>sty>>stz>>edx>>edy>>edz>>strEd; if(stx==edx&&sty==edy&&stz==edz) //若初始位置与结束位置重合,直接返回步数0 { cout<<n<<' '<<0<<endl; // continue; } else { sum=BFS(n); if(flag==0) cout<<"NO ROUTE"<<endl; else cout<<n<<' '<<sum<<endl; } } return 0; }