-
Notifications
You must be signed in to change notification settings - Fork 0
/
mazeSolver.java
74 lines (52 loc) · 1.09 KB
/
mazeSolver.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class work6 {
public static void main(String[] args) {
mazeSolver();
System.out.println(GCD(6, 8));
}
public static void mazeSolver(){
int[][] maze = {{0,0,0,0,1,0},
{1,1,0,1,1,1},
{0,1,0,0,0,1},
{1,1,0,1,1,0},
{1,0,0,0,0,2},
{1,1,1,1,1,0}};
int pos_x = 5;
int pos_y = 5;
if(solve(maze, pos_x, pos_y)){
System.out.println("Found!");
}
else{
System.out.println("Not found!");
}
}
public static boolean solve(int[][] maze, int pos_x, int pos_y){
if(pos_x < 0 || pos_x > 5 || pos_y < 0 || pos_y > 5
|| maze[pos_x][pos_y] == 1){
return false;
}
if(maze[pos_x][pos_y] == 2){
return true;
}
maze[pos_x][pos_y] = 1;
if(solve(maze, pos_x, pos_y + 1)){
return true;
}
if(solve(maze, pos_x, pos_y - 1)){
return true;
}
if(solve(maze, pos_x + 1, pos_y)){
return true;
}
if(solve(maze, pos_x - 1, pos_y)){
return true;
}
return false;
}
public static int GCD(int x ,int y)
{
if(y == 0)
return x;
else
return GCD(y, x % y);
}
}