-
Notifications
You must be signed in to change notification settings - Fork 1
/
Solver.ts
147 lines (120 loc) · 3.1 KB
/
Solver.ts
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
import MinPQ from './MinPQ';
import type Board from './Board';
type SearchNode = {
board: Board;
moves: number;
previous: SearchNode | null;
};
let comparator = process.env.MANHATTAN
? (a: SearchNode, b: SearchNode) => {
const order =
b.board.manhattan() + b.moves - (a.board.manhattan() + a.moves);
return order === 0
? b.board.hamming() + b.moves - (a.board.hamming() - a.moves)
: order;
}
: (a: SearchNode, b: SearchNode) => {
return b.board.hamming() + b.moves - (a.board.hamming() - a.moves);
};
export default class Solver {
_initial: Board;
_moves: number | undefined;
_queue: MinPQ<SearchNode>;
_solution: SearchNode | null | undefined;
constructor(initial: Board) {
this._initial = initial;
this._queue = new MinPQ(comparator);
this._queue.insert({
board: this._initial,
moves: 0,
previous: null,
});
}
isSolvable() {
return this.moves() !== -1;
}
moves() {
if (this._moves === undefined) {
const solution = this.solution();
if (solution) {
this._moves = [...solution].length - 1;
} else {
this._moves = -1;
}
}
return this._moves;
}
solution(): Iterable<Board> | null {
let next: SearchNode | null = null;
if (this._solution === undefined) {
// Detect unsolvable by checking parity.
// See: https://stackoverflow.com/a/34570524/2103996
// See: https://youtu.be/YI1WqYKHi78
// Count inversions.
const dimension = this._initial.dimension();
const tiles = this._initial._tiles;
let inversions = 0;
for (let i = 0; i < dimension ** 2; i++) {
for (let j = i + 1; j < dimension ** 2; j++) {
const aRow = Math.floor(i / dimension);
const aColumn = i % dimension;
const bRow = Math.floor(j / dimension);
const bColumn = j % dimension;
const a = tiles[aRow][aColumn];
const b = tiles[bRow][bColumn];
if (a > b && b !== 0) {
inversions++;
}
}
}
const [row] = this._initial.empty();
const solvable =
dimension % 2 === 0 // Even-sized grid?
? row % 2 !== 0 // Blank is on odd row (from bottom)?
? inversions % 2 === 0
: inversions % 2 !== 0 // Blank is on even row (from bottom).
: inversions % 2 === 0; // Odd-sized grid.
if (!solvable) {
this._solution = null;
} else {
while (true) {
next = this._queue.extract() || null;
if (!next || next.board.isGoal()) {
break;
}
for (const neighbor of next.board.neighbors()) {
// The "critical optimization"; don't revisit current node's
// predecessor.
if (!neighbor.equals(next.previous?.board)) {
this._queue.insert({
board: neighbor,
moves: next.moves + 1,
previous: next,
});
}
}
}
this._solution = next;
}
}
let move: SearchNode | null = this._solution;
if (move) {
// Reverse order.
const moves: Array<Board> = [];
while (move) {
moves.unshift(move.board);
move = move.previous;
}
moves.reverse();
return {
*[Symbol.iterator]() {
while (moves.length) {
yield moves.pop()!;
}
},
};
} else {
return null;
}
}
}