Skip to content

Commit 3b4dd28

Browse files
authored
Create mazepath(HV)(D->D).java
Take as input N. N is the number of rows and columns on a square 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) or 1 step diagonally (south-east). But the diagonal step is allowed only when the player is currently on one of the diagonals (there are two diagonals) 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 prints moves for all valid paths across the board (void is the return type for function). Input Format Enter the number N. Constraints None Output Format Display the total number of paths and display all the possible paths in a space separated manner. Sample Input 3 Sample Output VVHH VHVH VHHV VHD HVVH HVHV HVD HHVV DVH DHV DD 11
1 parent 9d7070b commit 3b4dd28

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Recursion/mazepath(HV)(D->D).java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
if (cr==cc || cr==er-cc){
18+
printPath(cr + 1, cc+1, er, ec, path + 'D');
19+
}
20+
21+
}
22+
public static void main(String args[]) {
23+
Scanner sc = new Scanner(System.in);
24+
int n = sc.nextInt();
25+
printPath(0, 0, n-1, n-1, "");
26+
System.out.println();
27+
System.out.println(count);
28+
29+
}
30+
}

0 commit comments

Comments
 (0)