-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
322 lines (284 loc) · 8.83 KB
/
Board.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import java.util.Arrays;
import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
public class Board {
private final int n;
private final int[] tiles;
private int manhattanCache = -1;
private int hammingCache = -1;
private int blankIndex = -1;
private int twinIndex1;
private int twinIndex2;
/**
* create a board from an n-by-n array of tiles, where tiles[row][col] = tile at
* (row, col)
*
* @param initial 2D array
*/
public Board(final int[][] initial) {
assert initial!=null;
n = initial[0].length;
assert n >= 2 && n < 128;
tiles = flatten2DArray(initial);
// find position of blank tile
for (int i = 0; i < tiles.length; i++)
if (tiles[i] == 0) {
blankIndex = i;
break;
}
// find twin indeces
twinIndex1 = StdRandom.uniform(0, n * n);
if (twinIndex1 == blankIndex)
twinIndex1 = StdRandom.uniform(0, n * n);
twinIndex2 = StdRandom.uniform(0, n * n);
if (twinIndex2 == blankIndex)
twinIndex2 = StdRandom.uniform(0, n * n);
if (twinIndex1 == twinIndex2)
twinIndex1 = StdRandom.uniform(0, n * n);
}
/**
* find 1D 0-based index from row,col
*
* @param row
* @param col
* @return
*/
private int findIndex(final int row, final int col) {
assert row > 0 && col > 0;
return (row - 1) * n + (col - 1);
}
/**
*
* @param index 0-based index
* @return Row number (1 based)
*/
private int getRow(final int index) {
return index / n + 1;
}
/**
*
* @param index 0-based index
* @return Column number (1 based)
*/
private int getCol(final int index) {
return index % n + 1;
}
/**
* convert a 2D array to 1D array
*
* @param initial
* @return
*/
private int[] flatten2DArray(final int[][] initial) {
final int n = initial[0].length;
final int[] array1D = new int[n * n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
array1D[n * i + j] = initial[i][j];
return array1D;
}
/**
* Build 2D array from 1D array
*
* @param arr 1D array
* @param rowSize Dimension of row/column
* @return 2D array
*/
private int[][] resolve2D(final int[] arr, final int rowSize) {
final int[][] array2D = new int[rowSize][rowSize];
for (int i = 0; i < rowSize; i++)
for (int j = 0; j < rowSize; j++)
array2D[i][j] = arr[rowSize * i + j];
return array2D;
}
/**
* Find adjacent indeces of the the blank tile
*
* @param index index of blank tile
* @return An integer array containing the adjacent indeces
*/
private int[] findAdjacentIndex(final int index) {
assert index >= 0 && index < n * n;
final int row = getRow(index);
final int col = getCol(index);
if (n == 1)
return new int[] {};
final int top = findIndex(row - 1, col);
final int bottom = findIndex(row + 1, col);
final int right = findIndex(row, col + 1);
final int left = findIndex(row, col - 1);
if (row == 1) { // top
if (col == 1)// top-left
return new int[] { right, bottom };
if (col == n)// top-right
return new int[] { left, bottom };
return new int[] { right, left, bottom };
}
if (row == n) { // bottom
if (col == 1)// bottom-left
return new int[] { top, right };
if (col == n)// bottom-right
return new int[] { top, left };
return new int[] { right, left, top };
}
if (col == 1)// left
return new int[] { right, top, bottom };
if (col == n)// right
return new int[] { left, top, bottom };
// Default (4)
return new int[] { right, left, top, bottom };
}
/**
* Swap elements of an array
*
* @param q Array
* @param i 1st index
* @param j 2nd index
*/
private void swap(final int[] q, final int i, final int j) {
final int temp = q[i];
q[i] = q[j];
q[j] = temp;
}
// string representation of this board
@Override
public String toString() {
final StringBuilder s = new StringBuilder();
s.append(n + "\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
s.append(String.format("%2d ", tiles[n * i + j]));
s.append("\n");
}
return s.toString();
}
/**
* find board dimension
*
* @return board dimension
*/
public int dimension() {
return n;
}
/**
* Computes number of tiles out of place
*
* @return Hamming distance of the board
*/
public int hamming() {
if (hammingCache != -1)
return hammingCache;
int hamming = 0;
if (!isGoal()) {
for (int i = 0; i < tiles.length; i++) {
if (tiles[i] == 0)
continue;
if (tiles[i] != i + 1)
hamming++;
}
}
hammingCache = hamming;
return hammingCache;
}
/**
* Computes sum of Manhattan distances between tiles and goal
*
* @return Sum of Manhatten distances
*/
public int manhattan() {
if (manhattanCache != -1)
return manhattanCache;
int mancost = 0;
for (int i = 0; i < tiles.length; ++i) {
int v = tiles[i];
if (v == 0)
continue;
v = v - 1; // index in goal array
final int cost = Math.abs(getRow(i) - getRow(v)) + Math.abs(getCol(i) - getCol(v));
mancost += cost;
}
manhattanCache = mancost;
return manhattanCache;
}
/**
* is this board the goal board?
*
* @return true if goal board
*/
public boolean isGoal() {
for (int i = 0; i < tiles.length; i++) {
if (tiles[i] == 0)
continue;
if (tiles[i] != i + 1)
return false;
}
return true;
}
// does this board equal y?
@Override
public boolean equals(final Object y) {
if (y == this)
return true;
if (y == null)
return false;
if (y.getClass() != this.getClass())
return false;
final Board that = (Board) y;
return this.dimension() == that.dimension() && this.hamming() == that.hamming()
&& this.manhattan() == that.manhattan();
}
/**
* Possible board combination after moving the tiles. Depending on the location
* of the blank square, a board can have 2, 3, or 4 neighbors.
*
* @return An iterable containing the neighbors of the board.
*/
public Iterable<Board> neighbors() {
final int[] adj = findAdjacentIndex(blankIndex);
final Stack<Board> neighbors = new Stack<>();
for (final int index : adj) {
final int[] copy = Arrays.copyOf(tiles, tiles.length);
swap(copy, blankIndex, index);
neighbors.push(new Board(resolve2D(copy, n)));
}
return neighbors;
}
/**
* A board that is obtained by exchanging any pair of tiles. Unique for a given
* board
*
* @return Twin board
*/
public Board twin() {
final int[] copy = Arrays.copyOf(tiles, tiles.length);
swap(copy, twinIndex1, twinIndex2);
return new Board(resolve2D(copy, n));
}
// unit testing (not graded)
public static void main(final String[] args) {
final int[][] stateArr = new int[3][3];
stateArr[0] = new int[] { 8, 1, 3 };
stateArr[1] = new int[] { 4, 0, 2 };
stateArr[2] = new int[] { 7, 6, 5 };
final Board state = new Board(stateArr);
stateArr[0] = new int[] { 1, 2, 3 };
Board other = new Board(stateArr);
StdOut.println(state.hamming());
StdOut.println(state.manhattan());
StdOut.print(state.toString());
StdOut.println(state.equals(other));
StdOut.println(state.isGoal());
stateArr[0] = new int[] { 1, 2, 3 };
stateArr[1] = new int[] { 4, 5, 6 };
stateArr[2] = new int[] { 7, 8, 0 };
other = new Board(stateArr);
StdOut.println(other.isGoal());
StdOut.println("NEIGHBORS");
for (final Board nBoard : state.neighbors())
StdOut.println(nBoard.toString());
StdOut.println("TWINS");
StdOut.println(state.twin().toString());
StdOut.println(state.twin().toString());
}
}