-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathindex.js
39 lines (32 loc) · 1.11 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
//= =====================================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
const getMazePath = (cr, cc, er, ec) => {
if (cr === er && cc === ec) {
//= ===========POSITIVE BASE CASE===========
const br = [];
br.push('');
return br;
}
if (cr > er || cc > ec) {
//= ===========NEGATIVE BASE CASE===========
const br = [];
return br;
}
const myResult = [];
const recResultH = getMazePath(cr, cc + 1, er, ec);
recResultH.forEach((rrh) => {
myResult.push(`H${rrh}`);
});
const recResultV = getMazePath(cr + 1, cc, er, ec);
recResultV.forEach((rrv) => {
myResult.push(`V${rrv}`);
});
return myResult;
};
module.exports = { getMazePath };