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. Shell编程——位置参数变量

    1. (1)创建Shell脚本: (2)赋予执行权限: (3)执行Shell脚本: (4)对脚本赋值后执行脚本: $n:n为数字,$0代表命令本身,$1-9代表第一到第九个参数,十以上的参数用大括号包 ...

  2. 第四章、Go-面向“对象”

    4.1.结构体和方法 (1)go语言的面向对象 go仅支持封装,不支持继承和多态 go语言没有class,只有struct (2)struct的创建 package main import " ...

  3. TCP三次握手第三次握手时ACK丢失怎么办

    Server 端 第三次的ACK在网络中丢失,那么Server 端该TCP连接的状态为SYN_RECV,并且会根据 TCP的超时重传机制,会等待3秒.6秒.12秒后重新发送SYN+ACK包,以便Cli ...

  4. 在 Asp.Net Core 中安装 MVC

    在 ASP.NET Core 中安装 MVC 到目前为止,我们在本系列视频中使用的 ASP.NET Core 项目是使用“空”项目模板生成的.目前这个项目没有设置和安装 MVC. 两个步骤学会在 AS ...

  5. [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...

  6. Linux 启动数据库报错:could not open parameter file init**.ora

    sqlplus /nolog.conn /as sysdba.startup命令后显示 SQL> startupORA-01078: failure in processing system p ...

  7. gojs常用API-画布操作

    画布 获取当前画布的json myDiagram.model.toJson(); 加载json刷新画布 myDiagram.model = go.Model.fromJson(model); 删除选中 ...

  8. java登录点击验证码图片切换验证码无效

    1.问题:我在写一个登录时需要添加一个验证码的功能,但是第一次可以生成验证码,但是点击的时候无法发起请求. 2.解决方案:在请求地址后面加一个时间戳,保证每次请求都不一样就可以了! window.on ...

  9. 用 ubuntu 自带的 gome-screenshot 来实现类似QQ截图那样的功能,同时设置键盘快捷键

    在window下习惯了使用ctrl+Alt+A截图,在linux还真有点不习惯,所以下面介绍一下替代的用法. 打开 ubuntu 的系统设置-->键盘-->快捷键:界面如下: 01 添加一 ...

  10. docke通信之Linux 网络命名空间

    一.前言 namespace(命名空间)和cgroup是软件容器化(想想Docker)趋势中的两个主要内核技术.简单来说,cgroup是一种对进程进行统一的资源监控和限制,它控制着你可以使用多少系统资 ...