HDU 1240 Asteroids! 三维空间BFS

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

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>
#include <cstring>
#include <queue>
using namespace std;
struct node{
    int x,y,z;
    int step;
    friend bool operator<(node a,node b)
    {
        return a.step>b.step;
    }   
};
int N;
char edge[15][15][15];
int sta[15][15][15];
int sx,sy,sz,ex,ey,ez;
int dirx[6]={1,-1,0,0,0,0};
int diry[6]={0,0,1,-1,0,0};
int dirz[6]={0,0,0,0,1,-1};
int mina;
bool Judge(int x,int y,int z,int t)
{
    if(x<0||x>=N||y<0||y>=N||z<0||z>=N||edge[x][y][z]=='X'||sta[x][y][z]<=t)
    return true;
    return false;

}
bool BFS()
{
    priority_queue<node>Q;
    node cur,next;
    cur.x=sx;
    cur.y=sy;
    cur.z=sz;
    cur.step=0;
    sta[sx][sy][sz]=0;
    Q.push(cur);
    while(!Q.empty())
    {
        cur=Q.top();
        Q.pop();
        if(cur.x==ex&&cur.y==ey&&cur.z==ez)
        {
            mina=cur.step;
            return true;
        }       
        for(int i=0;i<6;i++)
        {
            next.x=cur.x+dirx[i];
            next.y=cur.y+diry[i];
            next.z=cur.z+dirz[i];
            next.step=cur.step+1;
            if(next.x==ex&&next.y==ey&&next.z==ez)
            {Q.push(next);
                continue;
            }
            if(Judge(next.x,next.y,next.z,next.step))
            continue;
            sta[next.x][next.y][next.z]=next.step;
            Q.push(next);
        }
    }

    return false;
}
int main(int argc, char *argv[])
{
    string s1,s2;
    while(cin>>s1)
    {
        //if(s1=="END")
        //cout<<11<<endl;
        //break;
        cin>>N;
        for(int k=0;k<N;k++)
        for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
        cin>>edge[k][i][j];
        cin>>sx>>sy>>sz;
        cin>>ex>>ey>>ez;
        cin>>s2;
        if(sx==ex&&sy==ey&&sz==ez)
        {
            cout<<N<<" "<<0<<endl;
        }
        else 
        {
            for(int k=0;k<N;k++)
            for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
            sta[i][j][k]=999;
            if(BFS())
            {
                cout<<N<<" "<<mina<<endl;
            }
            else
            cout<<"NO ROUTE"<<endl;

        }

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值