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] 总之,这是很 ...
随机推荐
- 5.Vue的组件
1.什么是组件 组件是可复用的Vue实例,也就是一组可以复用的模版,类似JSTL的自定义标签. 你可能会有页头.侧边栏.内容区等组件,每个组件又包含了其它的像导航链接.博文之类的组件. 2.第一个Vu ...
- [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
- PowerShell常用命令及美化(现代化的CMD)
PowerShell可谓现代终端,是微软用来替代古老的CMD的. PowerShell拥有面向对象的思想,非常方便. 常用命令 下载文件(此处以install.ps1文件为例) $client = n ...
- python开发--信息处理系统
#!/usr/bin/python card_list=[] #定义列表变量 def show_menu(): print("*" * 10) print("名片管理系统 ...
- mongodb 系列 ~ mongo 用户验证系列
MongoClientURI connectionString = new MongoClientURI("mongodb://root:****@dds-bp114e3f1fc441342 ...
- 妖娆的HTML
初涉前端之HTML 1.首先是Web服务的本质,是怎么工作的?(基于python的网络实现) import socket server = socket.socket() server.bind((' ...
- Jquery表单插件使用
一般表单提交都是同步,可以使用$.post进行异步提交,但这样意味着当表单属性很多的时候,要写的js也很多($("#xxx").val()获取属性的值后,在放入$.post第二个参 ...
- LInux 就该这么学 笔记分享
看了Linux就该这么学的前部分书,觉得写的还可以,就在网上找了下面这个同学写的笔记,觉得很详细,所以保存地址,供以后查阅参看.这里对作者表示感谢!!! 博客地址: https://www.cnblo ...
- json data转匿名对象C#
using Newtonsoft.Json.Linq; 代码如下: static void Main(string[] args) { Console.WriteLine("Test 4.8 ...