Skip to content

Commit 51c5ea4

Browse files
RatInAMaze prints path now
1 parent 9b37cb0 commit 51c5ea4

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

Backtracking/RatInAMaze.js

+47-34
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,65 @@
33
* - Given a NxN grid, find whether rat in cell (0,0) can reach target(N-1,N-1);
44
* - In grid "0" represent blocked cell and "1" represent empty cell
55
*
6+
* Reference for this problem:
7+
* - https://www.geeksforgeeks.org/rat-in-a-maze-backtracking-2/
8+
*
69
*/
710

11+
// Helper function to find if current cell is out of boundary
812
const outOfBoundary = (grid, currentRow, currentColumn) => {
9-
if (currentRow < 0 || currentColumn < 0 || currentRow >= grid.length || currentColumn >= grid[0].length) return true
10-
else return false
13+
if (currentRow < 0 || currentColumn < 0 || currentRow >= grid.length || currentColumn >= grid[0].length) {
14+
return true
15+
} else {
16+
return false
17+
}
1118
}
1219

13-
const isPossible = (grid, currentRow, currentColumn) => {
14-
if (outOfBoundary(grid, currentRow, currentColumn)) return false
15-
16-
if (grid[currentRow][currentColumn] === 0) return false
17-
18-
if (currentRow === targetRow && currentColumn === targetColumn) {
19-
return true
20-
}
21-
22-
const directions = [
23-
[1, 0],
24-
[0, 1],
25-
[-1, 0],
26-
[0, -1]
27-
]
28-
29-
for (let i = 0; i < directions.length; i++) {
30-
const nextRow = currentRow + directions[i][0]; const nextColumn = currentColumn + directions[i][1]
31-
grid[currentRow][currentColumn] = 0
32-
if (isPossible(grid, nextRow, nextColumn)) return true
33-
grid[currentRow][currentColumn] = 1
34-
}
35-
return false
20+
const printPath = (grid, currentRow, currentColumn, path) => {
21+
// If cell is out of boundary, we can't proceed
22+
if (outOfBoundary(grid, currentRow, currentColumn)) return false
23+
24+
// If cell is blocked then you can't go ahead
25+
if (grid[currentRow][currentColumn] === 0) return false
26+
27+
// If we reached target cell, then print path
28+
if (currentRow === targetRow && currentColumn === targetColumn) {
29+
console.log(path)
30+
return true
31+
}
32+
33+
// R,L,D,U are directions `Right, Left, Down, Up`
34+
const directions = [
35+
[1, 0, 'D'],
36+
[-1, 0, 'U'],
37+
[0, 1, 'R'],
38+
[0, -1, 'L']
39+
]
40+
41+
for (let i = 0; i < directions.length; i++) {
42+
const nextRow = currentRow + directions[i][0]
43+
const nextColumn = currentColumn + directions[i][1]
44+
const updatedPath = path + directions[i][2]
45+
46+
grid[currentRow][currentColumn] = 0
47+
if (printPath(grid, nextRow, nextColumn, updatedPath)) return true
48+
grid[currentRow][currentColumn] = 1
49+
}
50+
return false
3651
}
3752

3853
// Driver Code
3954

4055
const grid = [
41-
[1, 1, 1, 1],
42-
[1, 0, 0, 1],
43-
[0, 0, 1, 0],
44-
[1, 1, 0, 1]
56+
[1, 1, 1, 1],
57+
[1, 0, 0, 1],
58+
[0, 0, 1, 1],
59+
[1, 1, 0, 1]
4560
]
4661

4762
const targetRow = grid.length - 1
4863
const targetColumn = grid[0].length - 1
4964

50-
if (isPossible(grid, 0, 0)) {
51-
console.log('Possible')
52-
} else {
53-
console.log('Not Possible')
54-
}
65+
// Variation 2 : print a possible path to reach from (0, 0) to (N-1, N-1)
66+
// If there is no path possible then it will print "Not Possible"
67+
!printPath(grid, 0, 0, '') && console.log('Not Possible')

0 commit comments

Comments
 (0)