zoukankan      html  css  js  c++  java
  • hdu 1240 Asteroids!

    http://acm.hdu.edu.cn/showproblem.php?pid=1240

    Asteroids!

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


    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
     比较水3维bfs
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 using namespace std;
     5 char map[20][20][20];
     6 int vis[20][20][20];
     7 int dir[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
     8 int a,b,c,d,e,f,n,flag,ans;
     9 struct node
    10 {
    11      int x;
    12      int y;
    13      int z;
    14      int step;
    15 }info;
    16 void bfs(int x,int y,int z)
    17 {
    18      int i;
    19      vis[z][y][x]=1;
    20      info.x=z,info.y=y,info.z=x,info.step=0;
    21      queue<node>q;
    22      q.push(info);
    23      while(!q.empty())
    24      {
    25           node pos;
    26           pos=q.front();
    27           q.pop();
    28           if(pos.x==f&&pos.y==e&&pos.z==d&&map[pos.x][pos.y][pos.z]!='X')
    29           {
    30                flag=1;
    31                ans=pos.step;
    32                return;
    33           }
    34           for(i=0;i<6;i++)
    35           {
    36                int xx=pos.x+dir[i][0],yy=pos.y+dir[i][1],zz=pos.z+dir[i][2],step=pos.step+1;
    37                if(xx>=0&&xx<n&&yy>=0&&yy<n&&zz>=0&&zz<n&&!vis[xx][yy][zz]&&map[xx][yy][zz]!='X')
    38                {
    39                     vis[xx][yy][zz]=1;
    40                //printf("x=%d,y=%d,z=%d,step=%d
    ",yy,zz,xx,step);
    41                     info.x=xx;
    42                     info.y=yy;
    43                     info.z=zz;
    44                     info.step=step;
    45                     q.push(info);
    46 
    47                }
    48           }
    49      }
    50 }
    51 int main()
    52 {
    53      int i,j,str[10];
    54      while(scanf("%s %d",str,&n)!=EOF)
    55      {
    56           flag=0;
    57           ans=0;
    58           memset(map,0,sizeof(map));
    59           memset(vis,0,sizeof(vis));
    60           for(i=0;i<n;i++)
    61              for(j=0;j<n;j++)
    62               scanf("%s",map[i][j]);
    63               scanf("%d %d %d",&a,&b,&c);
    64               scanf("%d %d %d",&d,&e,&f);
    65               scanf("%s",str);
    66               bfs(a,b,c);
    67               if(flag)
    68               printf("%d %d
    ",n,ans);
    69               else
    70               printf("NO ROUTE
    ");
    71 
    72      }
    73      return 0;
    74 }
    View Code
  • 相关阅读:
    数据结构 练习 22-并查集以及图的最小生成树
    C# 上传RAR文件 解压 获取解压后的文件名称
    [置顶] 程序员学数据库那些事儿
    编程挑战:字符串的完美度
    Hibernate主键生成策略
    利用冒泡排序对数组进行排序
    小学生玩ACM----广搜
    CGContext绘图
    [置顶] 《Windows编程零基础》__2 一个完整的程序
    java 网络编程
  • 原文地址:https://www.cnblogs.com/llei1573/p/3200352.html
Copyright © 2011-2022 走看看