//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("没有符合的坐标");
} }

javascript A*算法 寻路算法 获取最短路径算法的更多相关文章

  1. 【算法日记】Dijkstra最短路径算法

    上一篇再说广度优先搜索的适合提到了图. 狄克斯拉特算法是在图的基础上增加了 加权图的概念.就是节点和节点之间是有不同距离的 1.算法实例 用Dijkstra算法找出以A为起点的单源最短路径步骤如下 算 ...

  2. Dijkstra算法详细(单源最短路径算法)

    介绍 对于dijkstra算法,很多人可能感觉熟悉而又陌生,可能大部分人比较了解bfs和dfs,而对dijkstra和floyd算法可能知道大概是图论中的某个算法,但是可能不清楚其中的作用和原理,又或 ...

  3. Johnson 全源最短路径算法

    解决单源最短路径问题(Single Source Shortest Paths Problem)的算法包括: Dijkstra 单源最短路径算法:时间复杂度为 O(E + VlogV),要求权值非负: ...

  4. Floyd-Warshall 全源最短路径算法

    Floyd-Warshall 算法采用动态规划方案来解决在一个有向图 G = (V, E) 上每对顶点间的最短路径问题,即全源最短路径问题(All-Pairs Shortest Paths Probl ...

  5. 最短路径算法——Dijkstra,Bellman-Ford,Floyd-Warshall,Johnson

    根据DSqiu的blog整理出来 :http://dsqiu.iteye.com/blog/1689163 PS:模板是自己写的,如有错误欢迎指出~ 本文内容框架: §1 Dijkstra算法 §2 ...

  6. 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较(转)

    几大最短路径算法比较 几个最短路径算法的比较:Floyd        求多源.无负权边(此处错误?应该可以有负权边)的最短路.用矩阵记录图.时效性较差,时间复杂度O(V^3).       Floy ...

  7. Python小白的数学建模课-16.最短路径算法

    最短路径问题是图论研究中的经典算法问题,用于计算图中一个顶点到另一个顶点的最短路径. 在图论中,最短路径长度与最短路径距离却是不同的概念和问题,经常会被混淆. 求最短路径长度的常用算法是 Dijkst ...

  8. 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输

    Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...

  9. 带权图的最短路径算法(Dijkstra)实现

    一,介绍 本文实现带权图的最短路径算法.给定图中一个顶点,求解该顶点到图中所有其他顶点的最短路径 以及 最短路径的长度.在决定写这篇文章之前,在网上找了很多关于Dijkstra算法实现,但大部分是不带 ...

随机推荐

  1. Tasker如何使用Tasker插件以及Tasker第三方应用

    很多人不清楚Tasker插件和Tasker第三方应用之间的区别,以及与Tasker的关系有何不同,其实对于使用者而言并不需要理解他们之间的区别,因为这两者在使用上的区别逐渐模糊而变得没有区别,不过本人 ...

  2. No property andp found for type String! Traversed path: CmsPage.siteId.

    Respository没有找到该参数 1)由该参数,函数名写错,不符合JPA规范 2)实体类没有该参数

  3. 微信小程序--百度地图坐标转换成腾讯地图坐标

    最近开发小程序时出现一个问题,后台程序坐标采用的时百度地图的坐标,因为小程序地图时采用的腾讯地图的坐标系,两种坐标有一定的误差,导致位置信息显示不正确.现在需要一个可以转换两种坐标的方法,经过查询发现 ...

  4. dp - 递推

    C. Multiplicity time limit per test 3 seconds memory limit per test 256 megabytes input standard inp ...

  5. kmp-最小子串回文次数

    poj 2406 Given two strings a and b we define a*b to be their concatenation. For example, if a = &quo ...

  6. Qt Installer Framework翻译(2)

    开始 Qt IFW作为Qt项目的一部分进行开发.该框架自身也使用Qt.然而,它能被用于安装所有类型的应用程序,包括(但不限于)使用Qt编译的. 支持的平台 已在下列平台中进行了测试: > Mic ...

  7. CCPC-wannafly Camp Day2 讲课内容总结(杜瑜皓-数据结构)

    ·栈.单调栈 1.栈的特点与基本操作 2.单调栈 单调栈是一种特殊的栈,其栈内的元素都保持一个单调性(单调递增或者递减). ·单调递增栈,从栈底到栈顶依次递增(单调非递减栈:允许有相等) ·单调递减栈 ...

  8. Java语法进阶16-Lambda-Stream-Optional

    Lambda 大年初二,大门不出二门不迈.继续学习! 函数式接口 Lambda表达式其实就是实现SAM接口的语法糖,所谓SAM接口就是Single Abstract Method,即该接口中只有一个抽 ...

  9. java8新特性Lambda和Stream

    Java8出来已经4年,但还是有很多人用上了jdk8,但并没用到里面的新东西,那不就等于没用?jdk8有许多的新特性,详细可看下面脑图 我只讲两个最重要的特性Lambda和Stram,配合起来用可以极 ...

  10. 基于python的感知机

    一. 1.感知机可以描述为一个线性方程,用python的伪代码可表示为: sum(weight_i * x_i) + bias -> activation #activation表示激活函数,x ...