in the Given 2D Grid Map, 'S' represents the start point, 'E' represents the end point, '0' represents an impassable wall, and '1' represents a passable path. Find the shortest path from the start point to the end point. if no path exists, return -1.
const grid = [
['S', '1', '0', '1', '1'],
['1', '1', '0', '1', '1'],
['0', '1', '1', '1', '0'],
['1', '0', '0', '1', 'E']
];
// output: 7
function bfsRecursive(grid, startX, startY, distance = 0, visited = new Set()){
visited.add(`${startX} ${startY}`);
const map = [
[-1,0], //top
[1,0], //bottom
[0,-1], //left
[0,1], //right
]
if(grid[startX][startY] === 'E'){
return distance;
}
let result = Infinity;
for(const [dx, dy] of map){
const newX = startX+dx;
const newY = startY+dy;
if(
newX >= 0
&& newX < grid.length
&& newY >= 0
&& newY < grid[newX].length
&& grid[newX][newY] !== '0'
&& !visited.has(`${newX} ${newY}`)
){
const number = bfsRecursive(grid, newX, newY, distance+1, visited);
if(number !== -1 && number < result){
result = number;
}
}
}
return result;
}
function solution(grid){
let startX, startY;
for(let x=0; x < grid.length; x++){
for(let y=0; y < grid[x].length; y++){
if(grid[x][y] === 'S'){
startX=x;
startY=y;
break; }
}
}
// const result = bfsQueue(grid, startX, startY);
const result = bfsRecursive(grid,startX, startY);
}
const grid = [
['S', '1', '0', '1', '1'],
['1', '1', '0', '1', '1'],
['0', '1', '1', '1', '0'],
['1', '0', '0', '1', 'E']
];
solution(grid);
startX, startY, 0;
const visited = new Set();
visited.add(`${startX} ${startY}`);
while(queue.length > 0) {
const [x, y, distance] = queue.shift();
console.log("=>(2.js:14) x,y,distance", x,y,distance);
if(grid[x][y] === 'E'){
return distance;
}
for(const [dx, dy] of directions){
const newX = x + dx;
const newY = y + dy;
if(
newX >= 0
&& newX < grid.length
&& newY >= 0
&& newY < grid[newX].length
&& grid[newX][newY] !== '0'
&& !visited.has(`${newX} ${newY}`)
){
queue.push([newX, newY, distance + 1])
visited.add(`${newX} ${newY}`)
} }
}
return -1;
}
function solution(grid){
let startX, startY;
for(let x=0; x < grid.length; x++){
for(let y=0; y < grid[x].length; y++){
if(grid[x][y] === 'S'){
startX=x;
startY=y;
break; }
}
}
const result = bfsQueue(grid, startX, startY);
}
const grid = [
['S', '1', '0', '1', '1'],
['1', '1', '0', '1', '1'],
['0', '1', '1', '1', '0'],
['1', '0', '0', '1', 'E']
];
solution(grid);">
function bfsQueue(grid, startX, startY){ const directions = [ [-1, 0], //top [1, 0], //bottom [0, -1], //left [0, 1] //right ] const queue = [[startX, startY, 0]]; const visited = new Set(); visited.add(`${startX} ${startY}`); while(queue.length > 0) { const [x, y, distance] = queue.shift(); console.log("=>(2.js:14) x,y,distance", x,y,distance); if(grid[x][y] === 'E'){ return distance; } for(const [dx, dy] of directions){ const newX = x + dx; const newY = y + dy; if( newX >= 0 && newX < grid.length && newY >= 0 && newY < grid[newX].length && grid[newX][newY] !== '0' && !visited.has(`${newX} ${newY}`) ){ queue.push([newX, newY, distance + 1]) visited.add(`${newX} ${newY}`) } } } return -1; } function solution(grid){ let startX, startY; for(let x=0; x < grid.length; x++){ for(let y=0; y < grid[x].length; y++){ if(grid[x][y] === 'S'){ startX=x; startY=y; break; } } } const result = bfsQueue(grid, startX, startY); } const grid = [ ['S', '1', '0', '1', '1'], ['1', '1', '0', '1', '1'], ['0', '1', '1', '1', '0'], ['1', '0', '0', '1', 'E'] ]; solution(grid);