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个方向. 第一维代表在 ...
随机推荐
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树
题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...
- [Jenkins]怎么删除jenkins里项目配置的svn记录
问题出现原因: 当持续集成项目运行一段时间之后,你会发现不知不觉中,配置了不少过往的SVN账号,都已经不能用了,失效了,影响选择有时候,想要删除 问题解决办法: 1.选择$JENKINS_HOME/c ...
- Kmplayer播放器 绿色免安装版 2016 中文版
软件名称: Kmplayer播放器 绿色免安装版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 42.8MB 图片预览: 软件简介: Kmplayer播放 ...
- python字符串和列表
import sys#sys.argv[0] 被设定为指定模块的全名#脚本名和附加参数传入一个名为 sys.argv 的字符串列表.你能够获取这个列表通过执行 import sys,列表的长度大于等于 ...
- 一个用于上传文件的servlet
1.jsp页面操作文件: <%@ page language="java" import="java.util.*" pageEncoding=" ...
- asp.net——初识多线程
1.首先讲解一下什么是线程(该定义是参考线程的百度百科) 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针( ...
- 关于php数组是否要声明
不知道大家有没有这样玩过php数组,正常情况下直接不声明,直接使用数组,会发现输出的与声明后的结果是一样的.
- XTU 1250 Super Fast Fourier Transform
$2016$长城信息杯中国大学生程序设计竞赛中南邀请赛$H$题 排序,二分. 对$a$数组,$b$数组从小到大进行排序. 统计每一个$a[i]$作为较大值的时候与$b[i]$对答案的贡献.反过来再统计 ...
- <hdu - 1280> 前M大的数 (注意其中的细节)
这是杭电hdu上的链接http://acm.hdu.edu.cn/showproblem.php?pid=1280 Problem Description: 还记得Gardon给小希布置的那个作业么 ...
- 在spring拦截器中response输出html标签到页面
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object ...