# 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;
}


• 本文已收录于以下专栏：

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

• u014141559
• 2014年07月30日 15:57
• 703

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

Description 给定一个三维的迷宫,已知起点和终点，问是否能从起点到达终点，并且最短的距离又是多少 Input 第一行为START n，n为三维迷宫的边长，之后为n个矩阵，每个矩阵为一层...
• V5ZSQ
• 2015年07月09日 10:51
• 339

## HDU 2225 Asteroids!（三维BFS）

Asteroids! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Desc...
• Hush_lei
• 2014年08月26日 14:01
• 614

## HDU 1240 Asteroids! （三维BFS）

Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...
• liangzhaoyang1
• 2016年06月04日 00:26
• 512

## hdu 1240 Asteroids! (三维bfs)

Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot...
• u011262722
• 2013年07月27日 06:11
• 1244

## hdu 1240 Asteroids!（三维bfs）

Problem Description You’re in space. You want to get home. There are asteroids. You don’t want t...
• feizaoSYUACM
• 2017年07月02日 12:53
• 137

## poj 2225 Asteroids!【BFS】

Asteroids! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3087   Acc...
• nailnehc
• 2015年11月20日 20:11
• 300

## HDOJ 1240 Asteroids!（三维BFS）

Asteroids! Problem Description You're in space. You want to get home. There are asteroids. You ...
• qq_26658823
• 2016年08月06日 22:20
• 162

## HDU 1240 Asteroids! 【BFS】

Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot...
• u013806814
• 2014年12月12日 23:38
• 427

## hdu 1240 Asteroids! (bfs)

• qiqi_skystar
• 2015年10月12日 14:22
• 298

举报原因： 您举报文章：hdu 1240 poj 2225 Asteroids! 三维bfs 解题报告 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)