Asteroids!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3159 Accepted Submission(s): 2106
You want to get home.
There are asteroids.
You don't want to hit them.
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.
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.
这道题是三维BFS搜索,类似于 hdu 1253:胜利大逃亡(基础广搜BFS) 。
for(i=;i<n;i++) //输入地图
for(j=;j<n;j++)
for(k=;k<n;k++)
cin>>a[j][k][i];
代码:
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
char a[][][]; //记录地图
bool isv[][][]; //记录访问过没有
int dx[] = {,,,-,,};
int dy[] = {,,-,,,};
int dz[] = {,,,,-,};
int n;
int sx,sy,sz,ex,ey,ez;
struct NODE{
int x;
int y;
int z;
int step;
};
bool judge(int x,int y,int z)
{
if( x< || y< || z< || x>=n || y>=n || z>=n ) //出界
return ;
if( isv[x][y][z] ) //走过
return ;
if( a[x][y][z]=='X' ) //遇到墙
return ;
return ;
}
int bfs() //返回到达终点的时间
{
memset(isv,,sizeof(isv));
queue <NODE> q;
NODE cur,next;
cur.x = sx;
cur.y = sy;
cur.z = sz;
cur.step = ;
isv[sx][sy][sz] = true;
q.push(cur); //第一个节点入队
while(!q.empty()){
cur = q.front();
q.pop(); //队首出队
if( cur.x==ex && cur.y==ey && cur.z==ez){
return cur.step;
}
for(int i=;i<;i++){
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
int nz = cur.z + dz[i];
if(judge(nx,ny,nz)) //判定
continue;
//可以走
next.x = nx;
next.y = ny;
next.z = nz;
isv[nx][ny][nz] = true; //记录访问过
next.step = cur.step + ;
q.push(next);
}
}
return -;
}
int main()
{
char str[];
int i,j,k;
while(cin>>str>>n){
for(i=;i<n;i++) //输入地图
for(j=;j<n;j++)
for(k=;k<n;k++)
cin>>a[j][k][i];
cin>>sx>>sy>>sz;
cin>>ex>>ey>>ez;
cin>>str;
int step = bfs();
if(step!=-) //到达终点
cout<<n<<' '<<step<<endl;
else
cout<<"NO ROUTE"<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013