Asteroids!

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

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:  1072 1241 1016 1010 1175 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
这道题的题目是真的长( ̄▽ ̄)"
还是英文
(小声BB)
但是这道题目看懂了过后发现就是
非常基础的三维bfs
和胜利大逃亡类似
几乎没有难度
于是就直接上手写≡(▔﹏▔)≡
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 //Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
struct node
{
int x,y,z,step;
};
char str[];
int n,vis[][][];
char map[][][];
int dir[][]={{-,,},{,,},{,-,},{,,},{,,-},{,,}};//三维,六个自由度,三个方向
int bfs(int a,int b,int c,int x,int y,int z)
{
int i;
queue<node> q;
q.push(node{a,b,c,});
vis[a][b][c]=;
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.x==x&&t.y==y&&t.z==z)
return t.step;
for(i=;i<;i++)
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
int dz=t.z+dir[i][];
if(dx>=&&dy>=&&dz>=&&dx<n&&dy<n&&dz<n&&!vis[dx][dy][dz]&&map[dx][dy][dz]=='O')//三维bfs
{
vis[dx][dy][dz]=;
q.push(node{dx,dy,dz,t.step+});
}
}
}
return -;
}
int main()
{
int a,b,c,x,y,z;
while(scanf("%s%d",str,&n)!=EOF)//吸收start
{
getchar();
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%s",map[i][j]);//输入map
}
}
scanf("%d%d%d%d%d%d",&a,&b,&c,&x,&y,&z);//输入起点和终点
scanf("%s",str);//吸收掉end
int ans=bfs(a,b,c,x,y,z);
if(ans==-)
printf("NO ROUTE\n");
else
printf("%d %d\n",n,ans);
}
return ;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

然后恭喜你

第二组样例

都过不了⊙﹏⊙∥

原因很简单

题意说的n是按照层数排列的

而这里的层数是z

而输入的时候x作为深度

显然就与题意不符了

这里有两种改法

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第一种:

 //Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
struct node
{
int x,y,z,step;
};
char str[];
int n,vis[][][];
char map[][][];
int dir[][]={{-,,},{,,},{,-,},{,,},{,,-},{,,}};//六个自由度,三个方向
int bfs(int a,int b,int c,int x,int y,int z)
{
int i;
queue<node> q;
q.push(node{a,b,c,});
vis[a][b][c]=;
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.x==x&&t.y==y&&t.z==z)
return t.step;
for(i=;i<;i++)
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
int dz=t.z+dir[i][];
if(dx>=&&dy>=&&dz>=&&dx<n&&dy<n&&dz<n&&!vis[dx][dy][dz]&&map[dx][dy][dz]=='O')//三维bfs
{
vis[dx][dy][dz]=;
q.push(node{dx,dy,dz,t.step+});
}
}
}
return -;
}
int main()
{
int a,b,c,x,y,z;
while(scanf("%s%d",str,&n)!=EOF)//吸收掉start
{
getchar();
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%s",map[i][j]);//输入map
}
}
scanf("%d%d%d%d%d%d",&c,&b,&a,&z,&y,&x);//注意输入的方向是反过来的,层数是z,输入的时候做了一个纠正
scanf("%s",str);//同理,吸收掉end
int ans=bfs(a,b,c,x,y,z);
if(ans==-)
printf("NO ROUTE\n");
else
printf("%d %d\n",n,ans);
}
return ;
}

这里在输入起点与终点方面交换了一下位置

效果拔群

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第二种:

 //Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
struct node
{
int x,y,z,step;
};
char str[];
int n,vis[][][];
char map[][][];
int dir[][]={{-,,},{,,},{,-,},{,,},{,,-},{,,}};//六个自由度,三个方向
int bfs(int a,int b,int c,int x,int y,int z)
{
int i;
queue<node> q;
q.push(node{a,b,c,});
vis[a][b][c]=;
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.x==x&&t.y==y&&t.z==z)
return t.step;
for(i=;i<;i++)
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
int dz=t.z+dir[i][];
if(dx>=&&dy>=&&dz>=&&dx<n&&dy<n&&dz<n&&!vis[dx][dy][dz]&&map[dx][dy][dz]=='O')//三维bfs
{
vis[dx][dy][dz]=;
q.push(node{dx,dy,dz,t.step+});
}
}
}
return -;
}
int main()
{
int a,b,c,x,y,z;
while(scanf("%s%d",str,&n)!=EOF)//吸收掉start
{
getchar();
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
for(int k=;k<n;k++)
cin>>map[k][j][i];//输入map的时候直接按照需求输入
}
}
scanf("%d%d%d%d%d%d",&a,&b,&c,&x,&y,&z);//输入起点和终点
scanf("%s",str);//同理,吸收掉end
int ans=bfs(a,b,c,x,y,z);
if(ans==-)
printf("NO ROUTE\n");
else
printf("%d %d\n",n,ans);
}
return ;
}

输入时按照需求输入

效果同样拔群

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Notes:看清题目,看清题目,看清题目(重要的事情说三遍)

2018-11-16  03:24:04  Author:LanceYu

HDU 1240 Asteroids! 题解的更多相关文章

  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)

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

  3. HDU 1240 Asteroids! 解题报告

    //这道题做完我只有 三个感受  第一:坑: 第二 : 坑! 第三:还是坑! 咳咳  言归正传  WA了无数次之后才发现是输入进去时坐标时z, y, x的顺序输入的 题解   :  类似胜利大逃亡 只 ...

  4. hdu 1240 Asteroids! (三维bfs)

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

  5. HDU 1240 Asteroids!

    三维广搜 #include <cstdio> #include <iostream> #include <cstring> #include <queue&g ...

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

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

  7. hdu 1240 Asteroids!(BFS)

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

  8. HDU 1240 Asteroids!【BFS】

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

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

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

随机推荐

  1. linux pkgsrc 学习(一) 安装pkgsrc

    使用pkgsrc.joyent.com 提供的linux 包 下载包 # # Copy and paste the lines below to install the 64-bit EL 7.x s ...

  2. ESA2GJK1DH1K升级篇: STM32远程乒乓升级,基于(Wi-Fi模块AT指令TCP透传方式),MQTT通信控制升级

    实现功能概要 前面的版本都是,定时访问云端的程序版本,如果版本不一致,然后下载最新的升级文件,实现升级. 这一节,在用户程序里面加入MQTT通信,执行用户程序的时候,通过接收MQTT的升级命令实现升级 ...

  3. ESP8266 SDK开发: 外设篇-GPIO输入检测

    前言 官方提供了以下函数检测引脚输入状态 检测GPIO5 if( GPIO_INPUT_GET(5) == 0 ) GPIO5当前为低电平 if( GPIO_INPUT_GET(5) == 1 ) G ...

  4. Bootstrap-table 增删改查

    1.引入bootstarp-table 系类的js/css文件 @*1.Jquery组件引用*@ <script src="~/Scripts/jquery-1.10.2.js&quo ...

  5. HTTP协议,到底是什么鬼?

    作者 | Jeskson 来源 | 达达前端小酒馆 了解HTTP HTTP是什么呢?它是超文本传输协议,HTTP是缩写,它的全英文名是HyperText Transfer Protocol. 那么什么 ...

  6. 源码解读SLF4J绑定日志实现的原理

    一.导读 我们使用log4j框架时,经常会用slf4j-api.在运行时,经常会遇到如下的错误提示: SLF4J: Class path contains multiple SLF4J binding ...

  7. Office 2016正式版/2019预览版 使用注意

    改写状态已经被隐藏 右击Word状态栏可以看到改写是否打开

  8. vue子组件数据跟着父组件改变

    父组件的代码 <template> <div class="home"> <img alt="Vue logo" src=&quo ...

  9. c++小学期大作业攻略(一)环境配置

    UPDATE at 2019/07/20 20:21 更新了Qt连接mysql的方法,但是是自己仿照连VS的方法摸索出来的,简单测试了一下能work但是不保证后期不会出问题.如果你在尝试过程中出现了任 ...

  10. Redis学习之对象系统源码分析

    背景知识: Redis并没有直接使用sds,双端链表,字典,压缩列表,跳表等这些数据结构来直接实现键值对数据库,而是基于这些对象创建了一个对象系统,这个对象系统包含5个对象:字符串对象,列表对象,哈希 ...