1
+ //======================================Problem Statement=============================================
2
+ // --->> Print all possible path to reach the end of the GRID/MAZE (N x N) from starting point to ending point
3
+ // --->> One horizontal move will be represented by H and one vertical move will be represented by V
4
+ // --->> Complexity = Complexity will be exponential as there are many overlapping solutions
5
+ // --->> cr = current row
6
+ // --->> cc = current column
7
+ // --->> er = end row
8
+ // --->> ec = end column
9
+
10
+
11
+
12
+
13
+ let getMazePath = ( cr , cc , er , ec ) => {
14
+ if ( cr == er && cc == ec ) { //============POSITIVE BASE CASE===========
15
+ let br = [ ] ;
16
+ br . push ( '' ) ;
17
+ return br ;
18
+ }
19
+
20
+ if ( cr > er || cc > ec ) { //============NEGATIVE BASE CASE===========
21
+ let br = [ ] ;
22
+ return br ;
23
+ }
24
+
25
+ let myResult = [ ] ;
26
+
27
+ let recResultH = getMazePath ( cr , cc + 1 , er , ec ) ;
28
+ recResultH . forEach ( ( rrh ) => {
29
+ myResult . push ( "H" + rrh ) ;
30
+ } ) ;
31
+
32
+ let recResultV = getMazePath ( cr + 1 , cc , er , ec ) ;
33
+ recResultV . forEach ( ( rrv ) => {
34
+ myResult . push ( "V" + rrv ) ;
35
+ } ) ;
36
+
37
+ return myResult ;
38
+ }
39
+
40
+
41
+ let path = getMazePath ( 0 , 0 , 2 , 2 ) ;
42
+ console . log ( path ) ;
0 commit comments