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