-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.java
More file actions
116 lines (111 loc) · 3.45 KB
/
Solver.java
File metadata and controls
116 lines (111 loc) · 3.45 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
import java.util.Comparator;
import java.util.LinkedList;
import edu.princeton.cs.algs4.MinPQ;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.In;
public class Solver {
private final int totalMoves;
private final Node sol;
private final boolean canBeSolved;
private class Node implements Comparable<Node> {
public final Board board;
public final Node prev;
public final int moves;
private final int man;
private final Node root;
public Node(Board b) {
board = b;
prev = null;
moves = 0;
man = board.manhattan();
this.root = null;
}
public Node(Board b, Node p) {
board = b;
prev = p;
moves = prev.moves + 1;
man = board.manhattan();
if (p.root == null) {
this.root = p;
} else {
this.root = p.root;
}
}
public Node getRoot() {
return this.root;
}
public int compareTo(Node that) {
int sum1 = this.man + this.moves;
int sum2 = that.man + that.moves;
if (sum1 == sum2) {
return this.man - that.man;
} else {
return sum1 - sum2;
}
}
};
public Solver(Board initial) {
if (initial == null) throw new IllegalArgumentException();
Node prime = new Node(initial);
Node twin = new Node(prime.board.twin());
Node node = prime;
MinPQ<Node> pq = new MinPQ<Node>();
pq.insert(prime);
pq.insert(twin);
while (!node.board.isGoal()) {
node = pq.delMin();
for (Board i : node.board.neighbors()) {
if (node.prev == null || !node.prev.board.equals(i)) pq.insert(new Node(i, node));
}
}
if (node.getRoot() == prime || prime.board.isGoal()) {
canBeSolved = true;
sol = node;
totalMoves = sol.moves;
} else {
canBeSolved = false;
sol = null;
totalMoves = -1;
}
}
public boolean isSolvable() {
return canBeSolved;
}
public int moves() {
return totalMoves;
}
public Iterable<Board> solution() {
if (canBeSolved) {
LinkedList<Board> ret = new LinkedList<Board>();
Node a = sol;
while (a != null) {
// addFirst effectively reverses order. So order: topmost Node ... to ... sol (at the end)
ret.addFirst(a.board);
a = a.prev;
}
return ret;
} else {
return null;
}
}
public static void main(String[] args) {
// create initial board from file
In in = new In(args[0]);
int n = in.readInt();
int[][] blocks = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
blocks[i][j] = in.readInt();
Board initial = new Board(blocks);
// solve the puzzle
Solver solver = new Solver(initial);
// print solution to standard output
if (!solver.isSolvable())
StdOut.println("No solution possible");
else {
StdOut.println("Minimum number of moves = " + solver.moves());
for (Board board : solver.solution())
StdOut.println(board);
}
}
}