-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathSolver.java
115 lines (103 loc) · 3.01 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
package four;
import edu.princeton.cs.algs4.MinPQ;
import java.util.Comparator;
import java.util.Stack;
/**
* @author sunyue
* @version 1.0 2017/2/18 15:36
*/
public class Solver {
private int count = 0;
private MinPQ<Node> pq1;
private MinPQ<Node> pq2;
private Comparator<Node> manhattanPriority = new ManhattanPriority();
/**
* find a solution to the initial board (using the A* algorithm)
*
* @param initial
*/
public Solver(Board initial) {
pq1 = new MinPQ<>(manhattanPriority);
pq2 = new MinPQ<>(manhattanPriority);
pq1.insert(new Node(initial, count, null));
pq2.insert(new Node(initial.twin(), count, null));
Node cur1;
Node cur2;
while (!pq1.min().board.isGoal() && !pq2.min().board.isGoal()) {
cur1 = pq1.delMin();
cur2 = pq2.delMin();
for (Board board : cur1.board.neighbors()) {
if (null != cur1.prev && board.equals(cur1.prev.board)) continue;
pq1.insert(new Node(board, cur1.moves + 1, cur1));
}
for (Board board : cur2.board.neighbors()) {
if (null != cur2.prev && board.equals(cur2.prev.board)) continue;
pq2.insert(new Node(board, cur2.moves + 1, cur2));
}
}
if (!pq1.min().board.isGoal()) count = -1;
else count = pq1.min().moves;
}
/**
* solve a slider puzzle (given below)
*
* @param args
*/
public static void main(String[] args) {
}
/**
* is the initial board solvable?
*
* @return
*/
public boolean isSolvable() {
return count != -1;
}
/**
* min number of moves to solve initial board; -1 if unsolvable
*
* @return
*/
public int moves() {
return count;
}
/**
* sequence of boards in a shortest solution; null if unsolvable
*
* @return
*/
public Iterable<Board> solution() {
if (!isSolvable()) return null;
Stack<Board> stack = new Stack<>();
Node path = pq1.min();
while (path != null) {
stack.push(path.board);
path = path.prev;
}
return stack;
}
private class Node {
private Board board;
private int moves;
private Node prev;
public Node(Board board, int moves, Node prev) {
this.board = board;
this.moves = moves;
this.prev = prev;
}
}
private class ManhattanPriority implements Comparator<Node> {
@Override
public int compare(Node o1, Node o2) {
int num1 = o1.board.manhattan() + o1.moves;
int num2 = o2.board.manhattan() + o2.moves;
if (num1 == num2) {
if (o1.board.hamming() == o2.board.hamming()) return 0;
if (o1.board.hamming() < o2.board.hamming()) return -1;
return 1;
}
if (num1 < num2) return -1;
return 1;
}
}
}