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): 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
    #include<stdio.h>
    #include<iostream>
    #include<cstdlib>
    #include<queue>
    #define HH 1111111
    using namespace std;
    int map[6][3]={ {0,0,1},{0,1,0},{0,-1,0},{0,0,-1},{1,0,0},{-1,0,0} };
    char f[11][11][11];
    int visit[11][11][11];
    bool use[11][11][11];
    int n;
    struct st
    {
        int x;
        int y;
        int z;
    };
    st q,zd;
    int bfs()
    {
        int x1,y1,z1,i;
        queue<st>Q;
        st tmp,tmp1;
        tmp.x=q.x;tmp.y=q.y;tmp.z=q.z;
        Q.push(tmp);
        visit[q.z][q.x][q.y]=0;
    //    use[q.z][q.x][q.y]=true;
        while(Q.size()>0)
        {
            tmp=Q.front();
            Q.pop();
    //        use[tmp.z][tmp.x][tmp.y]=false;
            for(i=0;i<6;i++)
            {
                x1=tmp.x+map[i][1];
                y1=tmp.y+map[i][2];
                z1=tmp.z+map[i][0];
                if(x1>=1&&x1<=n && y1>=1&&y1<=n && z1>=1&&z1<=n)
                {
                    if(f[z1][x1][y1]=='O' && visit[z1][x1][y1]==HH)
                    {
                        visit[z1][x1][y1]=visit[tmp.z][tmp.x][tmp.y]+1;
                        tmp1=tmp;
                        tmp.x=x1;tmp.y=y1;tmp.z=z1;
                        Q.push(tmp);
                        tmp=tmp1;
                    }
    
                }
            }
        }
        return visit[zd.z][zd.x][zd.y];
    }
    int main()
    {
        int i,j,k;
        char b[10];
        while(scanf("%s%d",b,&n)>0)
        {
            for(i=1;i<=n;i++)
                for(j=1;j<=n;j++)
                        scanf("%s",f[i][j]+1);
            scanf("%d%d%d",&q.x,&q.y,&q.z);
            scanf("%d%d%d",&zd.x,&zd.y,&zd.z);
            getchar();
            q.x++;q.y++;q.z++;
            zd.x++;zd.y++;zd.z++;
            scanf("%s",b);
            for(i=1;i<=n;i++)
                for(j=1;j<=n;j++)
                    for(k=1;k<=n;k++)
                        visit[i][j][k]=HH;
            memset(use,false,sizeof(use));
            k=HH;
            k=bfs();
            if(k==HH) printf("NO ROUTE
    ");
            else
                printf("%d %d
    ",n,k);
        }
        return 0;
    }
     
  • 相关阅读:
    使用IDEA打包出现“Cleaning up unclosed ZipFile for archive”错误
    Kafka读取本地文件作为生产者
    Redis连接池
    如果javaapi长时间消费不到数据
    Redis 持久化的两种方式
    kafka结合streaming的两种方式
    二分法查找
    shell脚本学习
    Md5Utils
    idea添加jar包
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3201606.html
Copyright © 2011-2022 走看看