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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值