-
Notifications
You must be signed in to change notification settings - Fork 0
/
BreadthFirstSearch.java
106 lines (97 loc) · 2.58 KB
/
BreadthFirstSearch.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
import java.util.ArrayList;
public class BreadthFirstSearch extends MazeSearch {
Cell path_cell;
private ArrayList<Cell> open_list = new ArrayList<Cell>();
private ArrayList<Cell> closed_list = new ArrayList<Cell>();
private boolean end_found = false, solution_drawn = false;
public BreadthFirstSearch(int[][] generated_maze) {
super(generated_maze);
prepareSearch();
}
@Override
public boolean isDone() {
if (solution_drawn)
return true;
return false;
}
@Override
protected void prepareSearch() {
open_list.add(start_cell);
start_cell.parent = start_cell;
start_cell.setCost(0);
}
@Override
protected void expandSearch() {
if (end_found) { // Backtrack
if (path_cell.y == end_cell.y && path_cell.x == end_cell.x)
path_cell = super.backtrackPath(path_cell);
path_cell = super.backtrackPath(path_cell);
if (maze[path_cell.y][path_cell.x] == START) solution_drawn = true;
steps--;
}
else if (open_list.size() == 0) { // End node not reachable
distance = -1;
maze[start_cell.y][start_cell.x] = END;
solution_drawn = true;
}
else { // Expand
Cell open_cell = open_list.remove(0);
computeNeighbors(open_cell);
closeCell(open_cell);
open_cell.setCost(open_cell.parent.getCost()+1);
distance = open_cell.getCost();
}
}
private void computeNeighbors(Cell c) {
int y = c.y, x = c.x;
Cell neighbor;
if (y > 1 && (maze[y-1][x] == PASSAGE || maze[y-1][x] == END)) {
neighbor = new Cell(y-1,x);
neighbor.parent = c;
open_list.add(neighbor);
if (maze[y-1][x] == END) {
end_found = true;
path_cell = neighbor;
}
else
maze[y-1][x] = OPEN;
}
if (x > 1 && (maze[y][x-1] == PASSAGE || maze[y][x-1] == END)) {
neighbor = new Cell(y,x-1);
neighbor.parent = c;
open_list.add(neighbor);
if (maze[y][x-1] == END) {
end_found = true;
path_cell = neighbor;
}
else
maze[y][x-1] = OPEN;
}
if (y < maze.length-2 && (maze[y+1][x] == PASSAGE || maze[y+1][x] == END)) {
neighbor = new Cell(y+1,x);
neighbor.parent = c;
open_list.add(neighbor);
if (maze[y+1][x] == END) {
end_found = true;
path_cell = neighbor;
}
else
maze[y+1][x] = OPEN;
}
if (x < maze[0].length-2 && (maze[y][x+1] == PASSAGE || maze[y][x+1] == END)) {
neighbor = new Cell(y,x+1);
neighbor.parent = c;
open_list.add(neighbor);
if (maze[y][x+1] == END) {
end_found = true;
path_cell = neighbor;
}
else
maze[y][x+1] = OPEN;
}
}
private void closeCell(Cell c) {
closed_list.add(c);
if (maze[c.y][c.x] != START) maze[c.y][c.x] = CLOSED;
}
}