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] 总之,这是很 ...
随机推荐
- Mysql对表中 数据 的操作 DML
上一知识点回顾: mysql的备份: 直接使用navicat进行备份 转储SQL文件:有结构和数据/ 仅结构 两种 需要还原时 单击 数据库名字 运行SQL文件 创建表ctreate 修改表alt ...
- 5.Vue的组件
1.什么是组件 组件是可复用的Vue实例,也就是一组可以复用的模版,类似JSTL的自定义标签. 你可能会有页头.侧边栏.内容区等组件,每个组件又包含了其它的像导航链接.博文之类的组件. 2.第一个Vu ...
- CF1175D Array Splitting
题目链接 题意 给出一个长度为\(n\)的序列\(a\),要求分为恰好\(K\)段.第\(i\)个点的贡献是\(a_i \times f(i)\),\(f(x)\)表示x所属的是第几段. 思路 非常巧 ...
- C++中二分法之upper_bound()、lower_bound、binary_search()函数
前言 数组.容器vector都适用,在头文件"algorithm"中 下面的例子是针对容器的,注意返回的是距离元素3最近的指针it,输出的是*it结果为元素4,假如我想得到位置而非 ...
- Object.defineProperty 中的 writable 和 configurable 和 enumerable 的理解
在现在比较新的框架中, 比如 reactjs, vuejs中用得很多的一个属性便是 Object.defineOProperty 此属性的文档在网上一搜, 其中的几个属性, 包括 存取描述符(有set ...
- C# HTTP系列7 HttpWebRequest.Method属性
系列目录 [已更新最新开发文章,点击查看详细] HttpWebRequest.Method属性,获取或设置请求的方法.用于联系 Internet 资源的请求方法. 默认值为 GET. Syst ...
- openldap 指定普通用户登录ldap后可查看某分组下的用户信息
#ldap普通用户登录限制查看信息#在/openldap/slapd.conf文件最下面添加一下代码,可控制某个用户拥有查看用户信息的权限,而其他普通用户登录后无法查看用户信息,若有多个普通用户需要用 ...
- k8s本地部署
k8s是什么 Kubernetes是容器集群管理系统,是一个开源的平台,可以实现容器集群的自动化部署.自动扩缩容.维护等功能. Kubernetes 具有如下特点: 便携性: 无论公有云.私有云.混合 ...
- Github的初始设置
设置姓名和邮箱地址 git config --global user.name "Firstname Lastname" git config --global user.emai ...
- go-gin-api 规划目录和参数验证(二)
概述 首先同步下项目概况: 上篇文章分享了,使用 go modules 初始化项目,这篇文章咱们分享: 规划目录结构 模型绑定和验证 自定义验证器 制定 API 返回结构 废话不多说,咱们开始吧. 规 ...