-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.java
More file actions
144 lines (131 loc) · 5.66 KB
/
Solver.java
File metadata and controls
144 lines (131 loc) · 5.66 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/******************************************************************************
* Name: Nick Barnett
* NetID: nrbarnet
* Precept: P04
*
* Partner Name: N/A
* Partner NetID: N/A
* Partner Precept: N/A
*
* Description: Creates an immutable solver data type with the API below
******************************************************************************/
import edu.princeton.cs.algs4.MinPQ;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.Stopwatch;
public class Solver {
private SearchNode end;
private class SearchNode implements Comparable<SearchNode>
{
private Board board;
private int moves;
private int priority;
private SearchNode previous;
public SearchNode(Board board, int moves, SearchNode previous) {
this.board = board;
this.moves = moves;
//priority = board.manhattan() + moves; //hamming or manhattan
priority = board.hamming() + moves;
this.previous = previous;
}
public int compareTo(SearchNode that) {
if (this.priority > that.priority) return 1;
if (this.priority < that.priority) return -1;
return 0;
}
}
// find a solution to the initial board (using the A* algorithm)
public Solver(Board initial)
{
//check if initial board is null
if (initial == null) throw new NullPointerException();
//check if board is solvable
if (!initial.isSolvable()) throw new IllegalArgumentException();
//create min priority queue
MinPQ<SearchNode> pq = new MinPQ<SearchNode>();
//instantiate the root of the game tree
SearchNode root = new SearchNode(initial, 0, null);
pq.insert(root);
//remove the node with the smallest priority from the priority queue
//and processes it by adding its children to both the game tree and the
//priority queue. Repeat until the solution's board is equal to the
//game board
while (true) {
SearchNode sn = pq.delMin();
if (sn.board.isGoal()) {
end = sn;
//since the board has already been checked that it's
//solvable, this if statement guarantees a break from the
//while loop
break;
}
//iterate through each of the search node's neighbors
for (Board neighbor: sn.board.neighbors()) {
//check the base case and that the neighbor is not the same
//as the previous search node's board
if (sn.previous == null ||
!neighbor.equals(sn.previous.board)) {
SearchNode child =
new SearchNode(neighbor, sn.moves + 1, sn);
pq.insert(child);
}
}
}
}
// min number of moves to solve initial board
public int moves()
{
return end.moves;
}
// sequence of boards in a shortest solution
// Since each search node records the previous search node to get there,
// you can chase the pointers all the way back to the initial search node
// (and consider them in reverse order).
public Iterable<Board> solution()
{
Stack<Board> solution = new Stack<Board>();
//check if the board has a solution
if (end != null) {
//follow the pointers from the end of the game tree back to the
//start
SearchNode snTracer = end;
while (snTracer != null) {
solution.push(snTracer.board);
snTracer = snTracer.previous;
}
return solution;
}
else {
solution = null;
return solution;
}
}
// solve a slider puzzle (given below)
public static void main(String[] args)
{
// create initial board from file
In in = new In(args[0]);
int N = in.readInt();
int[][] tiles = new int[N][N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
tiles[i][j] = in.readInt();
Board initial = new Board(tiles);
//System.out.println(initial.inversions());
// check if puzzle is solvable; if so, solve it and output solution
Stopwatch stopwatch = new Stopwatch(); //Start timer
if (initial.isSolvable()) {
Solver solver = new Solver(initial);
double time = stopwatch.elapsedTime(); //stop timer
System.out.println("Time: " + time);
StdOut.println("Minimum number of moves = " + solver.moves());
for (Board board : solver.solution())
StdOut.println(board);
}
// if not, report unsolvable
else {
StdOut.println("Unsolvable puzzle");
}
}
}