-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.java
More file actions
123 lines (111 loc) · 3.58 KB
/
Solver.java
File metadata and controls
123 lines (111 loc) · 3.58 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
/**
* Created by DmitryLabutin on 22.07.15.
*/
public class Solver {
private SearchNode solve = null;
private class SearchNode implements Comparable<SearchNode> {
private Board board;
private SearchNode prev;
private int movies;
private int priority = -1;
public SearchNode(Board b, int movies, SearchNode prev) {
this.board = b;
this.movies = movies;
this.prev = prev;
}
public int priority() {
if (priority == -1) {
priority = board.manhattan() + movies;
}
return priority;
}
@Override
public int compareTo(SearchNode that) {
int p1 = this.priority();
int p2 = that.priority();
if (p1 < p2) return -1;
if (p1 > p2) return +1;
return 0;
}
}
public Solver(Board initial) {
if (null == initial) {
throw new NullPointerException();
}
Board twin = initial.twin();
MinPQ<SearchNode> mqpInitial = new MinPQ<SearchNode>();
MinPQ<SearchNode> mqpTwin = new MinPQ<SearchNode>();
SearchNode snInitial = new SearchNode(initial, 0, null);
SearchNode snTwin = new SearchNode(twin, 0, null);
mqpInitial.insert(snInitial);
mqpTwin.insert(snTwin);
while (true) {
snInitial = mqpInitial.delMin();
if (snInitial.board.isGoal()) {
solve = snInitial;
break;
} else {
for (Board n: snInitial.board.neighbors()) {
if (snInitial.prev == null || !n.equals(snInitial.prev.board)) {
mqpInitial.insert(new SearchNode(
n,
snInitial.movies + 1,
snInitial));
}
}
}
snTwin = mqpTwin.delMin();
if (snTwin.board.isGoal()) {
solve = null;
break;
} else {
for (Board n: snTwin.board.neighbors()) {
if (snTwin.prev == null || !n.equals(snTwin.prev.board)) {
mqpTwin.insert(new SearchNode(n, snTwin.movies + 1, snTwin));
}
}
}
}
}
public boolean isSolvable() {
return solve != null;
}
public int moves() {
if (!this.isSolvable()) {
return -1;
}
return solve.movies;
}
public Iterable<Board> solution() {
if (!this.isSolvable()) {
return null;
}
Stack<Board> res = new Stack<Board>();
SearchNode s = solve;
while (s != null) {
res.push(s.board);
s = s.prev;
}
return res;
}
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);
}
}
}