-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRatInAMaze.cpp
53 lines (41 loc) · 1.08 KB
/
RatInAMaze.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void helper(vector<vector<int>> &maze, int r, int c, string path, vector<string> &ans)
{
int n = maze.size();
if (r < 0 || c < 0 || r >= n || c >= n || maze[r][c] == 0 || maze[r][c] == -1) // base case
{
return;
}
if (r == n - 1 && c == n - 1)
{
ans.push_back(path);
return;
}
maze[r][c] = -1; // visited
helper(maze, r + 1, c, path + "D", ans); // down
helper(maze, r - 1, c, path + "U", ans); // up
helper(maze, r, c - 1, path + "L", ans); // left
helper(maze, r, c + 1, path + "R", ans); // right
maze[r][c] = 1; // backtracking
}
vector<string> findPath(vector<vector<int>> &maze)
{
int n = maze.size();
vector<string> ans;
string path = "";
helper(maze, 0, 0, path, ans);
return ans;
}
int main()
{
vector<vector<int>> maze = {{1, 0, 0, 0}, {1, 1, 0, 1}, {1, 1, 0, 0}, {0, 1, 1, 1}};
vector<string> ans = findPath(maze);
for (string path : ans)
{
cout << path << endl;
}
return 0;
}