forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (31 loc) · 1.2 KB
/
index.js
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
//======================================Problem Statement=============================================
// --->> Print all possible path to reach the end of the GRID/MAZE (N x N) from starting point to ending point
// --->> One horizontal move will be represented by H and one vertical move will be represented by V
// --->> Complexity = Complexity will be exponential as there are many overlapping solutions
// --->> cr = current row
// --->> cc = current column
// --->> er = end row
// --->> ec = end column
let getMazePath = (cr, cc, er, ec) => {
if(cr == er && cc == ec) { //============POSITIVE BASE CASE===========
let br = [];
br.push('');
return br;
}
if(cr > er || cc > ec) { //============NEGATIVE BASE CASE===========
let br = [];
return br;
}
let myResult = [];
let recResultH = getMazePath(cr, cc + 1, er, ec);
recResultH.forEach((rrh) => {
myResult.push("H" + rrh);
});
let recResultV = getMazePath(cr + 1, cc, er, ec);
recResultV.forEach((rrv) => {
myResult.push("V" + rrv);
});
return myResult;
}
let path = getMazePath(0, 0, 2, 2);
console.log(path);