From 832aa6ea0610cc39ee8afb180a22aaaf74a103fe Mon Sep 17 00:00:00 2001 From: Chiranjeev <6258860@gmail.com> Date: Sun, 22 Aug 2021 22:44:52 +0530 Subject: [PATCH 1/4] RatInAMaze added in Backtracking --- Backtracking/RatInAMaze.js | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Backtracking/RatInAMaze.js diff --git a/Backtracking/RatInAMaze.js b/Backtracking/RatInAMaze.js new file mode 100644 index 000000000..c1d3297a9 --- /dev/null +++ b/Backtracking/RatInAMaze.js @@ -0,0 +1,55 @@ +/* + * Problem Statement: + * - Given a NxN grid, find whether rat in cell (0,0) can reach target(N-1,N-1); + * - In grid "0" represent blocked cell and "1" represent empty cell + * + */ + +const outOfBoundary = (grid, currentRow, currentColumn) => { + if (currentRow < 0 || currentColumn < 0 || currentRow >= grid.length || currentColumn >= grid[0].length) return true + else return false +} + +const isPossible = (grid, currentRow, currentColumn) => { + if (outOfBoundary(grid, currentRow, currentColumn)) return false + + console.log(currentRow, currentColumn) + if (grid[currentRow][currentColumn] === 0) return false + + if (currentRow === targetRow && currentColumn === targetColumn) { + return true + } + + const directions = [ + [1, 0], + [0, 1], + [-1, 0], + [0, -1] + ] + + for (let i = 0; i < directions.length; i++) { + const nextRow = currentRow + directions[i][0]; const nextColumn = currentColumn + directions[i][1] + grid[currentRow][currentColumn] = 0 + if (isPossible(grid, nextRow, nextColumn)) return true + grid[currentRow][currentColumn] = 1 + } + return false +} + +// Driver Code + +const grid = [ + [1, 1, 1, 1], + [1, 0, 0, 1], + [0, 1, 0, 1], + [1, 1, 1, 1] +] + +const targetRow = grid.length - 1 +const targetColumn = grid[0].length - 1 + +if (isPossible(grid, 0, 0)) { + console.log('Possible') +} else { + console.log('Not Possible') +} From 9b37cb07059537d28e60678b9cbed28b25221744 Mon Sep 17 00:00:00 2001 From: Chiranjeev <6258860@gmail.com> Date: Sun, 22 Aug 2021 22:46:29 +0530 Subject: [PATCH 2/4] removed console lines from RatInAMaze --- Backtracking/RatInAMaze.js | 53 +++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/Backtracking/RatInAMaze.js b/Backtracking/RatInAMaze.js index c1d3297a9..923abcc5f 100644 --- a/Backtracking/RatInAMaze.js +++ b/Backtracking/RatInAMaze.js @@ -6,50 +6,49 @@ */ const outOfBoundary = (grid, currentRow, currentColumn) => { - if (currentRow < 0 || currentColumn < 0 || currentRow >= grid.length || currentColumn >= grid[0].length) return true - else return false + if (currentRow < 0 || currentColumn < 0 || currentRow >= grid.length || currentColumn >= grid[0].length) return true + else return false } 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) { - return true - } + if (currentRow === targetRow && currentColumn === targetColumn) { + return true + } - const directions = [ - [1, 0], - [0, 1], - [-1, 0], - [0, -1] - ] + const directions = [ + [1, 0], + [0, 1], + [-1, 0], + [0, -1] + ] - for (let i = 0; i < directions.length; i++) { - const nextRow = currentRow + directions[i][0]; const nextColumn = currentColumn + directions[i][1] - grid[currentRow][currentColumn] = 0 - if (isPossible(grid, nextRow, nextColumn)) return true - grid[currentRow][currentColumn] = 1 - } - return false + for (let i = 0; i < directions.length; i++) { + const nextRow = currentRow + directions[i][0]; const nextColumn = currentColumn + directions[i][1] + grid[currentRow][currentColumn] = 0 + if (isPossible(grid, nextRow, nextColumn)) return true + grid[currentRow][currentColumn] = 1 + } + return false } // Driver Code const grid = [ - [1, 1, 1, 1], - [1, 0, 0, 1], - [0, 1, 0, 1], - [1, 1, 1, 1] + [1, 1, 1, 1], + [1, 0, 0, 1], + [0, 0, 1, 0], + [1, 1, 0, 1] ] const targetRow = grid.length - 1 const targetColumn = grid[0].length - 1 if (isPossible(grid, 0, 0)) { - console.log('Possible') + console.log('Possible') } else { - console.log('Not Possible') + console.log('Not Possible') } From 51c5ea48b89b3398c88f932bafe2a08fa8213278 Mon Sep 17 00:00:00 2001 From: Chiranjeev <6258860@gmail.com> Date: Sun, 22 Aug 2021 23:24:13 +0530 Subject: [PATCH 3/4] RatInAMaze prints path now --- Backtracking/RatInAMaze.js | 73 ++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/Backtracking/RatInAMaze.js b/Backtracking/RatInAMaze.js index 923abcc5f..df38c4545 100644 --- a/Backtracking/RatInAMaze.js +++ b/Backtracking/RatInAMaze.js @@ -3,52 +3,65 @@ * - Given a NxN grid, find whether rat in cell (0,0) can reach target(N-1,N-1); * - In grid "0" represent blocked cell and "1" represent empty cell * + * 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) => { - if (currentRow < 0 || currentColumn < 0 || currentRow >= grid.length || currentColumn >= grid[0].length) return true - else return false + if (currentRow < 0 || currentColumn < 0 || currentRow >= grid.length || currentColumn >= grid[0].length) { + return true + } else { + return false + } } -const isPossible = (grid, currentRow, currentColumn) => { - if (outOfBoundary(grid, currentRow, currentColumn)) return false +const printPath = (grid, currentRow, currentColumn, path) => { + // If cell is out of boundary, we can't proceed + if (outOfBoundary(grid, currentRow, currentColumn)) return false - if (grid[currentRow][currentColumn] === 0) return false + // If cell is blocked then you can't go ahead + if (grid[currentRow][currentColumn] === 0) return false - if (currentRow === targetRow && currentColumn === targetColumn) { - return true - } + // If we reached target cell, then print path + if (currentRow === targetRow && currentColumn === targetColumn) { + console.log(path) + return true + } - const directions = [ - [1, 0], - [0, 1], - [-1, 0], - [0, -1] - ] + // R,L,D,U are directions `Right, Left, Down, Up` + const directions = [ + [1, 0, 'D'], + [-1, 0, 'U'], + [0, 1, 'R'], + [0, -1, 'L'] + ] - for (let i = 0; i < directions.length; i++) { - const nextRow = currentRow + directions[i][0]; const nextColumn = currentColumn + directions[i][1] - grid[currentRow][currentColumn] = 0 - if (isPossible(grid, nextRow, nextColumn)) return true - grid[currentRow][currentColumn] = 1 - } - return false + for (let i = 0; i < directions.length; i++) { + const nextRow = currentRow + directions[i][0] + const nextColumn = currentColumn + directions[i][1] + const updatedPath = path + directions[i][2] + + grid[currentRow][currentColumn] = 0 + if (printPath(grid, nextRow, nextColumn, updatedPath)) return true + grid[currentRow][currentColumn] = 1 + } + return false } // Driver Code const grid = [ - [1, 1, 1, 1], - [1, 0, 0, 1], - [0, 0, 1, 0], - [1, 1, 0, 1] + [1, 1, 1, 1], + [1, 0, 0, 1], + [0, 0, 1, 1], + [1, 1, 0, 1] ] const targetRow = grid.length - 1 const targetColumn = grid[0].length - 1 -if (isPossible(grid, 0, 0)) { - console.log('Possible') -} else { - console.log('Not Possible') -} +// 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" +!printPath(grid, 0, 0, '') && console.log('Not Possible') From b2b78127d4d4814e955e4b114009f52c2d95db9c Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 23 Aug 2021 04:06:07 +0000 Subject: [PATCH 4/4] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7cb95ddfb..8a51ddf54 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -5,6 +5,7 @@ * [GeneratePermutations](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/GeneratePermutations.js) * [KnightTour](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/KnightTour.js) * [NQueen](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/NQueen.js) + * [RatInAMaze](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/RatInAMaze.js) * [Sudoku](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/Sudoku.js) * tests * [NQueen](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/tests/NQueen.test.js)