Rewrote "Rat in a maze" algorithm

It's based on the previous implementation but offers a better API and is now properly testable, too
This commit is contained in:
Roland Hummel
2021-10-03 00:54:14 +02:00
parent 15835ede13
commit 78616fcadd
2 changed files with 201 additions and 48 deletions

View File

@ -1,67 +1,136 @@
/* /*
* Problem Statement: * Problem Statement:
* - Given a NxN grid, find whether rat in cell (0,0) can reach target(N-1,N-1); * - Given a NxN grid, find whether rat in cell [0, 0] can reach the target in cell [N-1, N-1]
* - In grid "0" represent blocked cell and "1" represent empty cell * - The grid is represented as an array of rows. Each row is represented as an array of 0 or 1 values.
* * - A cell with value 0 can not be moved through. Value 1 means the rat can move here.
* Reference for this problem: * - The rat can not move diagonally.
* - https://www.geeksforgeeks.org/rat-in-a-maze-backtracking-2/
* *
* Reference for this problem: https://www.geeksforgeeks.org/rat-in-a-maze-backtracking-2/
*/ */
// Helper function to find if current cell is out of boundary /**
const outOfBoundary = (grid, currentRow, currentColumn) => { * Checks if the given grid is valid.
if (currentRow < 0 || currentColumn < 0 || currentRow >= grid.length || currentColumn >= grid[0].length) { *
return true * A grid needs to satisfy these conditions:
} else { * - must not be empty
return false * - must be a square
} * - must not contain values other than {@code 0} and {@code 1}
*
* @param grid The grid to check.
* @throws TypeError When the given grid is invalid.
*/
function validateGrid (grid) {
if (!Array.isArray(grid) || grid.length === 0) throw new TypeError('Grid must be a non-empty array')
const allRowsHaveCorrectLength = grid.every(row => row.length === grid.length)
if (!allRowsHaveCorrectLength) throw new TypeError('Grid must be a square')
const allCellsHaveValidValues = grid.every(row => {
return row.every(cell => cell === 0 || cell === 1)
})
if (!allCellsHaveValidValues) throw new TypeError('Grid must only contain 0s and 1s')
} }
const printPath = (grid, currentRow, currentColumn, path) => { function isSafe (grid, x, y) {
// If cell is out of boundary, we can't proceed const n = grid.length
if (outOfBoundary(grid, currentRow, currentColumn)) return false return x >= 0 && x < n && y >= 0 && y < n && grid[y][x] === 1
}
// If cell is blocked then you can't go ahead /**
if (grid[currentRow][currentColumn] === 0) return false * Attempts to calculate the remaining path to the target.
*
* @param grid The full grid.
* @param x The current X coordinate.
* @param y The current Y coordinate.
* @param solution The current solution matrix.
* @param path The path we took to get from the source cell to the current location.
* @returns {string|boolean} Either the path to the target cell or false.
*/
function getPathPart (grid, x, y, solution, path) {
const n = grid.length
// If we reached target cell, then print path // are we there yet?
if (currentRow === targetRow && currentColumn === targetColumn) { if (x === n - 1 && y === n - 1 && grid[y][x] === 1) {
console.log(path) solution[y][x] = 1
return true return path
} }
// R,L,D,U are directions `Right, Left, Down, Up` // did we step on a 0 cell or outside the grid?
const directions = [ if (!isSafe(grid, x, y)) return false
[1, 0, 'D'],
[-1, 0, 'U'],
[0, 1, 'R'],
[0, -1, 'L']
]
for (let i = 0; i < directions.length; i++) { // are we walking onto an already-marked solution coordinate?
const nextRow = currentRow + directions[i][0] if (solution[y][x] === 1) return false
const nextColumn = currentColumn + directions[i][1]
const updatedPath = path + directions[i][2]
grid[currentRow][currentColumn] = 0 // none of the above? let's dig deeper!
if (printPath(grid, nextRow, nextColumn, updatedPath)) return true
grid[currentRow][currentColumn] = 1 // mark the current coordinates on the solution matrix
} solution[y][x] = 1
// attempt to move right
const right = getPathPart(grid, x + 1, y, solution, path + 'R')
if (right) return right
// right didn't work: attempt to move down
const down = getPathPart(grid, x, y + 1, solution, path + 'D')
if (down) return down
// down didn't work: attempt to move up
const up = getPathPart(grid, x, y - 1, solution, path + 'U')
if (up) return up
// up didn't work: attempt to move left
const left = getPathPart(grid, x - 1, y, solution, path + 'L')
if (left) return left
// no direction was successful: remove this cell from the solution matrix and backtrack
solution[y][x] = 0
return false return false
} }
// Driver Code function getPath (grid) {
// grid dimensions
const n = grid.length
const grid = [ // prepare solution matrix
[1, 1, 1, 1], const solution = []
[1, 0, 0, 1], for (let i = 0; i < n; i++) {
[0, 0, 1, 1], const row = Array(n)
[1, 1, 0, 1] row.fill(0)
] solution[i] = row
}
const targetRow = grid.length - 1 return getPathPart(grid, 0, 0, solution, '')
const targetColumn = grid[0].length - 1 }
// Variation 2 : print a possible path to reach from (0, 0) to (N-1, N-1) /**
// If there is no path possible then it will print "Not Possible" * Creates an instance of the "rat in a maze" based on a given grid (maze).
!printPath(grid, 0, 0, '') && console.log('Not Possible') */
export class RatInAMaze {
/** Path from the source [0, 0] to the target [N-1, N-1]. */
#_path = ''
#_solved = false
constructor (grid) {
// first, let's do some error checking on the input
validateGrid(grid)
// attempt to solve the maze now - all public methods only query the result state later
const solution = getPath(grid)
if (solution !== false) {
this.#_path = solution
this.#_solved = true
}
}
get solved () {
return this.#_solved
}
get path () {
return this.#_path
}
}

View File

@ -0,0 +1,84 @@
import { RatInAMaze } from '../RatInAMaze'
describe('RatInAMaze', () => {
it('should fail for non-arrays', () => {
const values = [undefined, null, {}, 42, 'hello, world']
for (const value of values) {
expect(() => {new RatInAMaze(value)}).toThrow()
}
})
it('should fail for an empty array', () => {
expect(() => {new RatInAMaze([])}).toThrow()
})
it('should fail for a non-square array', () => {
const array = [
[0, 0, 0],
[0, 0]
]
expect(() => {new RatInAMaze(array)}).toThrow()
})
it('should fail for arrays containing invalid values', () => {
const values = [[[2]], [['a']]]
for (const value of values) {
expect(() => {new RatInAMaze(value)}).toThrow()
}
})
it('should work for a single-cell maze', () => {
const maze = new RatInAMaze([[1]])
expect(maze.solved).toBe(true)
expect(maze.path).toBe('')
})
it('should work for a single-cell maze that can not be solved', () => {
const maze = new RatInAMaze([[0]])
expect(maze.solved).toBe(false)
expect(maze.path).toBe('')
})
it('should work for a simple 3x3 maze', () => {
const maze = new RatInAMaze([[1, 1, 0], [0, 1, 0], [0, 1, 1]])
expect(maze.solved).toBe(true)
expect(maze.path).toBe('RDDR')
})
it('should work for a simple 2x2 that can not be solved', () => {
const maze = new RatInAMaze([[1, 0], [0, 1]])
expect(maze.solved).toBe(false)
expect(maze.path).toBe('')
})
it('should work for a more complex maze', () => {
const maze = new RatInAMaze([
[1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[1, 1, 1, 0, 1, 0, 0],
[1, 0, 1, 0, 1, 0, 0],
[1, 0, 1, 1, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1]
])
expect(maze.solved).toBe(true)
expect(maze.path).toBe('RRRRDDDDLLUULLDDDDRRRRRR')
})
it('should work for a more complex maze that can not be solved', () => {
const maze = new RatInAMaze([
[1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[1, 1, 1, 0, 1, 0, 0],
[1, 0, 1, 0, 1, 0, 0],
[1, 0, 1, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1]
])
expect(maze.solved).toBe(false)
expect(maze.path).toBe('')
})
})