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 =