Rat In a Maze All Paths
You are given a 'N' * 'N' maze with a rat placed at 'MAZE[0][0]'. Find and print all paths that rat can follow to reach its destination i.e. 'MAZE['N' - 1]['N' - 1]'. Rat can move in any direction ( left, right, up and down).
Value of every cell in the 'MAZE' can either be 0 or 1. Cells with value 0 are blocked means the rat cannot enter into those cells and those with value 1 are open.
Input Format:
The first line of input contains an integer 'N' representing the dimension of the maze.
The next 'N' lines of input contain 'N' space-separated integers representing the type of the cell.
Output Format :
For each test case, return the path from the start position to the destination position and only cells that are part of the solution path should be 1, rest all cells should be 0.
Output for every test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= N <= 10
0 <= MAZE[i][j] <=1
Where 'MAZE[i][j]' denotes the value in the cell of 'MAZE'.
Time Limit: 1 sec
CodingNinjas
author
2y
Practise many grid problems
CodingNinjas
author
2y
Backtracking Approach
Initialize, all the cells of the solution matrix used to print the path matrix to 0. First, you cannot make use of the existing maze to print the solution maze as you have to dist...read more
Anonymous
1y
trivial, start with {0,0} nd simply change it to -1 look for all four direction [ifValid] via recursion, change it back to 1; if you reach {n-1,n-1} just add this to a answer vector. Return size of th...read more
Add answer anonymously...
Top Uber SDE-2 interview questions & answers
Popular interview questions of SDE-2
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