猫史档案馆


【dfs和bfs的区别】水贴

用户:yee089yee089查看:5 回复:6 评论:5 创建时间:2022-03-03T08:00:27


const dir =  [new Box3Vector3(-1,0,0),new Box3Vector3(0,0,-1),new Box3Vector3(0,0,1),new Box3Vector3(1,0,0) ];
console.clear();
var voxel = voxels.id('zero');
async function bfs(x,y,z){
    let queue = [new Box3Vector3(0,0,0),new Box3Vector3(0,0,0),new Box3Vector3(0,0,0)];
    queue.pop();
    queue.pop();
    queue.pop();
    let stpq=[0,0,0];
    stpq.pop();stpq.pop();
    
    queue.push(new Box3Vector3(x,y,z));
    world.say(x + ' ' + y +' ' + z);
    
    while(queue.length > 0){
        let now = queue[0];
        queue.shift();
        let stp = stpq[0];
        stpq.shift();
        for(let i = 0; i < 4; i++){

            let nowx = now.x+dir[i].x;
            let nowy = now.y+dir[i].y;
            let nowz = now.z+dir[i].z;
            if(voxels.getVoxel(nowx,nowy,nowz) == voxels.id('grass')){
                if(stp < 35){
                    voxels.setVoxelId(nowx,nowy,nowz,voxel+stp*2+2);
                    queue.push(new Box3Vector3(nowx,nowy,nowz));
                    stpq.push(stp+1);
                }
           }
        }
        await sleep(1);
    }
    console.log('finishbfs');
}

bfs(90,8,90);


async function dfs(x1,y1,z1,stp){
    if(stp == 35) return;
    
        for(let i = 0; i < 4; i++){

            let nowx = x1+dir[i].x;
            let nowy = y1+dir[i].y;
            let nowz = z1+dir[i].z;
            if(voxels.getVoxel(nowx,nowy,nowz) == voxels.id('grass')){
                if(stp < 36){
                    voxels.setVoxelId(nowx,nowy,nowz,voxel+stp*2+2);
                    dfs(nowx,nowy,nowz,stp+1);
                }
           }
           await sleep(100);
        }
        await sleep(1000);
}
dfs(36,8,36,0)
voxels.setVoxel(36,8,36,'zero')
voxels.setVoxel(90,8,90,'zero')
world.onPlayerJoin(({entity})=>{
    entity.player.canFly=true;
})

最后面的onPlayerJoin是用于测试的代码))不要管


回复

上一页1 页 / 共 1下一页
yee089yee089

沙发()

点赞0


评论


yee089yee089

板凳(我再抢)

点赞0


评论


yee089yee089

地板(三连)

点赞0


评论


testifytestify

水了个鬼啊,这是干货啊!

点赞0


评论


Stephan轩Stephan轩

我没看懂你信吗()()

点赞0


评论


灵清帝王吃爆一切厕所灵清帝王吃爆一切厕所

你真的是建筑师吗。。。。。。。。。

点赞0


评论