hdu-1240-Asteroids!

来源:互联网 发布:linux 解压缩zip 编辑:程序博客网 时间:2024/05/16 06:25
题目大意:你现在在太空中,但是你想回地球,星空中有很多的行星,你要做的就是避开这些行星。
给定三维空间中行星的分布图,X代表着行星区域 ,大写字母O代表着空白区域,接下来是你的起始点坐标,目的的坐标。
你需要求出能否到达目的地,如果可以你还需要输出N和最小的步数,否则输出NO ROUTE。
需要注意的就是输入分布图,然后广搜,ok了。我刚开始就是因为输入上的一点失误卡住了,只要开一个三位数组就行了。
 

Asteroids!

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


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 1O0 0 00 0 0ENDSTART 3XXXXXXXXXOOOOOOOOOXXXXXXXXX0 0 12 2 1ENDSTART 5OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXXXXXXXXXXXXXXXXXXXXXXXXXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO0 0 04 4 4END
 

Sample Output
1 03 4NO ROUTE
 
#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>using namespace std;struct nobe{    int x,y,z,time;}q[3000];char map[10][10][10];int visit[10][10][10],n,m,l,t,k;int go[6][3]={0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0},d[3000],flag;void BFS(int a,int b,int c){    struct nobe que,ue;     int front=0,rear=0;    int nx,ny,nz,i;    que.x=a;    que.y=b;    que.z=c;    que.time=0;    q[rear++]=que;    while(front<rear)    {        ue=q[front++];        if(ue.x==n&&ue.y==m&&ue.z==l)        {            flag=1;            return ;        }        for(i=0;i<6;i++)        {            nx=ue.x+go[i][0];            ny=ue.y+go[i][1];            nz=ue.z+go[i][2];            if(!visit[nz][nx][ny]&&map[nz][nx][ny]=='O'&&nx>=0&&nx<=n&&ny>=0&&ny<=m&&nz>=0&&nz<=l)            {                visit[nz][nx][ny]=1;                que.time=nx*k+ny+nz*k*k;                que.x=nx;                que.y=ny;                que.z=nz;                d[que.time]=d[ue.time]+1;                q[rear++]=que;            }        }    }}int main(){    int i,j,o,n1,m1,l1;    char nima[10];    while(~scanf("%s%d",nima,&k))    {        for(o=0;o<k;o++)        {            for(i=0;i<k;i++)            {                for(j=0;j<k;j++)                {                    cin>>map[o][i][j];                    visit[o][i][j]=0;                    d[o*k*k+i*k+j]=0;                }            }        }        scanf("%d%d%d%d%d%d%s",&n1,&m1,&l1,&n,&m,&l,nima);        flag=0;        visit[l1][n1][m1]=1;        BFS(n1,m1,l1);        if(flag)        printf("%d %d\n",k,d[l*k*k+n*k+m]);        else        printf("NO ROUTE\n");    }}


原创粉丝点击