javascript A*算法 寻路算法 获取最短路径算法
//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*算法 寻路算法 获取最短路径算法的更多相关文章
- 【算法日记】Dijkstra最短路径算法
		
上一篇再说广度优先搜索的适合提到了图. 狄克斯拉特算法是在图的基础上增加了 加权图的概念.就是节点和节点之间是有不同距离的 1.算法实例 用Dijkstra算法找出以A为起点的单源最短路径步骤如下 算 ...
 - Dijkstra算法详细(单源最短路径算法)
		
介绍 对于dijkstra算法,很多人可能感觉熟悉而又陌生,可能大部分人比较了解bfs和dfs,而对dijkstra和floyd算法可能知道大概是图论中的某个算法,但是可能不清楚其中的作用和原理,又或 ...
 - Johnson 全源最短路径算法
		
解决单源最短路径问题(Single Source Shortest Paths Problem)的算法包括: Dijkstra 单源最短路径算法:时间复杂度为 O(E + VlogV),要求权值非负: ...
 - Floyd-Warshall 全源最短路径算法
		
Floyd-Warshall 算法采用动态规划方案来解决在一个有向图 G = (V, E) 上每对顶点间的最短路径问题,即全源最短路径问题(All-Pairs Shortest Paths Probl ...
 - 最短路径算法——Dijkstra,Bellman-Ford,Floyd-Warshall,Johnson
		
根据DSqiu的blog整理出来 :http://dsqiu.iteye.com/blog/1689163 PS:模板是自己写的,如有错误欢迎指出~ 本文内容框架: §1 Dijkstra算法 §2 ...
 - 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较(转)
		
几大最短路径算法比较 几个最短路径算法的比较:Floyd 求多源.无负权边(此处错误?应该可以有负权边)的最短路.用矩阵记录图.时效性较差,时间复杂度O(V^3). Floy ...
 - Python小白的数学建模课-16.最短路径算法
		
最短路径问题是图论研究中的经典算法问题,用于计算图中一个顶点到另一个顶点的最短路径. 在图论中,最短路径长度与最短路径距离却是不同的概念和问题,经常会被混淆. 求最短路径长度的常用算法是 Dijkst ...
 - 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输
		
Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...
 - 带权图的最短路径算法(Dijkstra)实现
		
一,介绍 本文实现带权图的最短路径算法.给定图中一个顶点,求解该顶点到图中所有其他顶点的最短路径 以及 最短路径的长度.在决定写这篇文章之前,在网上找了很多关于Dijkstra算法实现,但大部分是不带 ...
 
随机推荐
- CTPN-自然文本场景检测代码阅读笔记
			
TensorFlow代码 https://github.com/eragonruan/text-detection-ctpn 训练 main/train.py 1. utils/prepare/spl ...
 - Java Collection集合概述及其常用方法
			
Collection集合概述 Java数组的长度是固定的,为了使程序能够方便地存储和操作数目不固定的一组数据,JDK类库提供了Java集合 与数组不同的是,集合中不能存放基本类型数据,而只能存放对象的 ...
 - mysql 行增删改查
			
一.增 ); ),(); insert into student(name, age) select name, age from info; 二.删 delete from db1; delete ...
 - path_info和get_full_path()的区别
			
1.get_full_path() 获取的url路径包含参数 2.path_info 获取的路径不包含参数 注意:获取的路径都不包含协议 IP 和端口 3.补充 sesssion http://127 ...
 - 高通量计算框架HTCondor(三)——使用命令
			
目录 1. 目录 2. 进程 3. 命令 3.1. condor_q 3.2. condor_status 3.3. conodr_submit 3.4. conodr_rm 4. 相关 1. 目录 ...
 - selenium,CSS定位法应用
			
如图,下载按钮 查看其元素,是无法直接定位的,通过xpath也无法定位,转为firepath获取的CSS也是无效的#downloadItems>a 但是为经过firepath,直接在html下复 ...
 - ios启动流程
			
1.创建UIApplication (1.打开网页,发短信,打电话 . 2.设置应用程序提醒数字 . 3.设置联网状态 . 4.设置状态栏) 2.创建AppDelegate代理对象,并且成为UIApp ...
 - composer intall 报错
			
报错 [Composer\Exception\NoSslException] The openssl extension is required for SSL/TLS protection but ...
 - POI导出excel的三种方式
			
原文链接:https://www.cnblogs.com/zhaoblog/p/7661245.html poi导出excel最常用的是第一种方式HSSFWorkbook,不过这种方式数据量大的话会产 ...
 - requestAnimationFrame 与  seeTimeout 的区别
			
requestAnimationFrame 随着浏览器的刷新而执行. let a = () =>{ doSomething() ; window.reques ...