POJ2225 Asteroids! (三维的BFS求最短路)


Asteroids!
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3017   Accepted: 1138

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:
  1. Start line - A single line, "START N", where 1 <= N <= 10.
  2. 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

  3. 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.
  4. 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.
  5. 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
 
    
题目大意:按z,x,y的顺序输入一张N*N*N的地图,O可以通过,X不能通过,问最短路径,输出时第一个数字为N,输入的START和END可忽略。
还是BFS搜最短路。不过一定注意是z,x,y的顺序,为这个贡献了WA。
代码(HDU1240 CE 原因不明):
 
    
 
    
 
    
#include 
#include 
#include 
#include 
#include 

using namespace std;

struct point
{
    int x;
    int y;
    int z;
    int step;
};

queue q;
point cur,next;
int vis[12][12][12],n,flag,lie,a,b,c,d,e,f,ans;
char s[12][12][12];

void bfs()
{
    int diry[6]={0,0,1,-1,0,0},dirz[6]={0,0,0,0,-1,1},dirx[6]={-1,1,0,0,0,0};
    int x0,y0,z0,x1,y1,z1,i;
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        x0=cur.x;
        y0=cur.y;
        z0=cur.z;
        if (x0==d&&y0==e&&z0==f)
        {
            ans=cur.step;
            flag=1;
        }
        for (i=0;i<=5;i++)
        {
            x1=x0+dirx[i];
            y1=y0+diry[i];
            z1=z0+dirz[i];
            if (x1<=n-1&&y1<=n-1&&z1<=n-1&&x1>=0&&y1>=0&&z1>=0&&vis[x1][y1][z1]==0&&s[x1][y1][z1]=='O')
            {
                next.x=x1;
                next.y=y1;
                next.z=z1;
                next.step=cur.step+1;
                vis[x1][y1][z1]=1;
                q.push(next);
            }
        }
    }
}

int main()
{
    int i,j,k;
    char u[102];
    while(scanf("%s",u)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        flag=0;
        scanf("%d",&n);
        i=0;
        getchar();
        for (i=0;i<=n-1;i++)
        {
            for (j=0;j<=n-1;j++)
            {
                for (k=0;k<=n-1;k++)
                {
                    scanf("%c",&s[k][j][i]);
                }
                getchar();
            }
        }
        scanf("%d%d%d",&a,&b,&c);
        scanf("%d%d%d",&d,&e,&f);
        cur.x=a;
        cur.y=b;
        cur.z=c;
        cur.step=0;
        q.push(cur);
        vis[a][b][c]=1;
        bfs();
        if (flag==0)
        {
            printf("NO ROUTE\n");
        }
        else
        {
            printf("%d %d\n",n,ans);
        }
        scanf("%s",u);
    }
    return 0;
}

 
   

你可能感兴趣的:(算法与ACM)