Monday, January 31, 2022

Print Maze Path With Multiple Jumps

 




public class PrintMazePathWithMultipleJumps {
public static void main(String[] args) throws Exception {
printMazePaths(0, 0, 2, 2, "");
}

// sr - source row
// sc - source column
// dr - destination row
// dc - destination column
public static void printMazePaths(int sr, int sc, int dr, int dc, String psf) {
if (sr == dr && sc == dc) {
System.out.println(psf);
return;
}

// horizontal
for (int jump = 1; jump <= dc - sc; jump++) {
printMazePaths(sr, sc + jump, dr, dc, psf + "h" + jump);
}

// vertical
for (int jump = 1; jump <= dr - sr; jump++) {
printMazePaths(sr + jump, sc, dr, dc, psf + "v" + jump);
}

for (int jump = 1; jump <= dc - sc && jump <= dr - sr; jump++) {
printMazePaths(sr + jump, sc + jump, dr, dc, psf + "d" + jump);
}
}
}

No comments:

Post a Comment

Diagonal Traversal

 eg.  1       2       3       4 5      6       7       8 9    10    11     12 13  14   15    16 Output: 1 6 11 16 2 7 12 3 8 4  Approach:...