Rat in a Maze Problem I
Problem
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time. If the source cell is 0, the rat cannot move to any other cell.
Solution Approach
Use Backtracking and find all the possible ways of reaching the cell [N, N];
Expected Time complexity:
Click - to see solution code
- C++
class Solution {
vector<vector<int>> maze;
vector<string> ans;
string s;
int n;
public:
void compute(int i, int j) {
if (i == n - 1 && j == n - 1) {
ans.push_back(s);
return;
}
if (i > 0 && maze[i - 1][j]) {
maze[i][j] = 0;
s.push_back('U');
compute(i - 1, j);
s.pop_back();
maze[i][j] = 1;
}
if (j > 0 && maze[i][j - 1]) {
maze[i][j] = 0;
s.push_back('L');
compute(i, j - 1);
s.pop_back();
maze[i][j] = 1;
}
if (i < n - 1 && maze[i + 1][j]) {
maze[i][j] = 0;
s.push_back('D');
compute(i + 1, j);
s.pop_back();
maze[i][j] = 1;
}
if (j < n - 1 && maze[i][j + 1]) {
maze[i][j] = 0;
s.push_back('R');
compute(i, j + 1);
s.pop_back();
maze[i][j] = 1;
}
}
vector<string> findPath(vector<vector<int>> &m, int n) {
this->n = n;
this->maze = m;
s = "";
this->n = n;
if (maze[0][0]) compute(0, 0);
return ans;
}
};