-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.java
More file actions
79 lines (75 loc) · 1.85 KB
/
Solver.java
File metadata and controls
79 lines (75 loc) · 1.85 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
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
package puzzleSolver;
/**
*
*File:
* $Id: Solver.java,v 1.6 2013/05/03 13:52:11 cas5420 Exp $
*
*Revisions
* $Log: Solver.java,v $
* Revision 1.6 2013/05/03 13:52:11 cas5420
* Completed chess solver, debugging
*
* Revision 1.5 2013/05/01 11:46:16 cas5420
* Updated solver to generics
*
* Revision 1.4 2013/04/27 13:01:00 cas5420
* Started project
*
* Revision 1.2 2013/04/14 20:50:52 mgp9795
* Part 2 Adjustments
*
* Revision 1.1 2013/04/01 19:41:43 mgp9795
* Initial Version
*
*/
/**
* @author Michael
* @author Chris Sleys
*/
import java.util.ArrayList;
import java.util.HashSet;
public class Solver<E> {
/**
* Solves a puzzle
*
* @param puzzle
* - the puzzle to be solved
* @return ArrayList holding solution
*/
public ArrayList<E> solve(Puzzle<E> puzzle) {
HashSet<E> visited = new HashSet<E>();
ArrayList<ArrayList<E>> queue = new ArrayList<ArrayList<E>>();
ArrayList<E> startConfig = new ArrayList<E>();
ArrayList<E> current = null;
startConfig.add(puzzle.getStart());
boolean found = puzzle.isGoal(puzzle.getStart());
queue.add(startConfig);
visited.add(puzzle.getStart());
while (!queue.isEmpty() && !found) {
current = queue.remove(0);
ArrayList<E> neighbors = puzzle.getNeighbors(current.get(current.size() - 1));
for (E neighbor : neighbors) {
if (!visited.contains(neighbor)) {
ArrayList<E> nextConfig = new ArrayList<E>();
for (E item : current) {
nextConfig.add(item);
}
nextConfig.add(neighbor);
if (puzzle.isGoal(nextConfig.get(nextConfig.size() - 1))) {
current = nextConfig;
found = true;
break;
} else {
queue.add(nextConfig);
}
visited.add(neighbor);
}
}
}
if (found) {
return current;
} else {
return null;
}
}
}