forked from strang3-r/Leetcode75
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get maze.cpp
56 lines (51 loc) · 1.23 KB
/
get maze.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
54
55
56
//use eulers tree to understand
// sc=starting col
// sr=starting row
// dc =destination col
// dr =destination row
// v & h= vertical and horizontal
#include <iostream>
#include <vector>
using namespace std;
vector<string> getpath(int sr, int sc, int dr, int dc)
{
if (sr == dr && sc == dc) //base case
{
vector<string> base;
base.push_back("");
return base;
}
vector<string> ans;
if (sc + 1 <= dc) // condition to not go outside the maze
{
vector<string> hpath = getpath(sr, sc + 1, dr, dc); //moving in horizontsl means rigth side
for (string s : hpath)
{
ans.push_back("h" + s);
}
}
if (sr + 1 <= dr) // condition to not go outside the maze
{
vector<string> vpath = getpath(sr + 1, sc, dr, dc); //moving in vertical direction means downwards
for (string s : vpath)
{
ans.push_back("v" + s);
}
}
return ans;
}
void display(vector<string> &a) //display the output
{
for (int i = 0; i < a.size(); i++)
{
cout << a[i];
cout << " ";
}
}
int main()
{
int n, m;
cin >> n >> m;
vector<string> answer = getpath(0, 0, n - 1, m - 1);
display(answer);
}