-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.java
More file actions
149 lines (126 loc) · 3.25 KB
/
Solver.java
File metadata and controls
149 lines (126 loc) · 3.25 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
145
146
147
148
149
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.MinPQ;
import edu.princeton.cs.algs4.StdOut;
import java.util.ArrayList;
import java.util.Stack;
public class Solver {
private SearchNode goal;
private class SearchNode implements Comparable<SearchNode> {
public int step = 0;
public SearchNode prev = null;
public Board board;
public boolean isTwin;
public int manhattan = -1;
public SearchNode(Board b, boolean isTwin) {
board = b;
this.isTwin = isTwin;
if (manhattan == -1) {
manhattan = board.manhattan();
}
}
public int priority() {
return manhattan + step;
}
public Iterable<SearchNode> neighbors() {
ArrayList<SearchNode> searchNodeArrayList = new ArrayList<>();
for (Board board : board.neighbors()) {
SearchNode searchNode = new SearchNode(board, isTwin);
searchNode.step = this.step+1;
searchNode.prev = this;
searchNodeArrayList.add(searchNode);
}
return searchNodeArrayList;
}
@Override
public int compareTo(SearchNode o) {
if (priority() == o.priority()) {
return this.manhattan - o.manhattan;
} else {
return priority() - o.priority();
}
}
}
// find a solution to the initial board (using the A* algorithm)
public Solver(Board initial) {
if (initial == null) {
throw new java.lang.IllegalArgumentException();
}
MinPQ<SearchNode> pq = new MinPQ<>();
pq.insert(new SearchNode(initial, false));
pq.insert(new SearchNode(initial.twin(), true));
while (true) {
SearchNode min = pq.delMin();
if (min.board.isGoal()) {
goal = min;
break;
}
for (SearchNode neighbour : min.neighbors()) {
if (!IsExistPrevious(neighbour, min)) {
pq.insert(neighbour);
}
}
if (pq.isEmpty()) {
break;
}
}
}
private boolean IsExistPrevious(SearchNode n, SearchNode parent) {
SearchNode p = parent;
if (parent.prev == null || !n.board.equals(parent.prev.board)) {
return false;
}
return true;
}
// is the initial board solvable?
public boolean isSolvable() {
if (goal.isTwin == true) {
return false;
}
return true;
}
// min number of moves to solve initial board; -1 if unsolvable
public int moves() {
if (!isSolvable()) {
return -1;
}
return goal.step;
}
// sequence of boards in a shortest solution; null if unsolvable
public Iterable<Board> solution() {
if (!isSolvable()) {
return null;
}
Stack<SearchNode> s = new Stack<>();
ArrayList<Board> arr = new ArrayList<>();
SearchNode node = goal;
while (node != null) {
s.push(node);
node = node.prev;
}
while (!s.empty()) {
arr.add(s.pop().board);
}
return arr;
}
// solve a slider puzzle (given below)
public static void main(String[] args) {
// create initial board from file
In in = new In("a.txt");
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);
}
}
}