Skip to content

Commit 33f7f42

Browse files
authored
Create mazepath(HV).java
Take as input N1 and N2, both numbers. N1 and N2 is the number of rows and columns on a rectangular board. Our player starts in top-left corner of the board and must reach bottom-right corner. In one move the player can move 1 step horizontally (right) or 1 step vertically (down). a. Write a recursive function which returns the count of different ways the player can travel across the board. Print the value returned. b. Write a recursive function which returns an ArrayList of moves for all valid paths across the board. Print the value returned. e.g. for a board of size 3,3; a few valid paths will be “HHVV”, “VVHH”, “VHHV” etc. c. Write a recursive function which prints moves for all valid paths across the board (void is the return type for function). Input Format Enter the number of rows N and columns M Constraints None Output Format Display the total number of paths and display all the possible paths in a space separated manner Sample Input 3 3 Sample Output VVHH VHVH VHHV HVVH HVHV HHVV 6
1 parent 3b4dd28 commit 33f7f42

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Recursion/mazepath(HV).java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.*;
2+
public class Main {
3+
static int count;
4+
public static void printPath(int cr, int cc, int er, int ec, String path) {
5+
if(cr > er || cc > ec) {
6+
7+
return;
8+
}
9+
10+
if(cr == er && cc == ec) {
11+
count++;
12+
System.out.print(path + " ");
13+
return;
14+
}
15+
printPath(cr + 1, cc, er, ec, path + 'V');
16+
printPath(cr, cc + 1, er, ec, path + 'H');
17+
}
18+
public static void main(String args[]) {
19+
Scanner sc = new Scanner(System.in);
20+
int n1 = sc.nextInt();
21+
int n2 = sc.nextInt();
22+
printPath(0, 0, n1 - 1, n2 - 1, "");
23+
System.out.println();
24+
System.out.println(count);
25+
26+
}
27+
}

0 commit comments

Comments
 (0)