-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.java
254 lines (227 loc) · 8.46 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
/******************************************************************************
* Name: Nick Barnett
* NetID: nrbarnet
* Precept: P04
*
* Partner Name: N/A
* Partner NetID: N/A
* Partner Precept: N/A
*
* Description: Creates an immutable board data type with the API below
******************************************************************************/
import edu.princeton.cs.algs4.Stack;
public class Board {
private final int N;
private int[][] tiles;
// construct a board from an N-by-N array of tiles
// (where tiles[i][j] = tile at row i, column j)
public Board(int[][] tiles)
{
this.N = tiles.length;
this.tiles = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
this.tiles[i][j] = tiles[i][j];
}
}
}
// return tile at row i, column j (or 0 if blank)
public int tileAt(int i, int j)
{
if ((i < 0 || i > N-1) || (j < 0 || j > N-1))
throw new IndexOutOfBoundsException();
return tiles[i][j];
}
// board size N
public int size()
{
return this.N;
}
// number of tiles out of place
public int hamming()
{
int count = 0;
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
if (tileAt(row, col) != 0 &&
tileAt(row, col) != gbTileAt(row, col)) count++;
}
}
return count;
}
// sum of Manhattan distances between tiles and goal
public int manhattan()
{
int count = 0;
for (int y = 0; y < N; y++) {
for (int x = 0; x < N; x++) {
int tile = tiles[y][x];
if (tileAt(y, x) != 0 && tileAt(y, x) != gbTileAt(y, x)) {
int gbX = (tile - 1) % N; // expected x-coordinate (row)
int gbY = (tile - 1) / N; // expected y-coordinate (col)
int dx = x - gbX; // distance to expected x-coordinate
int dy = y - gbY; // distance to expected y-coordinate
count += Math.abs(dx) + Math.abs(dy);
}
}
}
return count;
}
// is this board the goal board?
public boolean isGoal()
{
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
if (tileAt(row, col) != gbTileAt(row, col)) return false;
}
}
return true;
}
//returns the tile of the goal board at row i and col j
private int gbTileAt(int i, int j)
{
if (i == N-1 && j == N-1) return 0;
return i*N + j + 1;
}
// is this board solvable?
public boolean isSolvable()
{
//if the board size is odd and the # of inversions is odd, then the
//board is unsolvable
if (N % 2 != 0 && inversions() % 2 != 0) return false;
//find the row of the blankspace
int blankRow = -1;
boolean blankFound = false;
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
if (tiles[row][col] == 0) {
blankRow = row;
blankFound = true;
break;
}
}
if (blankFound) break;
}
//assert (blankFound == true && blankRow != -1);
//System.out.println("blankRow: " + blankRow);
//if the board size is even and the number of inversions is odd, then
//the board is unsolvable
if (N % 2 == 0 && ((blankRow + inversions()) % 2 == 0)) return false;
return true;
}
// Definition: For any other configuration besides the goal,
// whenever a tile with a greater number on it precedes a
// tile with a smaller number, the two tiles are said to be inverted
private int inversions()
{
//for me, it was easier to detect an inversion in a 1-D array than it
//was for a 2-D array
int[] array = new int[N*N];
int inversions = 0;
//copy the elements in the tiles array into the new 1-D array by
//row-major order
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
array[row*N+col] = tiles[row][col];
}
}
for (int i = 0; i < N*N; i++) {
for (int j = i+1; j < N*N; j++) {
//don't count the inversion if the jth element is the blank
//space
if (array[j] != 0 && array[j] < array[i]) {
//System.out.println(array[j] + " < " + array[i] + " but "
//+ array[i] + " comes before " + array[j]);
//System.out.println(array[i] + "-" + array[j]);
inversions++;
}
}
}
return inversions;
}
// does this board equal y?
public boolean equals(Object y)
{
if (y == this) return true; //optimize for true object equality
if (y == null) return false; //check for null
//objects must be in the same class
if (y.getClass() != this.getClass()) return false;
Board that = (Board) y; //cast is guaranteed to succeed
if (that.N != N) return false;
boolean tilesEqual = true;
//check that all significant fields are the same
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (this.tiles[i][j] != that.tiles[i][j]) tilesEqual = false;
}
}
return this.N == that.N && tilesEqual;
}
// all neighboring boards
public Iterable<Board> neighbors()
{
int blankRow = -1;
int blankCol = -1;
boolean blankFound = false;
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
if (tiles[row][col] == 0) {
blankRow = row;
blankCol = col;
blankFound = true;
break;
}
}
if (blankFound) break;
}
//assert (blankFound == true && (blankRow != -1 && blankRow != -1));
Stack<Board> boards = new Stack<Board>();
//moving the blankspace up won't result in a NullPointerException
if (blankRow != 0) {
Board board = new Board(tiles);
board.exch(blankRow, blankCol, blankRow - 1, blankCol);
boards.push(board);
}
//moving the blankspace down won't result in a NullPointerException
if (blankRow != N-1) {
Board board = new Board(tiles);
board.exch(blankRow, blankCol, blankRow + 1, blankCol);
boards.push(board);
}
//moving the blankspace right won't result in a NullPointerException
if (blankCol != N-1) {
Board board = new Board(tiles);
board.exch(blankRow, blankCol, blankRow, blankCol + 1);
boards.push(board);
}
//moving the blankspace left won't result in a NullPointerException
if (blankCol != 0) {
Board board = new Board(tiles);
board.exch(blankRow, blankCol, blankRow, blankCol - 1);
boards.push(board);
}
return boards;
}
//helper method for making moves happen on the board
private void exch(int i, int j, int a, int b) {
int temp = tiles[i][j];
tiles[i][j] = tiles[a][b];
tiles[a][b] = temp;
}
// string representation of this board (in the output format specified below)
public String toString()
{
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 ", tileAt(i, j)));
}
s.append("\n");
}
return s.toString();
}
// unit testing (not graded)
public static void main(String[] args)
{ }
}