Maze Simple
Simplified version of the rat in the maze algorithm
by Alex Myronov
Babel + JSX
function isSafe(maze, { i, j }, n) {
return i >= 0 && i < n && j >= 0 && j < n && maze[i][j] === 1
}
function find(maze, { i, j }, sol, n) {
if (i === n - 1 && j === n - 1) {
sol.map(row => console.log(row))
return true
}
if (!isSafe(maze, { i, j }, n)) {
return false
}
sol[i][j] = 1
if (find(maze, { i: i + 1, j }, sol, n)) {
return true
}
if (find(maze, { i, j: j + 1 }, sol, n)) {
return true
}
sol[i][j] = 0
return false
}
const m = [
[1, 0, 0, 0],
[1, 1, 0, 1],
[0, 1, 0, 0],
[1, 1, 1, 1],
]
const sol = m.map(row => row.map(cell => 0))
const res = find(m, { i: 0, j: 0 }, sol, m.length)
console.log('res:', res)