function iddfsPathfind(grid, start, end, allowDiag) {
const maxDepth = grid.length * grid[0].length;
for (let limit = 0; limit <= maxDepth; limit++) {
const pathVisited = new Set();
const prev = new Map();
const found = dls(start[0], start[1], 0, limit, pathVisited, prev);
if (found) return;
}
reportNoPath();
function dls(r, c, depth, limit, pathVisited, prev) {
const cellKey = key(r, c);
if (pathVisited.has(cellKey) || grid[r][c] === WALL) return false;
pathVisited.add(cellKey);
visitNode(r, c);
if (r === end[0] && c === end[1]) {
reconstructPath(prev, start, end);
return true;
}
if (depth === limit) {
pathVisited.delete(cellKey);
return false;
}
for (const [nr, nc] of getNeighbors(grid, r, c, allowDiag)) {
const neighborKey = key(nr, nc);
if (pathVisited.has(neighborKey)) continue;
prev.set(neighborKey, [r, c]);
const found = dls(nr, nc, depth + 1, limit, pathVisited, prev);
if (found) return true;
prev.delete(neighborKey);
}
pathVisited.delete(cellKey);
return false;
}
} def iddfsPathfind(grid, start, end, allowDiag):
maxDepth = (grid.length * grid[0].length)
for limit in range((maxDepth + 1)):
pathVisited = Set()
prev = Map()
found = dls(start[0], start[1], 0, limit, pathVisited, prev)
if found:
return
reportNoPath()
def dls(r, c, depth, limit, pathVisited, prev):
cellKey = key(r, c)
if (pathVisited.has(cellKey) or (grid[r][c] == WALL)):
return False
pathVisited.add(cellKey)
visitNode(r, c)
if ((r == end[0]) and (c == end[1])):
reconstructPath(prev, start, end)
return True
if (depth == limit):
pathVisited.delete(cellKey)
return False
for nr, nc in getNeighbors(grid, r, c, allowDiag):
neighborKey = key(nr, nc)
if pathVisited.has(neighborKey):
continue
prev.set(neighborKey, [r, c])
found = dls(nr, nc, (depth + 1), limit, pathVisited, prev)
if found:
return True
prev.delete(neighborKey)
pathVisited.delete(cellKey)
return False #include <vector>
#include <algorithm>
void iddfsPathfind(int grid, int start, int end, int allowDiag) {
auto maxDepth = (grid.length * grid[0].length);
for(int limit=0; limit<(maxDepth + 1); limit++) {
auto pathVisited = Set();
auto prev = Map();
auto found = dls(start[0], start[1], 0, limit, pathVisited, prev);
if(found) {
return;
}
}
reportNoPath();
int dls(int r, int c, int depth, int limit, int pathVisited, int prev) {
auto cellKey = key(r, c);
if((pathVisited.has(cellKey) || (grid[r][c] == WALL))) {
return false;
}
pathVisited.add(cellKey);
visitNode(r, c);
if(((r == end[0]) && (c == end[1]))) {
reconstructPath(prev, start, end);
return true;
}
if((depth == limit)) {
pathVisited.delete(cellKey);
return false;
}
for(auto& [nr, nc] : getNeighbors(grid, r, c, allowDiag)) {
auto neighborKey = key(nr, nc);
if(pathVisited.has(neighborKey)) {
continue;
}
prev.set(neighborKey, [r, c]);
auto found = dls(nr, nc, (depth + 1), limit, pathVisited, prev);
if(found) {
return true;
}
prev.delete(neighborKey);
}
pathVisited.delete(cellKey);
return false;
}
} public void iddfsPathfind(int grid, int start, int end, int allowDiag) {
var maxDepth = (grid.length * grid[0].length);
for(int limit=0; limit<(maxDepth + 1); limit++) {
var pathVisited = Set();
var prev = Map();
var found = dls(start[0], start[1], 0, limit, pathVisited, prev);
if(found) {
return;
}
}
reportNoPath();
int dls(int r, int c, int depth, int limit, int pathVisited, int prev) {
var cellKey = key(r, c);
if((pathVisited.has(cellKey) || (grid[r][c] == WALL))) {
return false;
}
pathVisited.add(cellKey);
visitNode(r, c);
if(((r == end[0]) && (c == end[1]))) {
reconstructPath(prev, start, end);
return true;
}
if((depth == limit)) {
pathVisited.delete(cellKey);
return false;
}
foreach(var (nr, nc) in getNeighbors(grid, r, c, allowDiag)) {
var neighborKey = key(nr, nc);
if(pathVisited.has(neighborKey)) {
continue;
}
prev.set(neighborKey, [r, c]);
var found = dls(nr, nc, (depth + 1), limit, pathVisited, prev);
if(found) {
return true;
}
prev.delete(neighborKey);
}
pathVisited.delete(cellKey);
return false;
}
} #include <stdio.h>
void iddfsPathfind(int grid, int start, int end, int allowDiag) {
var maxDepth = (grid.length * grid[0].length);
for(int limit=0; limit<(maxDepth + 1); limit++) {
var pathVisited = Set();
var prev = Map();
var found = dls(start[0], start[1], 0, limit, pathVisited, prev);
if(found) {
return;
}
}
reportNoPath();
int dls(int r, int c, int depth, int limit, int pathVisited, int prev) {
var cellKey = key(r, c);
if((pathVisited.has(cellKey) || (grid[r][c] == WALL))) {
return false;
}
pathVisited.add(cellKey);
visitNode(r, c);
if(((r == end[0]) && (c == end[1]))) {
reconstructPath(prev, start, end);
return true;
}
if((depth == limit)) {
pathVisited.delete(cellKey);
return false;
}
for(int _fod_i = 0; _fod_i < getNeighbors(grid, r, c, allowDiag)_len; _fod_i++) {
int nr = getNeighbors(grid, r, c, allowDiag)[_fod_i][0];
int nc = getNeighbors(grid, r, c, allowDiag)[_fod_i][1];
var neighborKey = key(nr, nc);
if(pathVisited.has(neighborKey)) {
continue;
}
prev.set(neighborKey, [r, c]);
var found = dls(nr, nc, (depth + 1), limit, pathVisited, prev);
if(found) {
return true;
}
prev.delete(neighborKey);
}
pathVisited.delete(cellKey);
return false;
}
}