# Asteroids!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4554    Accepted Submission(s): 2936

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 <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

struct node
{
int c,x,y,step;
};

char start[50],map[12][12][12];
int vis[12][12][12],no,ans,sc,sx,sy,ec,ex,ey;
int fx[6][3]={1,0,0,0,1,0,0,0,1,-1,0,0,0,-1,0,0,0,-1};

int check(node p)
{
if(p.c<0||p.x<0||p.y<0||p.c>=no||p.x>=no||p.y>=no)return 0;
if(vis[p.c][p.x][p.y])return 0;
if(map[p.c][p.x][p.y]=='X')return 0;
return 1;
}

int bfs()
{
queue<node>Q;
node t,p;
t.c=sc;
t.x=sx;
t.y=sy;
t.step=0;
Q.push(t);
while(!Q.empty())
{
t=Q.front();
Q.pop();
if(t.c==ec&&t.x==ex&&t.y==ey)return t.step;
for(int i=0;i<6;i++)
{
p.c=t.c+fx[i][0];
p.x=t.x+fx[i][1];
p.y=t.y+fx[i][2];
p.step=t.step+1;
if(check(p))
{
Q.push(p);
vis[p.c][p.x][p.y]=1;
}
}
}
return -1;
}

int main()
{
while(scanf("%s %d",start,&no)!=EOF)
{
getchar();
memset(vis,0,sizeof(vis));
/*
for(int i=0;i<no;i++)
for(int j=0;j<no;j++)
scanf("%s",map[i][j]);*/
for(int i=0;i<no;i++)
for(int j=0;j<no;j++){
for(int k=0;k<no;k++)
scanf("%c",&map[k][j][i]);
getchar();
}

scanf("%d%d%d%d%d%d",&sc,&sx,&sy,&ec,&ex,&ey);
scanf("%s",start);
ans=bfs();
if(ans==-1)
printf("NO ROUTE\n");
else printf("%d %d\n",no,ans);
}
return 0;
}


#### HDU1240 Asteroids!-题目意思详解（ 三维BFS）

2014-08-15 16:24:18

#### HDU 1240——Asteroids!（三维BFS）POJ 2225——Asteroids

2014-07-30 15:57:55

#### POJ 2225 && HDU 1240 Asteroids!（bfs）

2015-07-09 10:51:33

#### HDU 2225 Asteroids!（三维BFS）

2014-08-26 14:01:01

#### hdu 1240 Asteroids!（三维bfs）

2017-07-02 12:53:57

#### 动态规划DP结题报告

2010年08月10日 86KB 下载

#### hdu 1240 Asteroids BFS

2013-03-24 17:15:52

#### hdu 1240 Asteroids! (bfs)

2015-10-12 14:22:10

#### HDU 1240 Asteroids! 【BFS】

2014-12-12 23:38:13

#### HDU - 1240 Asteroids!（BFS）

2017-12-07 16:23:05