用户:
qndm查看:1 回复:4 评论:1 创建时间:2024-08-19T21:10:30
一个基于BFS算法的自动寻路 测试视频:www.bilibili.com/video/BV1xwpme4Epu 代码链接:qndm.github.io/box3-docs/learn/arithmetic/BFS 在旧岛中,之前加入以下代码: constVector3=Box3Vector3; 在新岛中,之前加入以下代码: constVector3=GameVector3; 建议运行前清空控制台 主体代码: constSIDES=[ newVector3(-1,0,0), newVector3(1,0,0), newVector3(0,0,-1), newVector3(0,0,1), newVector3(-1,-1,0), newVector3(1,-1,0), newVector3(0,-1,-1), newVector3(0,-1,1), newVector3(-1,1,0), newVector3(1,1,0), newVector3(0,1,-1), newVector3(0,1,1), ]; constentityPositionFix=newVector3(0.5,0.5,0.5);//修复实喵置 conststartPosition=newVector3(128,9,128); constendPosition=newVector3(243,19,11); conste=world.createEntity({ mesh:'mesh/test.vb',//假设文件里有一个叫test.vb的实体 position:startPosition.add(entityPositionFix), meshScale:newVector3(0.0625,0.0625,0.0625), collides:false, fixed:true, gravity:false }); functionBFS(s,e){ /**@type{number[][][]}*/ letvis=[], /**@type{Vector3[]}*/ que=[],found=false,cnt=1; //手动修正Box3的bug //测试地图为256地图不同地图需要手动修改数值 voxels.shape.set(255,63,255); console.log('地图尺寸',voxels.shape.x,voxels.shape.y,voxels.shape.z); for(leti=0;i<voxels.shape.x;i++){ vis.push([]); for(letj=0;j<voxels.shape.y;j++) vis[vis.length-1].push(newArray(voxels.shape.z)); } que.push(s.clone()); vis[s.x][s.y][s.z]=0; while(que.length>0&&!found){ letu=que[0]; que.splice(0,1); for(constdofSIDES){ letv=u.add(d); if(v.x<0||v.x>=voxels.shape.x||v.y<0||v.y>=voxels.shape.y||v.z<0||v.z>=voxels.shape.z) continue; if(vis[v.x][v.y][v.z]!==undefined) continue; if(voxels.getVoxel(v.x,v.y,v.z)!==0||voxels.getVoxel(v.x,v.y-1,v.z)===0)//禁止穿墙和空中寻路 continue; vis[v.x][v.y][v.z]=vis[u.x][u.y][u.z]+1; que.push(v); if(v.equals(e)){ found=true; console.log('找到终点',v.x,v.y,v.z,vis[v.x][v.y][v.z]); break; } cnt++; } } console.log('搜索完成,共搜索',cnt,'个位置'); returnvis; } functionfindPath(map,s,e){ if(map[endPosition.x][endPosition.y][endPosition.z]===undefined) throw"无效数据"; letresult=[],now=newVector3(Math.round(e.x),Math.round(e.y),Math.round(e.z)); result.push(now.clone()); do{ letv; for(constdofSIDES){ v=now.add(d); if(v.x<0||v.x>=map.length||v.y<0||v.y>=map[now.x].length||v.z<0||v.z>=map[now.x][now.y].length) continue; if(map[now.x][now.y][now.z]-1===map[v.x][v.y][v.z]){ result.push(v); now.copy(v); break; } } if(v===undefined) throw"找不到路径"; }while(!result[result.length-1].equals(s)); returnresult.reverse(); } 测试代码: world.onPlayerJoin(async({entity})=>{ entity.player.spectator=true; entity.player.invisible=true; entity.player.cameraEntity=e; console.log('开始搜索'); letstartTime=Date.now() constMAP=BFS(startPosition,endPosition); console.log('寻路完成长度',MAP[endPosition.x][endPosition.y][endPosition.z],'用时',Date.now()-startTime,'ms'); console.log('开始寻路'); constPATH=findPath(MAP,startPosition,endPosition); console.log('路径长度',PATH.length); for(letpofPATH){ awaitsleep(100);//entity.player.nextPress(); e.position.copy(p.add(entityPositionFix)); console.log(p.toString()); } console.log('完成'); });
qndm直接看这两个链接 测试视频:www.bilibili.com/video/BV1xwpme4Epu 代码链接:qndm.github.io/box3-docs/learn/arithmetic/BFS
点赞0
评论
#include<bits/stdc++.h>
usng namespace std;
struct node{
int x,y,s;
};
const int d[2][8]={{0,0,1,-1,1,1,-1,-1},
{1,-1,0,0,1,-1,1,-1}};
int n,a[1010][1010];
bool f[1010][1010];
queue<node> q[1000010];
int bfs(){
f[1][1]=1;
q.push({1,1,0});
while(q.size()){
node t=q.front();
q.pop();
if(t.x==n&&t.y==n) return t.s;
for(int i=0; i<8; i++){
int dx=t.x+d[0][i],dy=t.y+d[1][i];
if(dx<1||dx>n||dy<1||dy>n||a[dx][dy]==0||f[dx][dy])
continue;
f[dx][dy]=1;
q.push({dx,dy,t.s+1});
}
}
return -1;
}
int main(){
scanf("%d",&n);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
scanf("%d",&a[i][j]);
printf("%d\n",bfs());
return 0;
}点赞0
评论