-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.java
More file actions
126 lines (108 loc) · 2.87 KB
/
Copy pathSolver.java
File metadata and controls
126 lines (108 loc) · 2.87 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
//package coursera;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.MinPQ;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Solver {
private class Node {
public Board curr;
public Node prev;
public int priority;
public int move;
public Node(Board curr, Node prev, int move) {
this.curr = curr;
this.prev = prev;
this.priority = curr.manhattan() + move;
this.move = move;
}
}
private Node lastMove;
private boolean solved;
public Solver(Board initial) { // find a solution to the initial board (using the A* algorithm)
MinPQ<Node> priorityQ = new MinPQ<>(compareNode());
Node n = new Node(initial, null, 0);
priorityQ.insert(n);
Node n2 = new Node(initial.twin(), null, 0);
priorityQ.insert(n2);
while (true) {
Node node1 = priorityQ.delMin();
if (node1.curr.isGoal()) {
this.lastMove = node1;
this.solved = true;
break;
}
run(node1, priorityQ);
}
Node temp = lastMove;
while (temp.prev != null) {
temp = temp.prev;
}
if (temp.equals(n2)) {
this.solved = false;
this.lastMove = null;
}
}
private void run(Node node, MinPQ<Node> queue) {
Iterable<Board> arr = node.curr.neighbors();
int move = node.move;
for (Board b1 : arr) {
if (node.prev == null || !b1.equals(node.prev.curr)) {
Node newNode = new Node(b1, node, move + 1);
queue.insert(newNode);
}
}
}
private Comparator<Node> compareNode() {
return new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
if (o1.priority > o2.priority)
return 1;
else if (o1.priority < o2.priority)
return -1;
else
return 0;
}
};
}
public boolean isSolvable() { // is the initial board solvable?
return this.solved;
}
public int moves() { // min number of moves to solve initial board; -1 if unsolvable
if (this.solved)
return lastMove.move;
return -1;
}
public Iterable<Board> solution() { // sequence of boards in a shortest solution; null if unsolvable
if (!this.solved)
return null;
ArrayList<Board> result = new ArrayList<>();
Node temp = lastMove;
while (temp != null) {
result.add(temp.curr);
temp = temp.prev;
}
Collections.reverse(result);
return result;
}
public static void main(String[] args) { // solve a slider puzzle (given below)
int n = StdIn.readInt();
int[][] blocks = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
blocks[i][j] = StdIn.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);
}
}
}