forked from reking/algs-8puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolver.java
121 lines (103 loc) · 3.93 KB
/
Solver.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.util.Comparator;
public class Solver {
private SearchNode goal;
private class SearchNode {
private int moves;
private Board board;
private SearchNode preNode;
public SearchNode(Board b) {
moves = 0;
preNode = null;
board = b;
}
}
// find a solution to the initial board (using the A* algorithm)
public Solver(Board initial) {
/* main search node */
PriorityOrder po = new PriorityOrder();
MinPQ<SearchNode> pq = new MinPQ<SearchNode>(po);
SearchNode sn = new SearchNode(initial);
/* twin search node */
PriorityOrder twinPo = new PriorityOrder();
MinPQ<SearchNode> twinPq = new MinPQ<SearchNode>(twinPo);
SearchNode twinSn = new SearchNode(initial.twin());
pq.insert(sn);
twinPq.insert(twinSn);
/* delete the minimum priority node from queue until goal is reached */
SearchNode minNode = pq.delMin();
SearchNode twinMinNode = twinPq.delMin();
while (!minNode.board.isGoal() && !twinMinNode.board.isGoal()) {
for (Board b : minNode.board.neighbors()) {
if ((minNode.preNode == null)
|| !b.equals(minNode.preNode.board)) {
SearchNode node = new SearchNode(b);
node.moves = minNode.moves + 1;
node.preNode = minNode;
pq.insert(node);
}
}
for (Board b : twinMinNode.board.neighbors()) {
if ((minNode.preNode == null)
|| !b.equals(twinMinNode.preNode.board)) {
SearchNode node = new SearchNode(b);
node.moves = twinMinNode.moves + 1;
node.preNode = twinMinNode;
twinPq.insert(node);
}
}
minNode = pq.delMin();
twinMinNode = twinPq.delMin();
}
if (minNode.board.isGoal())
goal = minNode;
else
goal = null;
}
private class PriorityOrder implements Comparator<SearchNode> {
public int compare(SearchNode s1, SearchNode s2) {
int priority1 = s1.board.manhattan() + s1.moves;
int priority2 = s2.board.manhattan() + s2.moves;
if (priority1 > priority2) return 1;
else if (priority1 < priority2) return -1;
else return 0;
}
}
// is the initial board solvable?
public boolean isSolvable() {
return goal != null;
}
// min number of moves to solve initial board; -1 if no solution
public int moves() {
if (goal == null) return -1;
return goal.moves;
}
// sequence of boards in a shortest solution; null if no solution
public Iterable<Board> solution() {
if (!isSolvable()) return null;
Stack<Board> bStack = new Stack<Board>();
for (SearchNode s = goal; s != null; s = s.preNode)
bStack.push(s.board);
return bStack;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// 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);
}
}
}