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

    Asteroids!

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


    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
     
    AC代码(BFS+QUEUE)
     1 #include<iostream>
     2 #include<fstream>
     3 #include<queue>
     4 #include<string>
     5 #include<cstring>
     6 using namespace std;
     7 const int MAX=13;
     8 struct pot
     9 {
    10     int x,y,k;
    11 };
    12 struct data
    13 {
    14     int time; char ch;
    15 };
    16 data arr[MAX][MAX][MAX];
    17 int x1,y1,k1,x2,y2,k2;
    18 int dir[6][3]={{1,0,0},{0,1,0},{-1,0,0},{0,-1,0},{0,0,-1},{0,0,1}};
    19 int n;
    20 int BFS()
    21 {
    22     pot beg={x1,y1,k1};
    23     queue<pot> que;
    24     que.push(beg);
    25     while(!que.empty())
    26     {
    27         pot cur=que.front();
    28         que.pop();
    29         if(cur.x==x2&&cur.y==y2&&cur.k==k2)
    30             return arr[cur.k][cur.x][cur.y].time;
    31         for(int i=0;i<6;i++)
    32         {
    33             pot next={cur.x+dir[i][0],cur.y+dir[i][1],cur.k+dir[i][2]};
    34             if(next.x>=0&&next.x<n&&
    35                next.y>=0&&next.y<n&&
    36                next.k>=0&&next.k<n&&
    37                arr[next.k][next.x][next.y].ch!='X')
    38                 if(arr[next.k][next.x][next.y].time==0||arr[next.k][next.x][next.y].time>arr[cur.k][cur.x][cur.y].time+1)
    39                 {
    40                     arr[next.k][next.x][next.y].time=arr[cur.k][cur.x][cur.y].time+1;
    41                     que.push(next);
    42                 }
    43         }
    44     }
    45     return -1;
    46 }
    47 int main()
    48 {
    49     //ifstream in("data.txt");
    50     string sign;
    51     while(cin>>sign>>n)
    52     {
    53         int k,i,j;
    54         for(k=0;k<n;k++)
    55             for(i=0;i<n;i++)
    56                 for(j=0;j<n;j++)
    57                 {
    58                     cin>>arr[k][i][j].ch;
    59                     arr[k][i][j].time=0;
    60                 }
    61         cin>>x1>>y1>>k1>>x2>>y2>>k2;
    62         cin>>sign;
    63         int result=BFS();
    64         if(result>=0)
    65             cout<<n<<" "<<result<<endl;
    66         else
    67             cout<<"NO ROUTE"<<endl;
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    遇见Javascript类型数组
    编译Android常用命令
    V4L2驱动视频开发要点
    Windows Phone开发(27):隔离存储A
    Ubuntu10.04下Android开发环境搭建
    V4L2开发要点
    使用 php Header 报错的一个原因
    Windows Phone开发(28):隔离存储B
    用HTML5 Audio API开发游戏音乐
    php备份数据库类分享
  • 原文地址:https://www.cnblogs.com/zhaopeng938/p/7920189.html
Copyright © 2011-2022 走看看