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个方向. 第一维代表在 ...
随机推荐
- equals和hashcode为什么要一起重写
equals()方法与hashCode()的通用协定是:2.1 如果两个对象相等(equal),那么必须拥有相同的哈希码(hash code)2.2 即使两个对象有相同的哈希值(hash code), ...
- C# Where
判断一个字符串中是否包含字符串数组里的字符,恶意字符限制提交,一般人,包括最初的我,会这样写 public bool ValidateStr(string[] parms) { bool result ...
- ios在项目中打开word文档、ppt等总结
最近在项目开发中遇到下载附件文档预览需求,在这里总结一下我的实现方法,本文最后会附带我写的demo下载地址 这里我总结了三种实现方法(1)用webView预览(2)通过UIDocumentIntera ...
- 【优先队列】-HDU4546比赛难度
比赛难度 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- get与post请求
1.post请求 如果表单以Post方式提交过来的,接收时必须以Requert.Form来接收,并且表单元素必须有name属性,而Form指定的键的名称就是name属性的值 <form meth ...
- PXE+kickstart自动安装ubuntu14.04
本文参考了诸多文章,先感谢这些文章的作者. 使用pxe安装系统需要安装dhcp,tftp,http等服务(当然也可以使用其他文件共享方式比如nfs,ftp). 实验环境: 1. vmware 12 2 ...
- XTU 1245 Hamiltonian Path
$2016$长城信息杯中国大学生程序设计竞赛中南邀请赛$C$题 简单题. 注意题目中给出的数据范围:$1 \le ai < bi \le n$,说明这是一个有向无环图,并且哈密顿路一定是$1 \ ...
- ap143 led修改
以前的硬件版本只使用了一个eth口,新的板子增加了一个eth口,并且增加了响应的通信时使用的灯. led修改涉及到一下的内容: (1)led 对应的gpio口(这个需要根据原理图来定义) 需要修改的源 ...
- eclipse背景主题
长时间对着电脑敲代码,眼睛受不了啊,果然程序员就没人权吗?之前咱给codeblocks背景换了颜色,去官网上粘粘代码然后替换一下就行了,这里不多说, 这里说的是给ecllipse换主题,按理说ecli ...
- C++知识体系
基础知识 推荐书目 C++ <C++程序设计>(课程教材即可,简而薄) <STL源码剖析>(对C++进一步深化,也是必备知识) <C++对象模型>(经典中经典,重点 ...