Asteroids! HDU 1240

Asteroids!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3163    Accepted Submission(s): 2110

点击打开1240
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  1253  1372  1241  1016

三维空间的BFS搜索题,和二维的差不多,注意x,y,z的顺序就好了。


题目大意:
输入到这个问题,将包括一个(非空白的)一系列多达100的数据集。每个数据集将根据以下的说明进行格式化,并且将有分离的数据集不空行。

单个数据集有5个组成部分:

开始线 - 一个单行, “ START N” ,其中1 < = N < = 10 。

切片列表 - N系列切片。每片起价为一个N× N矩阵通过小行星领域代表水平切片。矩阵中的每个位置将是两个值之一:

“O” - (字母“哦” )空场地

'X' - (大写)小行星

开始位置 - 一个单行, “ABC” ,意指<A,B,C>协调你的手艺的起始位置。坐标值将由独立的空间分隔的整数。

目标位置 - 一个单一的线, “DEF” ,意指<D,E,F>协调的目标的位置。坐标值将由独立的空间分隔的整数。

端线 - 一个单一的线, “ END”

该坐标系的原点是<0,0,0 > 。因此,每个坐标矢量的每个分量将是介于0和N- 1的整数,包括。

在一组第一坐标表示列。左栏= 0 。(相当于X坐标)

在一组表示该行的第二个坐标。顶行= 0 。(相当于Y坐标)
第三个坐标在一组表示切片。第一片= 0 (相当于Z坐标)。
无论是起始位置和目标位置将在‘O’的位置。

BFS通过队列来实现,这里用到了一个容器,queue,下面通过百科来了解一下:
queue类是为程序员提供了一个队列的功能的容器适配器,具体而言,一个FIFO(先入先出)的数据结构
在头文件<queue>中定义。
原型
template<
class T,
  class Container =std::deque<T>
> class queue;

成员函数

  • empty()判断队列空,当队列空时,返回true。
  • size()访问队列中的元素个数。
  • push()会将一个元素置入queue中。
  • front()会返回queue内的下一个元素(也就是第一个被置入的元素)。
  • back()会返回queue中最后一个元素(也就是最后被插入的元素)。
  • pop()会从queue中移除一个元素。
  • top()会返回queue中优先级最高的元素。
  • 注意:pop()虽然会移除下一个元素,但是并不返回它,front()和back()返回下一个元素但并不移除该元素。

详见:http://blog.csdn.net/tianshuai1111/article/details/7652479

代码:

#include <iostream>
#include<stdio.h>
#include<memory.h>
#include<queue>
using namespace std;
struct node
{
    int x,y,z,step;
    friend bool operator<(node n1,node n2)
    {
        return n1.step>n2.step;
    }
};
struct point
{
    int x, y,z;
} s,d;
int vis[11][11][11];//判读是否搜索过
int dir[][3]= {{1,0,0},{0,1,0},{-1,0,0},{0,-1,0},{0,0,1},{0,0,-1}};//方向数组
char map[11][11][11];//三维字符数组来存储
int n,m,k;
int go(node a)
{
    if(a.x<0||a.y<0||a.z<0||a.z>=n||a.x>=n||a.y>=n||map[a.z][a.x][a.y]=='X'||vis[a.z][a.x][a.y]==0)//判断能否行走
        return 0;
    return 1;
}
int bfs()
{
    int i;
    priority_queue<node> q;//建一个优先队列
    node a,next;
    a.x=s.x;
    a.y=s.y;
    a.z=s.z;
    a.step=0;
    q.push(a);
    vis[s.z][s.x][s.y]=0;
    while(!q.empty())//判断队列是否为空
    {
        a=q.top();//出队
        q.pop();
        if(a.x==d.x&&a.y==d.y&&a.z==d.z)//判断是否找到
        {
            return a.step;
        }
        for(i=0; i<6; i++)//遍历六个方向
        {
            next=a;
            next.x+=dir[i][0];
            next.y+=dir[i][1];
            next.z+=dir[i][2];
            if(!go(next))
                continue;
            if(map[next.z][next.x][next.y]=='O')
                next.step++;
                vis[next.z][next.x][next.y]=0;
              q.push(next);//入队
        }
    }
    return -1;
}
int main()
{
    int i,j;
    char start[6];
    while(~scanf("%s%d",start,&n))
    {
        k=0;
        getchar();
        memset(map,'\0',sizeof(map[0][0])*11);
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                scanf("%s",map[i][j]);
            }
            getchar();
        }
        scanf("%d%d%d",&s.x,&s.y,&s.z);
        scanf("%d%d%d",&d.x,&d.y,&d.z);
        getchar();
        scanf("%s",start);
        int ans=0;
        memset(vis,1,sizeof(vis));
        ans=bfs();
        if(ans==-1)
            printf("NO ROUTE\n");
        else
            printf("%d %d\n",n,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值