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.

 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
 #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个不同方向的更多相关文章

  1. C#如何定义一个变长的一维和二维数组

    1.假设将要定义数组的长度为程序执行过程中计算出来的MAX List<int> Arc = new List<int>(); ; i < MAX; i++) { Arc. ...

  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. 编写一段代码,打印一个M行N列的二维数组转置。(交换行和列)

    import edu.princeton.cs.algs4.*; public class No_1_1_13 { public static void main(String[] args) { i ...

  4. [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 ...

  5. C#中如何获取一个二维数组的两维长度,即行数和列数?以及多维数组各个维度的长度?

    如何获取二维数组中的元素个数呢? int[,] array = new int[,] {{1,2,3},{4,5,6},{7,8,9}};//定义一个3行3列的二维数组int row = array. ...

  6. 计算机二级-C语言-程序设计题-190119记录-求出一个二维数组每一列的最小值。

    //编写一个函数:tt指向一个M行N列的二维数组,求出二维数组每列中最小的元素,并依次放入pp所指的一维数组中.二维数组中的数在主函数中赋予. //重难点:求出的是每一列的最小值,这里要注意,学会简化 ...

  7. SDUT OJ 图练习-BFS-从起点到目标点的最短步数 (vector二维数组模拟邻接表+bfs , *【模板】 )

    图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天 ...

  8. PHP如何判断一个数组是一维数组或者是二维数组?用什么函数?

    如题:如何判断一个数组是一维数组或者是二维数组?用什么函数? 判断数量即可 <?php if (count($array) == count($array, 1)) { echo '是一维数组' ...

  9. ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)

    1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 112[ ...

随机推荐

  1. 《APUE》第四章笔记(2)

    下面介绍对stat结构的各个成员的操作函数. 先贴个stat结构的图: access函数: #include <unistd.h> int access(const char *pathn ...

  2. QtSQL学习笔记(4)- 使用SQL Model类

    除了QSqlQuery,Qt提供了3个高级类用于访问数据库.这些类是QSqlQueryModel.QSqlTableModel和QSqlRelationalTableModel. 这些类是由QAbst ...

  3. mysql---union和左连接的两倒面试题

    第一道: 思路:无非是将hid与gid与t表中的tname关联起来.实质上是三表关联(m,t,t) 先将hid与tname关联起来,运用左连接 再将结果集与t表中的tname关联起来,使得gid与tn ...

  4. jQuery 中的防冲突(noConflict)机制

    许多的 JS 框架类库都选择使用 $ 符号作为函数或变量名,jQuery 是其中最为典型的一个.在 jQuery 中,$ 符号只是 window.jQuery 对象的一个引用,因此即使 $ 被删除,w ...

  5. Express框架学习总结

    最近学了Express框架,在学习的过程中,参考了一些资料,感觉Express框架比原生Node.js好用多了.下面我将我学习总结的内容如下: Express中文网     http://www.ex ...

  6. C# 格式化字符串(转载)

    1 前言 如果你熟悉Microsoft Foundation Classes(MFC)的CString,Windows Template Library(WTL)的CString或者Standard ...

  7. 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例

    wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...

  8. 【BZOJ1823】 [JSOI2010]满汉全席

    Description 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而 ...

  9. Spatial Pyramid Matching 小结

    Spatial Pyramid Matching 小结 稀疏编码系列: (一)----Spatial Pyramid 小结 (二)----图像的稀疏表示——ScSPM和LLC的总结 (三)----理解 ...

  10. Android编程中常用的PopupWindow和Dialog对话框

    注意:PopupWindow组件的使用问题,PopupWindow是一个阻塞对话框,如果你直接在Activity创建的方法中显示它,则会报错:android.view.WindowManager$Ba ...