基于javascript实现获取最短路径算法代码实例

 更新时间:2020年02月20日 14:13:46   作者:weihexin  
这篇文章主要介绍了基于javascript实现获取最短路径算法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了基于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("没有符合的坐标");
 }
 
}

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

相关文章

  • 阿里巴巴 Sentinel + InfluxDB + Chronograf 实现监控大屏

    阿里巴巴 Sentinel + InfluxDB + Chronograf 实现监控大屏

    这篇文章主要介绍了阿里巴巴 Sentinel + InfluxDB + Chronograf 实现监控大屏,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • Java设置PDF有序和无序列表的知识点总结

    Java设置PDF有序和无序列表的知识点总结

    在本篇文章中小编给大家整理了关于Java设置PDF有序和无序列表的知识点,需要的朋友们参考下。
    2019-03-03
  • Java Runnable和Thread实现多线程哪个更好你知道吗

    Java Runnable和Thread实现多线程哪个更好你知道吗

    这篇文章主要为大家详细介绍了Java Runnable和Thread实现多线程哪个更好,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助<BR>
    2022-03-03
  • Java Integer.ValueOf()的一些了解

    Java Integer.ValueOf()的一些了解

    这篇文章主要介绍了Java Integer.ValueOf()的一些了解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Java项目--家庭收支记录程序

    Java项目--家庭收支记录程序

    本文主要介绍Java基础阶段的一个小项目——家庭收支记录程序(附完整源代码),本项目所用到的主要知识点:基本语法、数组和方法。本项目并不难,主要是对Java初学者的基础综合运用的训练及检验
    2021-07-07
  • 浅谈Spring Boot 异常处理篇

    浅谈Spring Boot 异常处理篇

    本篇文章主要介绍了浅谈Spring Boot 异常处理篇,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • java并发编程中的SynchronousQueue实现原理解析

    java并发编程中的SynchronousQueue实现原理解析

    这篇文章主要介绍了java并发编程中的SynchronousQueue实现原理解析,SynchronousQueue是一个比较特别的队列,此队列源码中充斥着大量的CAS语句,理解起来是有些难度的,为了方便日后回顾,本篇文章会以简洁的图形化方式展示该队列底层的实现原理,需要的朋友可以参考下
    2023-12-12
  • 简要分析Java的Hibernate框架中的自定义类型

    简要分析Java的Hibernate框架中的自定义类型

    这篇文章主要介绍了Java的Hibernate框架中的自定义类型,Hibernate是Java的SSH三大web开发框架之一,需要的朋友可以参考下
    2016-01-01
  • Druid(新版starter)在SpringBoot下的使用教程

    Druid(新版starter)在SpringBoot下的使用教程

    Druid是Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能,DruidDataSource支持的数据库,这篇文章主要介绍了Druid(新版starter)在SpringBoot下的使用,需要的朋友可以参考下
    2023-05-05
  • PowerJob的GridFsManager工作流程源码解读

    PowerJob的GridFsManager工作流程源码解读

    这篇文章主要为大家介绍了PowerJob的GridFsManager工作流程源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01

最新评论