HDU 1240 Asteroid

简单bfs,找两点之间的最短路径
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

char graph[20][20][20];
bool vis[20][20][20];
int n,sx,sy,sz,ex,ey,ez;
int dx[] = {1,-1,0,0,0,0};
int dy[] = {0,0,1,-1,0,0};
int dz[] = {0,0,0,0,1,-1};

struct node{
    int x,y,z,step;
};

bool judge(int x,int y,int z){
    if(x<0 || y<0 || z<0 || x>=n || y>=n || z>=n || vis[x][y][z])
        return false;
    return true;
}

int bfs(){
    queue<node> q;
    node cur,next;
    cur.x = sx;
    cur.y = sy;
    cur.z = sz;
    cur.step = 0;
    vis[sx][sy][sz] = 1;
    q.push(cur);
    while(!q.empty()){
        cur = q.front();
        q.pop();
        if(cur.x==ex && cur.y==ey && cur.z==ez){
            return cur.step;
        }
        for(int i=0;i<6;i++){
            next = cur;
            next.x += dx[i];
            next.y += dy[i];
            next.z += dz[i];
            if(judge(next.x,next.y,next.z)){
                next.step++;
                vis[next.x][next.y][next.z] = true;
                q.push(next);
            }
        }
    }
    return -1;
}

int main(){
    char s[10];
    while(scanf("%s%d",s,&n)!=EOF){  //START N
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%s",graph[i][j]);
        scanf("%d%d%d%d%d%d",&sx,&sy,&sz,&ex,&ey,&ez);
        scanf("%s",s); //END
        int res = bfs();
        if(res>=0)
            printf("%d %d\n",n,res);
        else
            printf("NO ROUTE\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值