-
Notifications
You must be signed in to change notification settings - Fork 82
/
Maze.java
108 lines (98 loc) · 2.68 KB
/
Maze.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Created in the IntelliJ IDEA.
Copyright(c) 2020
Created by 0utplay | Aldin Sijamhodzic
Datum: 04.10.2020
Uhrzeit: 16:51
*/
public class Maze {
final int mazeSize, startX, startZ, endX, endZ;
static int[][] mazeLayout;
int[][] solution;
public Maze(int mazeSize, int startX, int startZ, int endX, int endZ) {
this.mazeSize = mazeSize;
this.startX = startX;
this.startZ = startZ;
this.endX = endX;
this.endZ = endZ;
}
public Maze(int mazeSize) {
this(mazeSize, 0, 0, mazeSize - 1, mazeSize - 1);
}
public Maze(int[][] mazeLayout) {
this(mazeLayout.length);
}
public void solve() {
solution = new int[mazeSize][mazeSize];
if (this.isNoValidPoint(this.startX, this.startZ)) {
System.out.println("No valid start found.");
return;
}
if (this.isNoValidPoint(this.endX, this.endZ)) {
System.out.println("No valid end found.");
return;
}
if (this.findNext(this.startX, this.startZ)) {
for (int i = 0; i < this.mazeSize; i++) {
for (int j = 0; j < this.mazeSize; j++) {
char c = 'A';
if (this.solution[i][j] == 1) {
c = '#';
}
System.out.print(" " + c + " ");
}
System.out.println();
}
} else {
System.out.println("No solution found.");
}
}
public boolean findNext(int x, int z) {
if (x == this.endX && z == this.endZ) {
this.solution[x][z] = 1;
return true;
}
if (this.isNoValidPoint(x, z)) {
return false;
}
solution[x][z] = 1;
if (this.findNext(x + 1, z)) {
return true;
}
if (this.findNext(x, z + 1)) {
return true;
}
solution[x][z] = 0;
return false;
}
public boolean isNoValidPoint(int x, int z) {
return x < 0 || z < 0
|| x >= this.mazeSize || z >= this.mazeSize
|| mazeLayout[x][z] != 1;
}
public static void main(String[] args) {
mazeLayout = new int[][]{
{1, 1, 1, 1},
{0, 1, 0, 1},
{1, 1, 0, 1},
{1, 0, 0, 1}
};
Maze maze = new Maze(mazeLayout);
maze.solve();
}
}
/*
Sample I/O
Change the mazeLayout (use 1 for possible paths)
Input:
{1, 1, 1, 1},
{0, 1, 0, 1},
{1, 1, 0, 1},
{1, 0, 0, 1}
Output:
# # # #
A A A #
A A A #
A A A #
Time complexity: O(2^(n^2))
Space complexity: O(n^2)
*/