JS-A*算法

// 转载自
js实现A*寻路算法

大概步骤

  1. 先把地图用二维数组分割成方格,障碍物的值为 1 ,可移动方格为 0
  2. 创建开启列表和关闭列表
  3. 从起点开始寻找四周的方格,把他们放入开启列表,并设上一个点为父方格,如果出现值为 1 的为不可去方格,则不做处理
  4. 给每一个方格设一个 F , G , H
    H 代表不考虑障碍物的因素,选中方格到终点的直线距离
    G 代表移动到下一个所消耗的值,上下左右移动为10,斜线移动为14
    F = G + H

  5. 从开启列表中选择F值最低的 C 方格,将其放入关闭列表,并检测周围方格(不考虑关闭列表以及障碍物),将不在开启列表中的方格放入开启列表,并把 C 方格作为它们的父方格

  6. 如果某个相邻方格 D 已经在 “开启列表” 里了, 检查如果用新的路径 (就是经过 C 的路径) 到达它的话, G 值是否会更低一些, 如果新的 G 值更低, 那就把它的 “父方格” 改为目前选中的方格 C , 然后重新计算它的 F 值和 G 值 ( H 值不需要重新计算, 因为对于每个方块, H 值是不变的). 如果新的 G 值比较高,就说明经过 C 再到达 D 不是一个明智的选择,因为它需要更远的路,这时我们什么也不做.

伪代码

把起始格添加到 "开启列表" 
do 
{ 
    寻找开启列表中F值最低的格子, 我们称它为当前格. 
    把它切换到关闭列表. 
    对当前格相邻的 8 格中的每一个 
       if (它不可通过 || 已经在 "关闭列表" 中) 
       { 
         什么也不做. 
       } 
       if (它不在开启列表中) 
       { 
         把它添加进 "开启列表", 把当前格作为这一格的父节点, 计算这一格的 FGH 
       }
       if (它已经在开启列表中) 
       { 
         if (用G值为参考检查新的路径是否更好, 更低的G值意味着更好的路径) 
         { 
                  把这一格的父节点改成当前格, 并且重新计算这一格的 GF 值. 
         } 
       }
} while( 目标格已经在 "开启列表", 这时候路径被找到) 
如果开启列表已经空了, 说明路径不存在.

最后从目标格开始, 沿着每一格的父节点移动直到回到起始格, 这就是路径.

JS代码实现

function searchRoad(start_x,start_y,end_x,end_y){
            var openList=[],    //开启列表
                closeList=[],   //关闭列表
                result=[],      //结果数组
                result_index;   //结果数组在开启列表中的序号

            //把当前点加入到开启列表中,并且G是0   
            openList.push({x:start_x,y:start_y,G:0}); 

            do{
                var currentPoint = openList.pop();
                closeList.push(currentPoint);
                var surroundPoint=SurroundPoint(currentPoint);
                for(var i in surroundPoint) {
                    var item = surroundPoint[i];    //获得周围的八个点
                    if (
                        item.x>=0 &&                //判断是否在地图上
                        item.y>=0 &&
                        item.x<MAP.rows &&
                        item.y<MAP.cols &&
                        //判断是否是障碍物
                        MAP.arr[item.x][item.y] != 1 &&         
                        //判断是否在关闭列表中
                        !existList(item, closeList) &&          
                        //判断之间是否有障碍物,如果有障碍物是过不去的
                        MAP.arr[item.x][currentPoint.y]!=1 &&   
                        MAP.arr[currentPoint.x][item.y]!=1) {
                        //g 到父节点的位置
                        //如果是上下左右位置的则g等于10,斜对角的就是14
                        var g = currentPoint.G + ((currentPoint.x - item.x)* 
                        (currentPoint.y - item.y) == 0 ? 10 : 14);
                        if (!existList(item, openList)) {       //如果不在开启列表中
                            //计算H,通过水平和垂直距离进行确定
                            item['H'] = Math.abs(end_x - item.x) 
                            * 10 + Math.abs(end_y - item.y) * 10;
                            item['G'] = g;
                            item['F'] = item.H + item.G;
                            item['parent'] = currentPoint;
                            openList.push(item);
                        }
                        else {                 
                            //存在在开启列表中,比较目前的g值和之前的g的大小                 
                            var index = existList(item, openList);
                            //如果当前点的g更小
                            if (g < openList[index].G) {
                                openList[index].parent = currentPoint;
                                openList[index].G = g;
                                openList[index].F=g+openList[index].H;
                            }
                        }
                    }
                }
                //如果开启列表空了,没有通路,结果为空
                if(openList.length==0) {
                    break;
                }
                //这一步是为了循环回去的时候,找出 F 值最小的, 将它从 "开启列表" 中移掉
                openList.sort(sortF);
            }while(!(result_index=existList({x:end_x,y:end_y},openList)));

            //判断结果列表是否为空
            if(!result_index) {
                result=[];
            }else {
                var currentObj=openList[result_index];
                do{
                    //把路劲节点添加到result当中
                    result.unshift({
                        x:currentObj.x,
                        y:currentObj.y
                    });
                    currentObj=currentObj.parent;
                }while (currentObj.x!=start_x || currentObj.y!=start_y);
            }
            return result;
        }
        //用F值对数组排序
        function sortF(a,b){
            return b.F- a.F;
        }
        //获取周围八个点的值
        function SurroundPoint(curPoint){
            var x=curPoint.x,y=curPoint.y;
            return [
                {x:x-1,y:y-1},
                {x:x,y:y-1},
                {x:x+1,y:y-1},
                {x:x+1,y:y},
                {x:x+1,y:y+1},
                {x:x,y:y+1},
                {x:x-1,y:y+1},
                {x:x-1,y:y}
            ]
        }
        //判断点是否存在在列表中,是的话返回的是序列号
        function existList(point,list) {
            for(var i in list) {
                if(point.x==list[i].x && point.y==list[i].y) {
                    return i;
                }
            }
            return false;
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用JavaScript实现A*算法的代码: ```javascript // 定义一个节点类 class Node { constructor(x, y) { this.x = x; this.y = y; this.f = 0; this.g = 0; this.h = 0; this.parent = null; } } // 创建地图 let map = [ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0] ]; // 定义起点和终点 let start = new Node(0, 0); let end = new Node(9, 8); // 定义open列表和close列表 let openList = []; let closeList = []; // 判断节点是否在close列表中 function isInCloseList(x, y) { for (let i = 0; i < closeList.length; i++) { if (closeList[i].x == x && closeList[i].y == y) { return true; } } return false; } // 判断节点是否在open列表中 function isInOpenList(x, y) { for (let i = 0; i < openList.length; i++) { if (openList[i].x == x && openList[i].y == y) { return true; } } return false; } // 计算节点的f, g, h值 function countFGH(node, parent, end) { node.g = parent.g + 1; node.h = Math.abs(end.x - node.x) + Math.abs(end.y - node.y); node.f = node.g + node.h; node.parent = parent; } // 查找open列表中f值最小的节点 function findMinNode() { let minF = openList[0].f; let minIndex = 0; for (let i = 1; i < openList.length; i++) { if (openList[i].f < minF) { minF = openList[i].f; minIndex = i; } } return minIndex; } // 查找路径 function findPath() { openList.push(start); while (openList.length > 0) { let minIndex = findMinNode(); let currNode = openList[minIndex]; if (currNode.x == end.x && currNode.y == end.y) { let path = []; while (currNode) { path.push(currNode); currNode = currNode.parent; } return path.reverse(); } openList.splice(minIndex, 1); closeList.push(currNode); // 寻找当前节点的邻居 let neighbors = []; if (currNode.x > 0) { neighbors.push(new Node(currNode.x - 1, currNode.y)); } if (currNode.y > 0) { neighbors.push(new Node(currNode.x, currNode.y - 1)); } if (currNode.x < map.length - 1) { neighbors.push(new Node(currNode.x + 1, currNode.y)); } if (currNode.y < map[0].length - 1) { neighbors.push(new Node(currNode.x, currNode.y + 1)); } for (let i = 0; i < neighbors.length; i++) { let neighbor = neighbors[i]; if (!isInCloseList(neighbor.x, neighbor.y)) { if (isInOpenList(neighbor.x, neighbor.y)) { let index = openList.findIndex((item) => item.x == neighbor.x && item.y == neighbor.y); let node = openList[index]; if (node.g > currNode.g + 1) { countFGH(node, currNode, end); } } else { countFGH(neighbor, currNode, end); openList.push(neighbor); } } } } return null; } // 测试 let path = findPath(); console.log(path); ``` 上面的代码实现了一个简单的寻路算法,通过传入地图、起点和终点,返回一条从起点到终点的最短路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值