HDU 1240 Asteroids! 题解
Asteroids!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6174 Accepted Submission(s): 3876
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.
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
3 4
NO ROUTE
//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! 题解的更多相关文章
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1240 Asteroids!(BFS)
题目链接 Problem Description You're in space.You want to get home.There are asteroids.You don't want to ...
- HDU 1240 Asteroids! 解题报告
//这道题做完我只有 三个感受 第一:坑: 第二 : 坑! 第三:还是坑! 咳咳 言归正传 WA了无数次之后才发现是输入进去时坐标时z, y, x的顺序输入的 题解 : 类似胜利大逃亡 只 ...
- hdu 1240 Asteroids! (三维bfs)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1240 Asteroids!
三维广搜 #include <cstdio> #include <iostream> #include <cstring> #include <queue&g ...
- 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!
给出一个三维的迷宫以及起点和终点,求能否到大终点,若果能输出最短步数 三维的问题无非就是变成了6个搜索方向 最后强调一下xyz的顺序,从输入数据来看,读入的顺序是map[z][x][y] 总之,这是很 ...
随机推荐
- 使用arthas 生成火焰图分析jvm
arthas 是阿里巴巴开源的强大的jvm 应该分析工具,以下是使用arthas 生成jvm 火焰图的一个学习 项目使用docker-compose 运行,对于生成的火焰图使用nginx 提供一个访问 ...
- ASP.NET Core 中的 Main 方法
ASP.NET Core 中的 Main 方法 在 ASP.NET Core 项目中,我们有一个名为Program.cs的文件.在这个文件中,我们有一个public static void Main( ...
- 【可视化】Vue基础
作者 | Jeskson 来源 | 达达前端小酒馆 Vue简介 Vue框架,框架的作者,尤雨溪,组件化,快速开发的特点. 生命周期 beforeCreate:组件刚刚被创建 created:组件创建完 ...
- [LeetCode] 264. Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- Elasticsearch由浅入深(五)_version乐观锁、external version乐观锁、partial update、groovy脚本实现partial update
基于_version进行乐观锁并发控制 先构造一条数据出来 PUT /test_index/test_type/ { "test_field": "test test&q ...
- Netty FixedChannelPool
如今越来越多的应用采用Netty作为服务端高性能异步通讯框架,对于客户端而言,大部分需求只需和服务端建立一条链接收发消息.但如果客户端需要和服务端建立多条链接的例子就比较少了. 最简单的实现就是一个f ...
- Nginx开启gzip提高页面加载速度
今天发现页面多次请求数据且加载的数据大,需要等待的时间很长 记得Nginx有gzip,可以对数据进行压缩 gzip是nginx服务器的ngx_http_gzip_module模块提供的在线实时数据压缩 ...
- Hbase flusher源码解析(flush全代码流程解析)
版权声明:本文为博主原创文章,遵循版权协议,转载请附上原文出处链接和本声明. 在介绍HBASE flush源码之前,我们先在逻辑上大体梳理一下,便于后续看代码.flush的整体流程分三个阶段 1.第一 ...
- 【More Effective C++ 条款2】最好使用C++转型操作符
C的转型方式存在以下两个缺点: 1)几乎允许你将任何类型转化为任何类型,不能精确的指明转型意图,这样很不安全 如将一个pointer-to-base-class-object转型为一个pointer- ...
- javascirpt的json.stringify()方法在IE浏览器兼容性模式下出错的原因与解决办法
今天开机混底薪的时候遇到一个JSON.stringify()在IE浏览器兼容模式下的问题. 问题描述 一个弹窗选择的功能原来好好的,突然就不行了. 想要调试调试不了,报错信息也看不到(一开F12这破I ...