Asteroids!(bfs)

Asteroids!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5527 Accepted Submission(s): 3514

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

#include<iostream>
#include<string.h>
#include<algorithm>
#include<string>
#include<queue>
#include<stdio.h>

using namespace std;
typedef struct LNode
{
    int x;
    int y;
    int z;
    int step;
}node;
int visit[15][15][15];
int dir[6][3] = {{1,0,0},{0,1,0},{-1,0,0},{0,-1,0},{0,0,1},{0,0,-1}};
int mp[15][15][15];
int sx,sy,sz;
int ex,ey,ez;
int n;
void bfs()
{
    if(sx==ex&&sy==ey&&sz==ez)
    {
        cout<<n<<" "<<0<<endl;
        return;
    }
    queue<LNode> q;
    node now ;
    now.x = sx;
    now.y = sy;
    now.z = sz;
    now.step = 0;
    visit[sx][sy][sz] = 1;
    q.push(now);
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        //cout<<now.step<<endl;
        if(now.x==ex&&now.y==ey&&now.z==ez)
        {
            cout<<n<<" "<<now.step<<endl;
            return;
        }
        for(int i =0;i<6;i++)
        {
            int xx = now.x + dir[i][0];
            int yy = now.y + dir[i][1];
            int zz = now.z + dir[i][2];
          //  cout<<xx<<" "<<yy<<" "<<zz<<endl;
            if(xx>=0&&yy>=0&&zz>=0&&xx<n&&yy<n&&zz<n&&mp[xx][yy][zz]&&!visit[xx][yy][zz])
            {
                node nxt ;
                nxt.x = xx;
                nxt.y = yy;
                nxt.z = zz;
                nxt.step = now.step + 1;
                visit[xx][yy][zz] = 1;
               // cout<<xx<<" "<<yy<<" "<<zz<<" "<<endl;
                q.push(nxt);
            }
        }
    }
    cout<<"NO ROUTE"<<endl;
}
int main()
{
    char tmp[7];
    while(cin>>tmp>>n)
    {
        for(int i =0;i<n;i++)
        {
            for(int j = 0;j<n;j++)
            {
                for(int k = 0;k<n;k++)
                {
                    char t;
                    cin>>t;
                    if(t=='X')
                    {
                        mp[i][j][k] =0;
                    }

                    else
                    {
                        mp[i][j][k] = 1;
                    }

                }
            }
        }
         //  cout<<sx<<sy<<sz<<endl;
          // cout<<ex<<ey<<ez<<endl;
             cin>>sz>>sy>>sx;
             cin>>ez>>ey>>ex;

        cin>>tmp;
        memset(visit,0,sizeof(visit));
        bfs();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值