HDU 1240 Asteroids!(BFS)
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.


#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
int n,m;
struct Node
{
int x,y,z;
int step;
}sp,ep,cp;
char mp[][][];
int dir[][]={{,,},{,-,},{-,,},{,,},{,,},{,,-}};
int bfs()
{
queue<Node> q;
while(!q.empty())q.pop();
q.push(sp);
while(!q.empty())
{
sp=q.front(),q.pop();
if(sp.x==ep.x&&sp.y==ep.y&&sp.z==ep.z)return sp.step;
for(int i=;i<;i++)
{
cp.x=sp.x+dir[i][];
cp.y=sp.y+dir[i][];
cp.z=sp.z+dir[i][];
cp.step=sp.step+;
if(mp[cp.x][cp.y][cp.z]=='X')continue;
if(cp.x<||cp.y<||cp.z<||cp.x>=n||cp.y>=n||cp.z>=n)continue;
mp[cp.x][cp.y][cp.z]='X';
q.push(cp);
}
}
return -;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
char t[];
while(cin>>t>>n)
{
m=;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
cin>>mp[i][j];
cin>>sp.z>>sp.y>>sp.x;//x-col,y-raw,z-dim
cin>>ep.z>>ep.y>>ep.x;
sp.step=,ep.step=;
mp[sp.x][sp.y][sp.z]='X';
int ans=bfs();
if(ans==-)printf("NO ROUTE\n");
else printf("%d %d\n",n,ans);
while(cin>>t)if(t[]=='E')break;
}
return ;
}
HDU 1240 Asteroids!(BFS)的更多相关文章
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 1240 Asteroids! (三维bfs)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1240——Asteroids!(三维BFS)POJ 2225——Asteroids
普通的三维广搜,须要注意的是输入:列,行,层 #include<iostream> #include<cstdio> #include<cstring> #incl ...
- hdu 1240 Asteroids!(BFS)
题目链接:点击链接 简单BFS,和二维的做法相同(需注意坐标) 题目大意:三维的空间里,给出起点和终点,“O”表示能走,“X”表示不能走,计算最少的步数 #include <iostream&g ...
- HDU 1240 Asteroids!【BFS】
题意:给出一个三维的空间,给出起点和终点,问是否能够到达终点 和上一题一样,只不过这一题的坐标是zxy输入的, 因为题目中说的是接下来的n行中分别是由n*n的矩形组成的,所以第一个n该是Z坐标,n*n ...
- HDU 1240 Asteroids! 题解
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1240 Asteroids! 解题报告
//这道题做完我只有 三个感受 第一:坑: 第二 : 坑! 第三:还是坑! 咳咳 言归正传 WA了无数次之后才发现是输入进去时坐标时z, y, x的顺序输入的 题解 : 类似胜利大逃亡 只 ...
- HDU 1240 Asteroids!
三维广搜 #include <cstdio> #include <iostream> #include <cstring> #include <queue&g ...
- hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1240 开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向. 第一维代表在 ...
随机推荐
- 自定义Dialog,从下面弹出
Window window= getWindow(); 只要 打开一个Activity 就有一个窗口存放这个Activity ,手机又很多个窗口,不只是一个窗口 import android.app. ...
- python 数组过滤
arr = [1,2,3,4,5,6,7,8,None]arr = [elem for elem in arr if elem != None]
- Ubuntu 14.04—Eclipse配置Pydev
Eclipse: 1. 下载 Eclipse 最新版 访问官方网站下载 Eclipse 最新版,这个就不多说了,大家自己去下. http://www.eclipse.org/downloads/?o ...
- CSS3 六边形绘制
把一个104px的div放在它们之间,设置一个背景颜色: width: 0; border-bottom: 30px solid #6C6; border-left: 52px solid trans ...
- layoutSubviews在什么情况下会被调用
layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubviews. 2.addSubview会触发layoutSubviews. 3.设置view的Frame ...
- eclipse中debug快捷方式
eclipse中如何跳转到指定行 :ctrl+L 然后输入行数 F5:跳入方法 F6:向下逐行调试 F7:跳出方法 F8:直接跳转到下一个断点 持续更新
- 著名清理软件(CCleaner) 5.24.5841 中文版
软件名称: 著名清理软件(CCleaner) 软件语言: 多国语言 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 5.7MB 图片预览: 软件简介: CCleaner的体积小, ...
- linux 安装php的redis拓展
安装步骤: #wget https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz #tar -zxvf 2.2.4 #cd phpredi ...
- HTTP 错误 404.8 - Not Found
HTTP 错误 404.8 - Not Found请求筛选模块被配置为拒绝包含 hiddenSegment 节的 URL 中的路径. 详细错误信息模块 RequestFilteringModule 通 ...
- Python学习笔记——进阶篇【第八周】———进程、线程、协程篇(Socket编程进阶&多线程、多进程)
本节内容: 异常处理 Socket语法及相关 SocketServer实现多并发 进程.线程介绍 threading实例 线程锁.GIL.Event.信号量 生产者消费者模型 红绿灯.吃包子实例 mu ...