-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreadthFirstSearcher.java
104 lines (84 loc) · 2.79 KB
/
BreadthFirstSearcher.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
import java.util.ArrayList;
import java.util.LinkedList;
/**
* Breadth-First Search (BFS)
*
* You should fill the search() method of this class.
*/
public class BreadthFirstSearcher extends Searcher {
/**
* Calls the parent class constructor.
*
* @see Searcher
* @param maze initial maze.
*/
public BreadthFirstSearcher(Maze maze) {
super(maze);
}
/**
* Main breadth first search algorithm.
*
* @return true if the search finds a solution, false otherwise.
*/
public boolean search() {
// FILL THIS METHOD
// explored list is a 2D Boolean array that indicates if a state associated with a given position in the maze has already been explored.
boolean[][] explored = new boolean[maze.getNoOfRows()][maze.getNoOfCols()];
// ...
// Queue implementing the Frontier list
LinkedList<State> queue = new LinkedList<State>();
//added the initial state of the player
State initState = new State(maze.getPlayerSquare(),null,0,0);
queue.add(initState);
boolean solnFound = false;
while (!queue.isEmpty()) {
// TODO return true if find a solution
// TODO maintain the cost, noOfNodesExpanded (a.k.a. noOfNodesExplored),
// maxDepthSearched, maxSizeOfFrontier during
// the search
// TODO update the maze if a solution found
//Removed node to be expanded
State curr = queue.pop();
explored[curr.getX()][curr.getY()] = true;
noOfNodesExpanded++; //increment # of nodes expanded
//if goal reached
//if(curr.getSquare() == maze.getGoalSquare()){
if(curr.isGoal(maze)){
//solnFound = true;
//noOfNodesExpanded++;
cost = curr.getGValue(); //get length of soln path
maxDepthSearched = curr.getDepth();
State currPath = curr;
while(currPath.getParent() != null){
currPath = currPath.getParent();
maze.setOneSquare(currPath.getSquare(), '.');
}
maze.setOneSquare(currPath.getSquare(), 'S');
return true;
}
maze = new Maze(maze.getMazeMatrix(), curr.getSquare(), maze.getGoalSquare());
//Get the successors
ArrayList<State> successors = curr.getSuccessors(explored, maze);
//for each successor returned, check if already present in the frontier queue, else add it at the end
for(int i = 0; i < successors.size(); i++){
for(State j: queue){
if (j.getX() == successors.get(i).getX() && j.getY() == successors.get(i).getY()) {
solnFound = true;
}
}
if (!solnFound) {
queue.add(successors.get(i));
}
solnFound = false;
}
//if frontier bigger after adding successors, update maxFrontierSize
if(queue.size() > maxSizeOfFrontier){
maxSizeOfFrontier = queue.size();
}
}
// use queue.pop() to pop the queue.
// use queue.add(...) to add elements to queue
return false;
// TODO return false if no solution
}
}