Rat in a Maze: All Paths Problem
You are provided with an N * N
maze where a rat is positioned at starting cell MAZE[0][0]
. The goal is to determine and print all possible paths that the rat can take to reach its destination at MAZE[N-1][N-1]
. The rat can move in any of the four cardinal directions: left, right, up, or down.
The maze's configuration is such that each cell holds a binary value, where 0 represents a blocked cell that the rat cannot traverse, and 1 signifies an open cell.
Input:
An integer N
indicating the dimension of the maze.
Following this, there are N
lines, each containing N
space-separated integers indicating the cell type (0 or 1).
Output:
For every test case, return the path from the starting position to the destination. In each path's representation, only the cells that constitute the path should have a value of 1, while all others should be 0.
Output for each test case should be on a new line.
Example:
Input:
N = 4
MAZE = [[1, 1, 0, 0],
[1, 1, 1, 0],
[0, 1, 0, 0],
[1, 1, 1, 1]]
Output:
[
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 1]
]
(Other valid paths may also be printed)
Constraints:
- 1 <= N <= 10
- 0 <= MAZE[i][j] <= 1
- Time Limit: 1 sec
Note:
You do not need to print anything, it is already handled. Implement the specified function to return the answer.
AnswerBot
6d
Find all possible paths for a rat in a maze from start to finish.
Use backtracking to explore all possible paths in the maze.
At each cell, check if it is a valid move and mark it as part of the path.
Ex...read more
Help your peers!
Add answer anonymously...
Top Hexaware Technologies Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Hexaware Technologies Software Developer
>
Hexaware Technologies Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app