function beamPathfind(grid, start, end, allowDiag, hFn) {
const h = hFn || HEURISTICS.manhattan;
const beamWidth = 4;
let beam = [{
r: start[0],
c: start[1]
}];
const visited = new Set([key(start[0], start[1])]);
const prev = new Map();
while (beam.length) {
const candidates = [];
for (const node of beam) {
visitNode(node.r, node.c);
if (node.r === end[0] && node.c === end[1]) {
reconstructPath(prev, start, end);
return;
}
for (const [nr, nc] of getNeighbors(grid, node.r, node.c, allowDiag)) {
const neighborKey = key(nr, nc);
if (visited.has(neighborKey)) continue;
visited.add(neighborKey);
prev.set(neighborKey, [node.r, node.c]);
candidates.push({
r: nr,
c: nc,
f: h(nr, nc, end[0], end[1])
});
pushFrontier(nr, nc);
}
}
sortByField(candidates, "f");
beam = candidates.slice(0, beamWidth);
}
reportNoPath();
} def beamPathfind(grid, start, end, allowDiag, hFn):
h = (hFn or HEURISTICS.manhattan)
beamWidth = 4
beam = [{r: start[0], c: start[1]}]
visited = Set([key(start[0], start[1])])
prev = Map()
while beam.length:
candidates = []
for node in beam:
visitNode(node.r, node.c)
if ((node.r == end[0]) and (node.c == end[1])):
reconstructPath(prev, start, end)
return
for nr, nc in getNeighbors(grid, node.r, node.c, allowDiag):
neighborKey = key(nr, nc)
if visited.has(neighborKey):
continue
visited.add(neighborKey)
prev.set(neighborKey, [node.r, node.c])
candidates.push({r: nr, c: nc, f: h(nr, nc, end[0], end[1])})
pushFrontier(nr, nc)
sortByField(candidates, "f")
beam = candidates[0:beamWidth]
reportNoPath() #include <vector>
#include <algorithm>
void beamPathfind(int grid, int start, int end, int allowDiag, int hFn) {
auto h = (hFn || HEURISTICS.manhattan);
auto beamWidth = 4;
auto beam = [{r: start[0], c: start[1]}];
auto visited = Set([key(start[0], start[1])]);
auto prev = Map();
while(beam.length) {
auto candidates = [];
/* for-of not directly supported */
sortByField(candidates, "f");
beam = candidates[0:beamWidth];
}
reportNoPath();
} public void beamPathfind(int grid, int start, int end, int allowDiag, int hFn) {
var h = (hFn || HEURISTICS.manhattan);
var beamWidth = 4;
var beam = [{r: start[0], c: start[1]}];
var visited = Set([key(start[0], start[1])]);
var prev = Map();
while(beam.length) {
var candidates = [];
/* for-of not directly supported */
sortByField(candidates, "f");
beam = candidates[0:beamWidth];
}
reportNoPath();
} #include <stdio.h>
void beamPathfind(int grid, int start, int end, int allowDiag, int hFn) {
var h = (hFn || HEURISTICS.manhattan);
var beamWidth = 4;
var beam = [{r: start[0], c: start[1]}];
var visited = Set([key(start[0], start[1])]);
var prev = Map();
while(beam.length) {
var candidates = [];
/* for-of not directly supported in C */
sortByField(candidates, "f");
beam = candidates[0:beamWidth];
}
reportNoPath();
}