Skip to content

Commit a8b8ae7

Browse files
committed
counts all possible maze path (2d matrix), recursively
1 parent 883bd56 commit a8b8ae7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package Lecture11;
2+
3+
public class CountMazePath {
4+
5+
public static void main(String[] args) {
6+
7+
int endrow = 2, endcol = 2, currentrow = 0, currentcol = 0;
8+
String result = "";
9+
10+
System.out
11+
.println("total path is: "
12+
+ countGridPath(endrow, endcol, currentrow, currentcol,
13+
result));
14+
}
15+
16+
public static int countGridPath(int er, int ec, int cr, int cc,
17+
String result) {
18+
19+
if (cr > er | cc > ec) {
20+
return 0;
21+
}
22+
if (cr == er && cc == ec) {
23+
return 1;
24+
}
25+
int total = 0;
26+
total = total + countGridPath(er, ec, cr, cc + 1, result + "H");
27+
total = total + countGridPath(er, ec, cr + 1, cc, result + "V");
28+
29+
return total;
30+
}
31+
}

0 commit comments

Comments
 (0)