-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet Maze paths with jump using recursion in java
More file actions
54 lines (45 loc) · 1.52 KB
/
Get Maze paths with jump using recursion in java
File metadata and controls
54 lines (45 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int m = Integer.parseInt(br.readLine());
ArrayList<String> Paths = getMazePaths(1,1,n,m);
System.out.println(Paths);
}
// sr - source row
// sc - source column
// dr - destination row
// dc - destination column
public static ArrayList<String> getMazePaths(int sr, int sc, int dr, int dc) {
if (sr==dr && sc ==dc ){
ArrayList<String> brec = new ArrayList<>();
brec.add("");
return brec;
}
ArrayList<String> paths = new ArrayList<>();
//horizontal moves
for (int ms=1;ms<=dc-sc;ms++){
ArrayList<String > hpaths = getMazePaths(sr,sc+ms,dr,dc);
for ( String hpath:hpaths){
paths.add("h"+ms + hpath);
}
}
// vertical paths
for (int ms=1;ms<=dr-sr;ms++){
ArrayList<String > vpaths = getMazePaths(sr+ms,sc,dr,dc);
for ( String vpath:vpaths){
paths.add("v"+ms + vpath);
}
}
//diagonals
for (int ms=1;ms<=dr-sr && ms<=dc-sc;ms++){
ArrayList<String > dpaths = getMazePaths(sr+ms,sc+ms,dr,dc);
for ( String dpath:dpaths){
paths.add("d"+ms + dpath);
}
}
return paths;
}
}