//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. 最短路SPFA模板

    // // dijkstra妯℃澘.cpp // algorithm // // Created by david.xu on 2018/8/6. // Copyright 漏 2018骞?david ...

  2. .net core2.2上传文件总结

    总结一下.net core的上传文件操作,这里主要分上传到本地的也就是MVC的,另一种是上传到WebAPi的. 一.Web端 1.新建一个.net core mvc项目 2.这里的版本是.net co ...

  3. UAF——use after free

    本文系pwn2web原创,转载请说明出处 UAF 漏洞,英文原名use after free,该漏洞简洁的可以概括为 分配一块内存 free该内存但不回收,构成悬垂指针 再次构造分配同样大小的内存,按 ...

  4. [bzoj4569] [loj#2014] [Scoi2016] 萌萌哒

    Description 一个长度为 \(n\) 的大数,用 \(S1S2S3...Sn\) 表示,其中 \(Si\) 表示数的第 \(i\) 位, \(S1\) 是数的最高位,告诉你一些限制条件,每个 ...

  5. Treap基本用法总结

    Treap=Tree+Heap  起名的人非常有才 Treap是啥? 一棵二叉搜索树可能退化成链,那样各种操作的效率都比较低 于是可爱的Treap在每个节点原先值v的基础上加了一个随机数rnd,树的形 ...

  6. VSCODE更改文件时,提示EACCES permission denied的解决办法(mac电脑系统)

    permission denied:权限问题 具体解决办法: 1.在项目文件夹右键-显示简介-点击右下角解锁 2.权限全部设置为读与写 3.最关键一步:点击"应用到包含的项目",这 ...

  7. java.sql.Date与java.sql.Date区别

    public static void main(String[] args) { java.sql.Date createTime = new java.sql.Date(System.current ...

  8. selenium,CSS定位法应用

    如图,下载按钮 查看其元素,是无法直接定位的,通过xpath也无法定位,转为firepath获取的CSS也是无效的#downloadItems>a 但是为经过firepath,直接在html下复 ...

  9. php--->cookie和session

    cookie和session cookie和session理解 HTTP协议本身是无状态的,这与HTTP协议本来的目的是相符的,客户端只需要简单的向服务器请求下载某些文件,无论是客户端还是服务器都没有 ...

  10. php--->注册模式

    注册模式 什么是注册树模式? 注册树模式当然也叫注册模式,注册器模式.注册树模式通过将对象实例注册到一棵全局的对象树上,需要的时候从对象树上采摘的模式设计方法. 优点:单例模式解决的是如何在整个项目中 ...