function bellmanfordPathfind(grid, start, end, allowDiag) {
const rows = grid.length;
const cols = grid[0].length;
const dist = Matrix(rows, cols, Infinity);
const prev = new Map();
dist[start[0]][start[1]] = 0;
const total = rows * cols;
for (let iter = 0; iter < total - 1; iter++) {
let updated = false;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === WALL || dist[r][c] === Infinity) continue;
let relaxedHere = false;
for (const [nr, nc] of getNeighbors(grid, r, c, allowDiag)) {
const nd = dist[r][c] + stepCost(grid, r, c, nr, nc);
if (nd < dist[nr][nc]) {
dist[nr][nc] = nd;
prev.set(key(nr, nc), [r, c]);
updated = true;
if (!relaxedHere) {
visitNode(r, c);
relaxedHere = true;
}
pushFrontier(nr, nc);
}
}
}
}
if (!updated) break;
}
if (dist[end[0]][end[1]] !== Infinity) {
reconstructPath(prev, start, end);
} else {
reportNoPath();
}
} def bellmanfordPathfind(grid, start, end, allowDiag):
rows = grid.length
cols = grid[0].length
dist = Matrix(rows, cols, Infinity)
prev = Map()
dist[start[0]][start[1]] = 0
total = (rows * cols)
for iter in range((total - 1)):
updated = False
for r in range(rows):
for c in range(cols):
if ((grid[r][c] == WALL) or (dist[r][c] == Infinity)):
continue
relaxedHere = False
for nr, nc in getNeighbors(grid, r, c, allowDiag):
nd = (dist[r][c] + stepCost(grid, r, c, nr, nc))
if (nd < dist[nr][nc]):
dist[nr][nc] = nd
prev.set(key(nr, nc), [r, c])
updated = True
if not relaxedHere:
visitNode(r, c)
relaxedHere = True
pushFrontier(nr, nc)
if not updated:
break
if (dist[end[0]][end[1]] != Infinity):
reconstructPath(prev, start, end)
else:
reportNoPath() #include <vector>
#include <algorithm>
void bellmanfordPathfind(int grid, int start, int end, int allowDiag) {
auto rows = grid.length;
auto cols = grid[0].length;
auto dist = Matrix(rows, cols, Infinity);
auto prev = Map();
dist[start[0]][start[1]] = 0;
auto total = (rows * cols);
for(int iter=0; iter<(total - 1); iter++) {
auto updated = false;
for(int r=0; r<rows; r++) {
for(int c=0; c<cols; c++) {
if(((grid[r][c] == WALL) || (dist[r][c] == Infinity))) {
continue;
}
auto relaxedHere = false;
for(auto& [nr, nc] : getNeighbors(grid, r, c, allowDiag)) {
auto nd = (dist[r][c] + stepCost(grid, r, c, nr, nc));
if((nd < dist[nr][nc])) {
dist[nr][nc] = nd;
prev.set(key(nr, nc), [r, c]);
updated = true;
if(!relaxedHere) {
visitNode(r, c);
relaxedHere = true;
}
pushFrontier(nr, nc);
}
}
}
}
if(!updated) {
break;
}
}
if((dist[end[0]][end[1]] != Infinity)) {
reconstructPath(prev, start, end);
} else {
reportNoPath();
}
} public void bellmanfordPathfind(int grid, int start, int end, int allowDiag) {
var rows = grid.length;
var cols = grid[0].length;
var dist = Matrix(rows, cols, Infinity);
var prev = Map();
dist[start[0]][start[1]] = 0;
var total = (rows * cols);
for(int iter=0; iter<(total - 1); iter++) {
var updated = false;
for(int r=0; r<rows; r++) {
for(int c=0; c<cols; c++) {
if(((grid[r][c] == WALL) || (dist[r][c] == Infinity))) {
continue;
}
var relaxedHere = false;
foreach(var (nr, nc) in getNeighbors(grid, r, c, allowDiag)) {
var nd = (dist[r][c] + stepCost(grid, r, c, nr, nc));
if((nd < dist[nr][nc])) {
dist[nr][nc] = nd;
prev.set(key(nr, nc), [r, c]);
updated = true;
if(!relaxedHere) {
visitNode(r, c);
relaxedHere = true;
}
pushFrontier(nr, nc);
}
}
}
}
if(!updated) {
break;
}
}
if((dist[end[0]][end[1]] != Infinity)) {
reconstructPath(prev, start, end);
} else {
reportNoPath();
}
} #include <stdio.h>
void bellmanfordPathfind(int grid, int start, int end, int allowDiag) {
var rows = grid.length;
var cols = grid[0].length;
var dist = Matrix(rows, cols, Infinity);
var prev = Map();
dist[start[0]][start[1]] = 0;
var total = (rows * cols);
for(int iter=0; iter<(total - 1); iter++) {
var updated = false;
for(int r=0; r<rows; r++) {
for(int c=0; c<cols; c++) {
if(((grid[r][c] == WALL) || (dist[r][c] == Infinity))) {
continue;
}
var relaxedHere = 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 nd = (dist[r][c] + stepCost(grid, r, c, nr, nc));
if((nd < dist[nr][nc])) {
dist[nr][nc] = nd;
prev.set(key(nr, nc), [r, c]);
updated = true;
if(!relaxedHere) {
visitNode(r, c);
relaxedHere = true;
}
pushFrontier(nr, nc);
}
}
}
}
if(!updated) {
break;
}
}
if((dist[end[0]][end[1]] != Infinity)) {
reconstructPath(prev, start, end);
} else {
reportNoPath();
}
}