【发布时间】:2021-03-23 03:52:27
【问题描述】:
我正在尝试开发 this answer 的实现,以支持可能大于单个单元格的单元的 A-star 路径查找。
我遇到的问题是目前我的输出中有一个小错误,我不确定我是否执行不当,或者算法是否没有正确处理这种边缘情况。
这里是一个完整的重现问题:
function createThickGrid(inputGrid, width, height) {
let outputGrid = [];
let largeInteger = 100000000;
for (let i = 0; i < width * height; i++) {
if (inputGrid[i] === 0) { // wall
outputGrid[i] = -1;
} else {
outputGrid[i] = largeInteger;
}
}
for (let i = 0; i < width * height; i++) {
if (outputGrid[i] > 0) {
outputGrid[i] = getSmallestNeighbor(outputGrid, i, width, height) + 2;
}
}
return outputGrid;
}
function getSmallestNeighbor(grid, idx, width, height) {
let col = idx % width;
let row = Math.floor(idx / width);
let smallest = 99999999999999;
if (row <= 0 || row >= height - 1 || col <= 0 || col >= width - 1) {
return -1;
}
let northValue = grid[idx - width];
if (northValue < smallest) {
smallest = northValue;
}
let northWestValue = grid[idx - width - 1];
if (northWestValue < smallest) {
smallest = northWestValue;
}
let northEastValue = grid[idx - width + 1];
if (northEastValue < smallest) {
smallest = northEastValue;
}
let southValue = grid[idx + width];
if (southValue < smallest) {
smallest = southValue;
}
let southWestValue = grid[idx + width - 1];
if (southWestValue < smallest) {
smallest = southWestValue;
}
let southEastValue = grid[idx + width + 1];
if (southEastValue < smallest) {
smallest = southEastValue;
}
let westValue = grid[idx - 1];
if (westValue < smallest) {
smallest = westValue;
}
let eastValue = grid[idx + 1];
if (eastValue < smallest) {
smallest = eastValue;
}
return smallest;
}
// You can ignore this, it's just for pretty printing
function convert1DTo2D(grid, width, height) {
let arr = [];
for (let row = 0; row < height; row++) {
let newRow = [];
for (let col = 0; col < width; col++) {
newRow.push(grid[row * width + col]);
}
arr.push(newRow);
}
return arr;
}
let width = 5;
let height = 5;
// 0 == wall and 1 == free space
grid = [1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1];
let thickGrid = createThickGrid(grid, width, height);
// console table works better but I don't know if Stackoverflow supports it
// console.table(convert1DTo2D(thickGrid, width, height));
console.log(convert1DTo2D(thickGrid, width, height));
如您所见,以下输出是这样的:
[1, 1, 1, 1, 1]
[1, 3, 3, 3, 1]
[1, 3, 5, 3, 1]
[1, 3, 5, 3, 1]
[1, 1, 1, 1, 1]
这似乎几乎完全正确,除了倒数第二行的中间值。我认为应该是3 而不是5。
这些数字是指它们与最近的墙之间的半瓦距离(在这种情况下,最近的墙是阵列的边缘)。我认为应该是以下内容:
[1, 1, 1, 1, 1]
[1, 3, 3, 3, 1]
[1, 3, 5, 3, 1]
[1, 3, 3, 3, 1]
[1, 1, 1, 1, 1]
但我不确定为什么现在还不是这样。是算法错误还是我的实现有缺陷?
【问题讨论】:
标签: javascript arrays algorithm path-finding a-star