removed console lines from RatInAMaze

This commit is contained in:
Chiranjeev
2021-08-22 22:46:29 +05:30
parent 832aa6ea06
commit 9b37cb0705

View File

@ -6,50 +6,49 @@
*/ */
const outOfBoundary = (grid, currentRow, currentColumn) => { const outOfBoundary = (grid, currentRow, currentColumn) => {
if (currentRow < 0 || currentColumn < 0 || currentRow >= grid.length || currentColumn >= grid[0].length) return true if (currentRow < 0 || currentColumn < 0 || currentRow >= grid.length || currentColumn >= grid[0].length) return true
else return false else return false
} }
const isPossible = (grid, currentRow, currentColumn) => { const isPossible = (grid, currentRow, currentColumn) => {
if (outOfBoundary(grid, currentRow, currentColumn)) return false if (outOfBoundary(grid, currentRow, currentColumn)) return false
console.log(currentRow, currentColumn) if (grid[currentRow][currentColumn] === 0) return false
if (grid[currentRow][currentColumn] === 0) return false
if (currentRow === targetRow && currentColumn === targetColumn) { if (currentRow === targetRow && currentColumn === targetColumn) {
return true return true
} }
const directions = [ const directions = [
[1, 0], [1, 0],
[0, 1], [0, 1],
[-1, 0], [-1, 0],
[0, -1] [0, -1]
] ]
for (let i = 0; i < directions.length; i++) { for (let i = 0; i < directions.length; i++) {
const nextRow = currentRow + directions[i][0]; const nextColumn = currentColumn + directions[i][1] const nextRow = currentRow + directions[i][0]; const nextColumn = currentColumn + directions[i][1]
grid[currentRow][currentColumn] = 0 grid[currentRow][currentColumn] = 0
if (isPossible(grid, nextRow, nextColumn)) return true if (isPossible(grid, nextRow, nextColumn)) return true
grid[currentRow][currentColumn] = 1 grid[currentRow][currentColumn] = 1
} }
return false return false
} }
// Driver Code // Driver Code
const grid = [ const grid = [
[1, 1, 1, 1], [1, 1, 1, 1],
[1, 0, 0, 1], [1, 0, 0, 1],
[0, 1, 0, 1], [0, 0, 1, 0],
[1, 1, 1, 1] [1, 1, 0, 1]
] ]
const targetRow = grid.length - 1 const targetRow = grid.length - 1
const targetColumn = grid[0].length - 1 const targetColumn = grid[0].length - 1
if (isPossible(grid, 0, 0)) { if (isPossible(grid, 0, 0)) {
console.log('Possible') console.log('Possible')
} else { } else {
console.log('Not Possible') console.log('Not Possible')
} }