Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2664 Accepted Submission(s): 1794
Problem Description
You're in space. 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.
#include<stdio.h> #include<queue> using namespace std; typedef struct { int x,y,z,steps; }point; point start,end; int n; char map[][][]; int dir[][]={{,,}, {-,,}, {,,}, {,-,}, {,,}, {,,-}}; int bfs(point start) { queue<point>q; int i; point cur,next; if(start.x==end.x&&start.y==end.y&&start.z==end.z)//考虑起点和终点相同的情况 { return ; } start.steps=; map[start.x][start.y][start.z]='X'; q.push(start); while(!q.empty()) { cur=q.front();//取队首元素 q.pop(); for(i=;i<;i++) //广度优先搜索 { next.x=cur.x+dir[i][]; next.y=cur.y+dir[i][]; next.z=cur.z+dir[i][]; if(next.x==end.x && next.y==end.y && next.z==end.z) //下一步就是目的地 { return cur.steps+; } if(next.x>=&&next.x<n&&next.y>=&&next.y<n&&next.z>=&&next.z<n)//下一步不越界 if(map[next.x][next.y][next.z]!='X') //下一步不是星星 { map[next.x][next.y][next.z]='X'; next.steps=cur.steps+; q.push(next); } } } return -; } int main() { char str[]; int i,j,step; while(scanf("START %d",&n)!=EOF) { for(i=;i<n;i++) for(j=;j<n;j++) scanf("%s",map[i][j]); scanf("%d%d%d",&start.x, &start.y, &start.z); scanf("%d%d%d",&end.x, &end.y, &end.z); scanf("%s",str); gets(str); step=bfs(start); if(step>=) printf("%d %d\n",n,step); else printf("NO ROUTE\n"); } return ; }
HDU-1240 Asteroids! (BFS)这里是一个三维空间,用一个6*3二维数组储存6个不同方向的更多相关文章
-
C#如何定义一个变长的一维和二维数组
1.假设将要定义数组的长度为程序执行过程中计算出来的MAX List<int> Arc = new List<int>(); ; i < MAX; i++) { Arc. ...
-
HDU 1240 Asteroids!(BFS)
题目链接 Problem Description You're in space.You want to get home.There are asteroids.You don't want to ...
-
编写一段代码,打印一个M行N列的二维数组转置。(交换行和列)
import edu.princeton.cs.algs4.*; public class No_1_1_13 { public static void main(String[] args) { i ...
-
[CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组
13.10 Write a function in C called my2DAlloc which allocates a two-dimensional array. Minimize the n ...
-
C#中如何获取一个二维数组的两维长度,即行数和列数?以及多维数组各个维度的长度?
如何获取二维数组中的元素个数呢? int[,] array = new int[,] {{1,2,3},{4,5,6},{7,8,9}};//定义一个3行3列的二维数组int row = array. ...
-
计算机二级-C语言-程序设计题-190119记录-求出一个二维数组每一列的最小值。
//编写一个函数:tt指向一个M行N列的二维数组,求出二维数组每列中最小的元素,并依次放入pp所指的一维数组中.二维数组中的数在主函数中赋予. //重难点:求出的是每一列的最小值,这里要注意,学会简化 ...
-
SDUT OJ 图练习-BFS-从起点到目标点的最短步数 (vector二维数组模拟邻接表+bfs , *【模板】 )
图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天 ...
-
PHP如何判断一个数组是一维数组或者是二维数组?用什么函数?
如题:如何判断一个数组是一维数组或者是二维数组?用什么函数? 判断数量即可 <?php if (count($array) == count($array, 1)) { echo '是一维数组' ...
-
ytu 1050:写一个函数,使给定的一个二维数组(3&#215;3)转置,即行列互换(水题)
1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 154 Solved: 112[ ...
随机推荐
-
在Update表数据同时将数据备份
分享一条有意思的SQL语句,也是前两天有个朋友在面试的时候碰到的,他当时没有做出来,回来之后问我, 如何在同一条语句中实现,更新表的时候同时备份更新前的记录数据. --在修改数据前,先要把修改前的数据 ...
-
Java 动态代理作用是什么?
Java 动态代理作用是什么? 1 条评论 分享 默认排序按时间排序 19 个回答 133赞同反对,不会显示你的姓名 Intopass 程序员,近期沉迷于动漫ING 133 人赞同 ① 首先你 ...
-
关于Scrum团队的理解
<阅读完<构建之法>第6~7章>之读后感 阅读完<构建之法>第6~7章之后,不仅感觉获益匪浅,也甚感团队合作.分配.工作的不易与一个团队运营一个项目并推广的艰辛与 ...
-
DevExpress Ribbongallerybaritem选择性皮肤重组
void InitSkinGallery() () { SkinHelper skinHelper = new SkinHelper(); RibbonControl masterRibbonCont ...
-
转:strcmp函数实现及分析
转自:strcmp函数实现及详解 strcmp函数是C/C++中基本的函数,它对两个字符串进行比较,然后返回比较结果,函数形式如下:int strcmp(constchar*str1,constcha ...
-
poj1270 拓扑序(DFS)
题意:给出将会出现的多个字母,并紧接着给出一部分字母的大小关系,要求按照字典序从小到大输出所有符合上述关系的排列. 拓扑序,由于需要输出所有排列,所以需要使用 dfs ,只要点从小到大遍历就可以实现字 ...
-
PHP解码unicode编码中文字符代码示例
在抓取某网站数据,结果在数据包中发现了一串编码的数据:"......\u65b0\u6d6a\u5fae\u535a......", 这其实是中文被unicode编码后了的数据,想 ...
-
批量安装操作系统之cobbler
Cobbler 部署文档 服务端配置 操作系统:Centos6.4 关闭防火墙及 selinux 安装cobbler软件 添加yum源 rpm -Uvh https://dl.fedoraprojec ...
-
PHP中括号“{}”的3个作用
1,将多个独立语句合并为一个复合语句,例如“if .... else ....”中推荐如此使用. 2,在变量的间接引用中进行定界,避免歧义.例如“${$my_var[8]}”与“${$my_var}[ ...
-
yum fastermirror插件屏蔽一些国内源
最近被yum上hust源的问题恶心的受不了了,真不明白这种源还活着有什么意义,干脆关了得了,省得恶心人,经常Errno 14,404not found,去网页一看,好家伙,提示404的xml.gz路径 ...