AsteroidsのBFS解題報告

6163 ワード

Asteroids!
Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2427    Accepted Submission(s): 1645
Problem Description
You're in space. You want to get home. There are asteroids. You don't want to hit them.
 
 
Input
Input to thisproblem will consist of a (non-empty) series of up to 100 data sets. Each dataset will be formatted according to the following description, and there will beno 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 ahorizontal slice through the asteroid field. Each position in the matrix willbe 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 coordinates of your craft's starting position. The coordinatevalues will be integers separated by individual spaces. Target Position - A single line, "D E F", denoting the coordinates of your target's position. The coordinate values will be integersseparated by individual spaces. End line - A single line, "END" The origin of the coordinate system is <0,0,0>. Therefore, each componentof 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 linesseparating output sets. A single output set consists of a single line. If a route exists, the line willbe in the format "X Y", where X is the same as N from thecorresponding input data set and Y is the least number of moves necessary toget your ship from the starting position to the target position. If there is noroute 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 ordecrement 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
 
 
テーマ:
       あなたは今宇宙にいますが、地球に帰りたいです.星空には多くの惑星があります.あなたがしなければならないのはこれらの惑星を避けることです.本題の難点は,与えられた3次元空間における惑星の分布図であり,Xは惑星領域を表す ,大文字Oは空白領域を表し、次はあなたの開始点座標、目的の座標です.もちろん探索はそうであるが,目的地に到達できるか否かを求める必要があり,できればNと最小のステップ数を出力し,そうでなければNO ROUTEを出力する必要がある.
大まかな考え方:
    この問題は同様に広義検索の古典的な問題であり,迷宮を歩くのとは異なり3次元空間構造に変換され,問題に記述されているように,3次元配列がデータ分布を表すことを定義する際に,2つの方法で入力することができる.1は、map[i][j][k]を3つの変数、すなわち3重サイクルで入力する.2,スライス層を二重ループ,すなわち3次元で表し,map[i][j]と入力する.方向は従来の4つから6つに変化した.すなわちdir[6]={0,0,1},{0,−1,0},{0,1,0},{0,1,0},{1,0,0}である.データ入力後も、最も重要なのはBFSアルゴリズムの定義ですが、アクセスした場所には「X」と表記するか、2つのVisited[MAX][MAX][MAX]と表記する必要があることに注意してください.
#include <iostream>
#include <stdio.h>
#include<memory.h>
#include <queue>
#define N 9
using namespace std;

int dir[6][3]={{0,0,1},{0,0,-1},{0,-1,0},{0,1,0},{1,0,0},{-1,0,0}};    //    ,      
char map[N][N][N];      //          
int visit[N][N][N];     //             ,       1
int stx,sty,stz,edx,edy,edz,flag;       //          
struct node
{
    int x,y,z;
    int step;
};

int BFS(int n)
{
    int i;
    flag=0;             //flag   ,           ,        
    node start,cur,next;
    memset(visit,0,sizeof(visit));
    queue<node>q;
    start.x=stx;
    start.y=sty;
    start.z=stz;
    start.step=0;
    visit[stx][sty][stz]=1;
    q.push(start);              //       
    while(!q.empty())           //       
    {
        cur=q.front();
        q.pop();
        for(i=0;i<6;i++)        //                  
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            next.z=cur.z+dir[i][2];
            next.step=cur.step+1;
            if(next.x<n&&next.y<n&&next.z<n&&next.x>=0&&next.y>=0&&next.z>=0&&map[next.x][next.y][next.z]=='O'&&visit[next.x][next.y][next.z]==0)
            {
                if(next.x==edx&&next.y==edy&&next.z==edz) //     ,    
                {
                    flag=1;
                    return next.step;
                }
                visit[next.x][next.y][next.z]=1;
                q.push(next);
            }
        }
    }
    return 0;
}

int main()
{
    char strSt[10],strEd[10];
    int n,sum,i,j,k;
    while(cin>>strSt&&cin>>n)
    {
        for(k=0;k<n;k++)
            for(i=0;i<n;i++)
                for(j=0;j<n;j++)
                    cin>>map[i][j][k];
        cin>>stx>>sty>>stz>>edx>>edy>>edz>>strEd;
        if(stx==edx&&sty==edy&&stz==edz)        //            ,      0
        {
            cout<<n<<' '<<0<<endl;
//            continue;
        }
        else
        {
            sum=BFS(n);
            if(flag==0)
                cout<<"NO ROUTE"<<endl;
            else
                cout<<n<<' '<<sum<<endl;
        }
    }
    return 0;
}