这篇文章主要介绍了基于javascript实现获取最短路径算法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码如下

//A算法 自动寻路 路径

class GetAutoPath{

constructor(id, map, sPos, ePos, mapArr){

//this.type = id.type;

this.id = id;

this.map = map;

this.sPos = sPos;

this.ePos = ePos;

this.mapArr = mapArr;

this.maxMach = 10000;

this.openArr = [];

this.closeArr = [];

this.minPath = [];

if(!this.isPath(this.sPos.x, this.sPos.y)){this.sPos = this.getNewDot(sPos, ePos);}

if(!this.isPath(this.ePos.x, this.ePos.y)){this.ePos = this.getNewDot(ePos, sPos);}

//console.log(this.mapArr);

return this.run();

}

posts(txt, arr){//post消息

//let id = this.id, sPos = this.sPos, ePos = this.ePos, arrs = arr || [];

return {id:this.id, map:this.map, arr:arr || [], sPos:this.sPos, ePos:this.ePos, txt:txt}

}

isPath(x, y){//isPath = true 合法路径 = isBanPath === undefined

let isPath = false, ym = this.mapArr.get(y), xm; //console.log(ym); debugger;

if(ym !== undefined){

xm = ym.get(x);

if(xm !== undefined){

if(xm.isBanPath === undefined){isPath = true;}

}

}

//if(this.mapArr[y] !== undefined && this.mapArr[y][x] !== undefined && this.mapArr[y][x].isPath === 1){isPath = true;}

return isPath;

}

getEqual(arr, x, y){//获取目标数组相同的坐标

let isPos = false;

if(arr.length === 0){

isPos = false;

}else{

isPos = arr.some(function (o){return o.x === x && o.y === y;});

}

return isPos;

}

getDot(x, y){//获取周围8个方向坐标

return [{x:x-1,y:y},{x:x+1,y:y},{x:x,y:y-1},{x:x,y:y+1},{x:x-1,y:y-1},{x:x+1,y:y+1},{x:x+1,y:y-1},{x:x-1,y:y+1}]

}

getNewDot(setPos, pos){//重定义起点或终点

let dot = setPos, pointDot, k, arr = [], arrs = [], g, end, maxMachT = 0;

while(!end && maxMachT < this.maxMach){

maxMachT++;

pointDot = this.getDot(dot.x, dot.y);

for(k in pointDot){

g = Math.round(Math.sqrt(Math.abs(pointDot[k].x - pos.x) + Math.abs(pointDot[k].y - pos.y)) * 100) / 100;

if(!this.isPath(pointDot[k].x, pointDot[k].y)){//不合法

arr.push({x:pointDot[k].x, y:pointDot[k].y, g:g});

arr.sort(function(a, b){return a.g - b.g;});

}else{//合法

arrs.push({x:pointDot[k].x, y:pointDot[k].y, g:g});

arrs.sort(function(a, b){return a.g - b.g;});

}

if(arrs.length > 0){end = true;}

}

dot = {x:arr[0].x, y:arr[0].y, g:arr[0].g}; arr = [];

}

if(!arrs[0].x || !arrs[0].y){return this.posts("没有符合的坐标");}

return {x:arrs[0].x, y:arrs[0].y};

}

run(){

if(this.sPos.x === undefined || this.ePos.x === undefined){return this.posts("没有符合的坐标");}

let sPos = this.sPos, ePos = this.ePos, point, key, i, newPoint, ger, gers, g, h, f, maxMachT = 0;

this.openArr[0] = {x : sPos.x, y : sPos.y, f : 0, p : 0, ger : 0}

while(this.openArr.length > 0){

maxMachT++;

point = this.openArr[0]; this.closeArr.push(point); this.openArr.splice(0,1);

key = this.closeArr.length - 1;//设置当前节点

newPoint = this.getDot(point.x, point.y);//获取周围点

for(i in newPoint){//设置周围点

ger = Math.round(Math.sqrt(Math.abs(newPoint[i].x - point.x) + Math.abs(newPoint[i].y - point.y)) * 100) / 100;//到当前节点的曼哈顿距离,保留两位小数点

gers = ger + point.ger;

g = Math.round(gers * 100) / 100;

h = Math.abs(newPoint[i].x - ePos.x) + Math.abs(newPoint[i].y - ePos.y);

f = g + h;

if(this.isPath(newPoint[i].x, newPoint[i].y) && !this.getEqual(this.openArr, newPoint[i].x, newPoint[i].y) && !this.getEqual(this.closeArr, newPoint[i].x, newPoint[i].y)){this.openArr.push({x:newPoint[i].x, y:newPoint[i].y, f:f, p:key, ger:ger});}

}

this.openArr.sort(function(a, b){return a.f - b.f;});//排序

if(this.getEqual(this.closeArr, ePos.x, ePos.y) || this.getEqual(this.openArr, ePos.x, ePos.y)){//end

this.minPath.unshift(this.closeArr[key]);

while(this.minPath.length > 0){

if(this.minPath[0].p == 0){return this.posts('success', this.minPath);}else{this.minPath.unshift(this.closeArr[this.minPath[0].p]);}

}

}else if(maxMachT === this.maxMach){

return this.posts("没有符合的坐标");

}

}

return this.posts("没有符合的坐标");

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

曼哈顿距离java实现_基于javascript实现获取最短路径算法代码实例相关推荐

  1. 基于python的音频播放器_基于python实现音乐播放器代码实例

    基于python实现音乐播放器代码实例,一首,函数,按钮,布局,音乐 基于python实现音乐播放器代码实例 易采站长站,站长之家为您整理了基于python实现音乐播放器代码实例的相关内容. 核心播放 ...

  2. python开发语音录入_基于python实现语音录入识别代码实例

    这篇文章主要介绍了如何通过python实现语音录入识别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一.介绍 1.第一步录音存入本地 2.调用百度 ...

  3. html js制作高级拼图,基于JavaScript实现十五拼图代码实例

    顾名思义,十五拼图就是将游戏画面中的数字从上到下,从左到右按顺序从1到15排列下来,看起来很简单,但是玩起来不容易. css代码 body { font-family: cursive; font-s ...

  4. PHP代码猜数字游戏,基于JavaScript实现猜数字游戏代码实例

    环境 vscode 1.46 Microsoft Edge 83 示例 以下内容为了解和熟悉 JavaScript,不需要理解,能够复制粘贴并正确运行即可. 新建网页模板 在 vscode 中新建一个 ...

  5. python音乐下载器交互界面_基于Python实现下载网易音乐代码实例

    代码如下 # 爬取网易音乐 import requests from bs4 import BeautifulSoup import urllib.request headers = {"o ...

  6. python下载音乐代码_基于Python实现下载网易音乐代码实例

    代码如下 # 爬取网易音乐 import requests from bs4 import BeautifulSoup import urllib.request headers = {"o ...

  7. python音乐编程_基于python实现音乐播放器代码实例

    核心播放模块(pygame内核) import time import pygame import easygui as gui file = r'D:\CloudMusic\G.E.M.邓紫棋,艾热 ...

  8. html年会抽奖代码实例,基于JavaScript实现简单抽奖功能代码实例

    为什么会做这个东西呢,纯属好玩,闲的 其实是在上次班会的时候想到的,班会的时候叫人回答问题,没人回答 当时就想,我如果抽签抽到你了,你还是不回答吗??好吧,一切都是扯淡 先来看看页面效果吧: 点击抽取 ...

  9. python 循环播放音乐_基于python实现音乐播放器代码实例

    核心播放模块(pygame内核) import time import pygame import easygui as gui file = r'D:\CloudMusic\G.E.M.邓紫棋,艾热 ...

最新文章

  1. opengl android 线宽
  2. 8g ubuntu 树莓派4b_树莓派4B(4G版本)安装Ubuntu 19.10
  3. remoting和webservice的区别
  4. 元气森林、完美日记、泡泡玛特们的2021,竞争壁垒低,前行道路难
  5. P3978 [TJOI2015]概率论
  6. c51汇编语言如何定义全局变量_汇编语言期末复习笔记(七)
  7. 设计模式之单件模式(Singleton Pattern)
  8. 工作235:splice
  9. 【PAT - 甲级1155】Heap Paths (30分)(栈,dfs,二叉树)
  10. 做了中台就不会死吗?每年至少40%开发资源是被浪费的!
  11. linux rt 应用程序,RTlinux编程
  12. javascript基础系列:字符串的常用方法
  13. java数组大小界限,Java数组索引超出界限
  14. win10专业版激活(cmd方式)
  15. Windows如何进入安全模式
  16. 计算机图形学-五角星的画法(转)
  17. java写足球游戏_月光软件站 - 编程文档 - Java - 足球战术之flyweight篇
  18. 基于python的-正则中的函数
  19. Tensorflow2.0之用遗传算法优化卷积神经网络结构 Version2
  20. 美团数据治理一体化实践

热门文章

  1. 还有多少时间可以用来读书
  2. 要速度更要方便!20款实用Chrome插件推荐
  3. Visual Studio 2008 每日提示(三十二)
  4. 我提出辞职,老板竟然让我做选择题(转)
  5. 2020年行政区划代码_2020年梧州市行政区划,了解梧州市有几个区,详细数据
  6. 【转载】SAP内部订单概念信息
  7. 企业是否真的需要BI?
  8. abap 单元格级别的操作ALV.
  9. ABAP常用字符串操作收集整理
  10. 围猎“下沉市场”,争抢「小镇青年」,尚美如何突围?