HDU 1240 Asteroids!

题目地址:点击打开链接

思路:三维空间求最短路,输入输出有点蛋疼啊

AC代码:

#include <iostream>
#include<queue>
#include<cstring>

using namespace std;

struct node
{
    int x,y,z,step;
}cf[1010];

int visit[15][15][15];
char map[15][15][15];
int n,endi,endj,endk;
int dir[][3] = {{0,-1,0},{1,0,0},{0,1,0},{-1,0,0},{0,0,1},{0,0,-1}};

bool judge(int x,int y,int z)
{
    if(x>=0 && y>=0 && z>=0 && x<n && y<n && z<n && !visit[x][y][z] && map[x][y][z] != 'X')
        return true;
    return false;
}
void bfs(int x,int y,int z)
{
    int i;
    queue<node> q;
    node in,out;
    in.x = x;
    in.y = y;
    in.z = z;
    in.step = 0;
    q.push(in);
    while(!q.empty())
    {
        out = q.front();
        q.pop();
        if(out.x == endi && out.y == endj && out.z == endk)
        {
            cout<<n<<" "<<out.step<<endl;
            return;
        }
        for(i=0; i<6; i++)
        {
            in.x = out.x + dir[i][0];
            in.y = out.y + dir[i][1];
            in.z = out.z + dir[i][2];
            if(judge(in.x,in.y,in.z))
            {
                in.step = out.step + 1;
                visit[in.x][in.y][in.z] = 1;
                q.push(in);
            }
        }
    }
    cout<<"NO ROUTE"<<endl;
}
int main()
{
    char s[10];
    int i,j,k,starti,startj,startk;
    while(cin>>s>>n)
    {
        memset(visit,0,sizeof(visit));
        for(k=0; k<n; k++)
        {
            for(i=0; i<n; i++)
            {
                for(j=0; j<n; j++)
                {
                    cin>>map[i][j][k];
                }
            }
        }
        cin>>starti>>startj>>startk>>endi>>endj>>endk;
        cin>>s;
        visit[starti][startj][startk] = 1;
        bfs(starti,startj,startk);
    }
    return 0;
}


你可能感兴趣的:(HDU 1240 Asteroids!)