A*算法JS实现

本文介绍A*算法的应用,它是一种启发式寻路算法,通过为每个节点分配估计值来优化搜索路径。在JavaScript中实现A*算法时,估计函数的设计对算法的效率至关重要。
摘要由CSDN通过智能技术生成

A*寻路算法就是启发式探索的一个典型实践,在寻路的过程中,给每个节点绑定了一个估计值(即启发式),在对节点的遍历过程中是采取估计值优先原则,估计值更优的节点会被优先遍历。所以估计函数的定义十分重要,显著影响算法效率。

<html>

<head>
</head>

<body>
    <canvas id="canvas" width="800" height="800"></canvas>
    <script>
        const drawMap = () => {
     
            for (let i = 0; i < bigWidth / unitWidth; i++) {
     
                for (let j = 0; j < bigHeight / unitHeight; j++) {
     
                    let value = locationMap.get(`${
       i},${
       j}`);
                    switch (value) {
     
                        case CANTPASS:
                            ctx.fillStyle = "rgb(0,0,0)"
                            ctx.fillRect(i * unitWidth, j * unitHeight, unitWidth, unitHeight)
                            break;
                        case START:
                            ctx.fillStyle = "rgb(0,255,0)"
                            ctx.fillRect(i * unitWidth, j * unitHeight, unitWidth, unitHeight)
                            break;
                        case END:
                            ctx.fillStyle = "rgb(0,0,255)"
                            ctx.fillRect(i * unitWidth, j * unitHeight, unitWidth, unitHeight)
                            break;
                        case PATH:
                            ctx.fillStyle = "rgb(255,0,0)"
                            ctx.fillRect(i * unitWidth, j * unitHeight, unitWidth, unitHeight)
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        function nearLocation(x, y, mX, mY) {
     
            // 上下左右
            let locs = 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值