-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.java
More file actions
53 lines (49 loc) · 1.58 KB
/
maze.java
File metadata and controls
53 lines (49 loc) · 1.58 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
public class maze {
public static void main(String[] args) {
boolean[][] m = {
{ true, true, true, true, true },
{ true, false, true, false, true },
{ true, true, true, false, true },
{ false, false, true, true, true },
{ true, true, true, false, true }
};
System.out.println ("all possible value ------" + maze(" " , m , 0 , 0 ));
}
public static int maze(String up , boolean[][]m , int r , int c) {
if (r == m.length - 1 && c == m[0].length - 1) {
System.out.println (up);
return 1;
}
int l = 0;
// this one is for down only
if (r < m.length -1 && m[r][c] ) {
m[r][c] = false;
l = maze (up + "D", m, r + 1, c);
m[r][c] = true;
}
//this one is for right only
int k = 0;
if (c < m[0].length - 1 && m[r][c] ) {
m[r][c] = false;
k = maze (up + "R", m, r, c + 1);
m[r][c] = true;
}
//this one is for left only
int j = 0;
if( c- 1 >=0 && m[r][c] ){
m[r][c] = false;
j = maze (up + "L", m , r , c - 1);
m[r][c] = true;
}
int p = 0 ;
if( r -1 >= 0 && m[r][c] ){
m[r][c] = false;
p = maze (up + "U" , m , r - 1 , c );
m[r][c] = true;
}
// if (r < m.length - 1 && c < m[0].length - 1 &&m[r][c] ) {
// j = maze (up + "S", m, r + 1, c + 1);
// }
return l + k + j + p;
}
}