Asteroids!

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

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
 
Source
 
Recommend
zf   |   We have carefully selected several similar problems for you:  1242 1072 1372 1238 1548 

 
  三维BFS搜索
  题意是先输入一行START N,“START”为字符串,表示开始,忽略即可。N是地图大小,为N*N*N。然后输入N*N行,每行N个字符,每N行表示一层,N层

  这道题是三维BFS搜索,类似于 hdu 1253:胜利大逃亡(基础广搜BFS) 。

  注意输入的时候,先输入第一层,也就是说z坐标轴等于0,等输入完第一层再将z坐标轴变为1。也就是说z坐标轴变化的要最慢,放在第一层循环。即三层循环结构应为:
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

hdu 1240:Asteroids!(三维BFS搜索)的更多相关文章

  1. hdu 1240 Asteroids! (三维bfs)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  2. hdu 1240 Asteroids!(BFS)

    题目链接:点击链接 简单BFS,和二维的做法相同(需注意坐标) 题目大意:三维的空间里,给出起点和终点,“O”表示能走,“X”表示不能走,计算最少的步数 #include <iostream&g ...

  3. HDU 1240 Asteroids!【BFS】

    题意:给出一个三维的空间,给出起点和终点,问是否能够到达终点 和上一题一样,只不过这一题的坐标是zxy输入的, 因为题目中说的是接下来的n行中分别是由n*n的矩形组成的,所以第一个n该是Z坐标,n*n ...

  4. HDU 1240 Asteroids! 题解

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. HDU 1240——Asteroids!(三维BFS)POJ 2225——Asteroids

    普通的三维广搜,须要注意的是输入:列,行,层 #include<iostream> #include<cstdio> #include<cstring> #incl ...

  6. HDU 1240 (简单三维广搜) Asteroids!

    给出一个三维的迷宫以及起点和终点,求能否到大终点,若果能输出最短步数 三维的问题无非就是变成了6个搜索方向 最后强调一下xyz的顺序,从输入数据来看,读入的顺序是map[z][x][y] 总之,这是很 ...

  7. HDU 1240 Asteroids!(BFS)

    题目链接 Problem Description You're in space.You want to get home.There are asteroids.You don't want to ...

  8. HDU 2364 (记忆化BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...

  9. HDU 2579 (记忆化BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有 ...

随机推荐

  1. mvc 简单笔记

    ---恢复内容开始--- 入口文件 index.php 唯一的一个让浏览器直接请求的脚本文件 控制器 协调模型和试图 模型 提供数据 保存数据 数据的验证 试图 只负责显示 <?php $c = ...

  2. 网站性能Web压力测试工具webbench

    webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便. 1.适用系统:Linux 2.编译安装: wget http:/ ...

  3. PDP 有多种定义,具体哪一种还需研究!!!!

    PDP (用户面进行隧道转发的信息的保存协议) 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 即PDP上下文,保存用户面进行隧道转发的所有信息,包括RNC/GGSN的 ...

  4. PQ格式化虚拟机硬盘如何生效

    用pq格式化虚拟机硬盘后,安装时,总是从dhcp的网卡启动,没有从硬盘启动 但是用ghost是可以拷贝镜像文件的 这就是说,硬盘有了,但是没有将硬盘"激活",没有将硬盘设为acti ...

  5. Object学习笔记

    <script type="text/javascript"> function forEach(o){ var html =""; for(var ...

  6. node.js模拟qq漂流瓶

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) node.js模拟简易漂流瓶,页面有扔瓶子和捡瓶子的功能,一个瓶子只能被捡到一次,阅读完就置状态位, ...

  7. HDOJ 2544

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  8. The Perfect Stall (incomplete)

    恩,一看就知道是一道二分图最大匹配的题. 感动得发现自己不会做..果然我是太弱了.学校里真是麻烦死,根本没有时间好吗. (NOIP)会不会感动地滚粗啊? 然后稍微看看,恩,匈牙利算法. 真是感动得落泪 ...

  9. 又是一个二模02,不过day2

    话说比较简单.除了第三题不会写平衡树啊你妹!!边做边写吧. 机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链 ...

  10. ubuntu apt-get update 失败解决

    在执行 sudo apt-get update 之后  会出现如下错误 这是要检测网络是否有问题 因为我之前只使用nfs挂载的时候,在虚拟机  编辑-> 虚拟网络编辑器里面->更改设置里面 ...