Consider a rat placed at (0, 0) in a square matrix m[ ][ ] of order n and has to reach the destination at (n-1, n-1). The task is to find a sorted array of strings denoting all the possible directions which the rat can take to reach the destination at (n-1, n-1). The directions in which the rat can move are ‘U'(up), ‘D'(down), ‘L’ (left), ‘R’ (right).
Input : N = 4
1 0 0 0
1 1 0 1
0 1 0 0
0 1 1 1
Input :N = 4
1 0 0 0
1 1 0 1
1 1 0 0
0 1 1 1
- Start from the initial index (i.e. (0,0)) and look for the valid moves through the adjacent cells in the order Down->Left->Right->Up (so as to get the sorted paths) in the grid.
- If the move is possible, then move to that cell while storing the character corresponding to the move(D,L,R,U) and again start looking for the valid move until the last index (i.e. (n-1,n-1)) is reached.
- Also, keep on marking the cells as visited and when we traversed all the paths possible from that cell, then unmark that cell for other different paths and remove the character from the path formed.
- As the last index of the grid(bottom right) is reached, then store the traversed path.
Below is the implementation of the above approach:
DDRRURRDDD DDRURRRDDD DRDRURRDDD DRRRRDDD
- Time Complexity: O(3^(n^2)).
As there are N^2 cells from each cell there are 3 unvisited neighbouring cells. So the time complexity O(3^(N^2).
- Auxiliary Space: O(3^(n^2)).
As there can be atmost 3^(n^2) cells in the answer so the space complexity is O(3^(n^2)).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Rat in a Maze with multiple steps or jump allowed
- A variation of Rat in a Maze : multiple steps or jumps allowed
- Rat in a Maze | Backtracking-2
- Rat in a Maze | Backtracking using Stack
- Find single Movement in a Matrix
- Find paths from corner cell to middle cell in maze
- Count number of ways to reach destination in a Maze
- Count number of ways to reach destination in a maze
- Count number of ways to reach destination in a Maze using BFS
- Maze With N doors and 1 Key
- Shortest path in a Binary Maze
- Print all paths from top left to bottom right in a matrix with four moves allowed
- Find the largest rectangle of 1's with swapping of columns allowed
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
- Maximum points collected by two persons allowed to meet once
- Minimum cost to reach a point N from 0 with two different operations allowed
- Find maximum path sum in a 2D matrix when exactly two left moves are allowed
- Printing all solutions in N-Queen Problem
- Print all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a mXn matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : andrew1234