題目大意:你現在在太空中,但是你想回地球,星空中有很多的行星,你要做的就是避開這些行星。
給定三維空間中行星的分布圖,X代表著行星區域 ,大寫字母O代表著空白區域,接下來是你的起始點坐標,目的的坐標。
你需要求出能否到達目的地,如果可以你還需要輸出N和最小的步數,否則輸出NO ROUTE。
需要注意的就是輸入分布圖,然後廣搜,ok了。我剛開始就是因為輸入上的一點失誤卡住了,只要開一個三位數組就行了。
Asteroids!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2663 Accepted Submission(s): 1793
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
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; struct nobe { int x,y,z,time; }q[3000]; char map[10][10][10]; int visit[10][10][10],n,m,l,t,k; int go[6][3]={0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0},d[3000],flag; void BFS(int a,int b,int c) { struct nobe que,ue; int front=0,rear=0; int nx,ny,nz,i; que.x=a; que.y=b; que.z=c; que.time=0; q[rear++]=que; while(front<rear) { ue=q[front++]; if(ue.x==n&&ue.y==m&&ue.z==l) { flag=1; return ; } for(i=0;i<6;i++) { nx=ue.x+go[i][0]; ny=ue.y+go[i][1]; nz=ue.z+go[i][2]; if(!visit[nz][nx][ny]&&map[nz][nx][ny]=='O'&&nx>=0&&nx<=n&&ny>=0&&ny<=m&&nz>=0&&nz<=l) { visit[nz][nx][ny]=1; que.time=nx*k+ny+nz*k*k; que.x=nx; que.y=ny; que.z=nz; d[que.time]=d[ue.time]+1; q[rear++]=que; } } } } int main() { int i,j,o,n1,m1,l1; char nima[10]; while(~scanf("%s%d",nima,&k)) { for(o=0;o<k;o++) { for(i=0;i<k;i++) { for(j=0;j<k;j++) { cin>>map[o][i][j]; visit[o][i][j]=0; d[o*k*k+i*k+j]=0; } } } scanf("%d%d%d%d%d%d%s",&n1,&m1,&l1,&n,&m,&l,nima); flag=0; visit[l1][n1][m1]=1; BFS(n1,m1,l1); if(flag) printf("%d %d\n",k,d[l*k*k+n*k+m]); else printf("NO ROUTE\n"); } }