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

    Asteroids!

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


    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 1372 1180 1239 1254 
     
     
    这道题的错了好久,一直过不了测试数据,后来发现它的迷宫输入有所不同,必须先输入第一层,也就是说z坐标轴等于0,等输入完第一层再将z坐标轴变为1。也就是说z坐标轴变化的要最慢,放在第一层循环。所以做题一定要细心。
     
    题意:先输入一行START N,“START”为字符串,表示开始,忽略即可。N是地图大小,为N*N*N。然后输入N*N行,每行N个字符,每N行表示一层,有N层,O表示可以走,X表示不能,再输入起点和终点,能走到,输出n和步数,不能,则输出“NO ROUTE”
     
    附上代码:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 using namespace std;
     6 
     7 int f[6][3]= {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};  //6个方向的模拟
     8 struct node
     9 {
    10     int x,y,z,t;
    11 } s1,s2;
    12 int n,a1,b1,c1,a2,b2,c2;
    13 int visit[15][15][15];
    14 char map[15][15][15];
    15 
    16 void BFS()
    17 {
    18     queue <node> q;
    19     while(!q.empty())
    20         q.pop();
    21     s1.x=c1;   //注意这里的变化!!
    22     s1.y=b1;
    23     s1.z=a1;
    24     s1.t=0;
    25     visit[c1][b1][a1]=0;
    26     q.push(s1);
    27     while(!q.empty())
    28     {
    29         s1=q.front();
    30         q.pop();
    31         if(s1.x==c2&&s1.y==b2&&s1.z==a2)
    32         {
    33             printf("%d %d
    ",n,s1.t);
    34             return;
    35         }
    36         for(int i=0; i<6; i++)
    37         {
    38             s2.x=s1.x+f[i][0];
    39             s2.y=s1.y+f[i][1];
    40             s2.z=s1.z+f[i][2];
    41             if(s2.x>=0&&s2.x<n&&s2.y>=0&&s2.y<n&&s2.z>=0&&s2.z<n&&visit[s2.x][s2.y][s2.z])
    42             {
    43                 //cout<<s2.x<<" "<<s2.y<<" "<<s2.z<<endl;
    44                 visit[s2.x][s2.y][s2.z]=0;
    45                 s2.t=s1.t+1;
    46                 q.push(s2);
    47             }
    48         }
    49     }
    50     printf("NO ROUTE
    ");
    51     return;
    52 }
    53 
    54 int main()
    55 {
    56     char str[10];
    57     int k,i,j;
    58     while(~scanf("%s %d",str,&n))
    59     {
    60         memset(visit,0,sizeof(visit));
    61         for(i=0; i<n; i++)
    62             for(j=0; j<n; j++)
    63                 for(k=0; k<n; k++)
    64                 {
    65                     cin>>map[i][j][k];
    66                     if(map[i][j][k]=='O')
    67                         visit[i][j][k]=1;
    68                 }
    69         scanf("%d %d %d",&a1,&b1,&c1);  //起点
    70         scanf("%d %d %d",&a2,&b2,&c2);   //终点
    71         scanf("%s",str);     //别忘了还有一个字符串的输入
    72         BFS();
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    6-1面向对象
    5-1模块
    python随机数
    4-5目录
    4-4内置函数
    4-3迭代器和生成器
    4-1装饰器1
    4-2装饰器2
    3-4函数-全局变量
    3-5递归-函数
  • 原文地址:https://www.cnblogs.com/pshw/p/4754821.html
Copyright © 2011-2022 走看看