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算法实现,但大部分是不带 ...
随机推荐
- document.visibilityState 监听浏览器
document.hidden:表示页面是否隐藏的布尔值.页面隐藏包括 页面在后台标签页中 或者 浏览器最小化 (注意,页面被其他软件遮盖并不算隐藏,比如打开的 sublime 遮住了浏览器). do ...
- Java入门 - 导读
原文地址:http://www.work100.net/training/java 更多教程:光束云 - 免费课程 Java入门 Java 是由 Sun Microsystems 公司于1995年5月 ...
- 批量解决win10图标上有两个蓝色箭头的方法
双击“此电脑”,点击“C盘”,可以看到一个”用户“文件夹,双击”用户“, 选择现在正在使用的用户名,双击用户名,找到该文件夹下的”桌面“或”Desktop“点击“属性”, 在“常规”选项卡中的属于项中 ...
- AutoIT测试实例
AutoIT是一款非常强大的免费功能自动化测试工具,使用它可以轻松实现web和winform的自动化测试.其脚本语言AU3语法类似于VB语言和vbs脚本语言,对于经常使用QTP的童鞋来说,非常容易上 ...
- python+win32--com实现excel自动化
import win32com APP_TYPE = 'Excel.Application' xlBlack,xlRed,xlGray,xlBlue = 1,3,15,41 xlBreakFull ...
- HCNA 2017年01月26日
[Huawei]ping 127.0.0.1 PING 127.0.0.1: 56 data bytes, press CTRL_C to break Reply from 127.0.0.1: by ...
- GP工作室——系统设计
团队作业第二次--系统设计 问题 答案 这个作业属于哪个课程 软件工程 这个作业要求在哪里 作业要求 团队名称 GP工作室 这个作业的目标 对项目软件进行更为详细的系统性设计 按照本游戏的设计要求,我 ...
- c# 一维数组和二维数组的几种定义方式<转>
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- DFS(深度优先搜索遍历有向图)-03-有向图-太平洋大西洋水流问题
给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度.“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界. 规定水流只能按照上.下.左.右四个方向流动,且只能从高 ...
- Nginx-负载均衡实现解读
负载均衡在服务端开发中算是一个比较重要的特性.因为Nginx除了作为常规的Web服务器外,还会被大规模的用于反向代理前端,因为Nginx的异步框架可以处理很大的并发请求,把这些并发请求hold住之后就 ...