Repository navigation
Expand file tree
/
Copy pathBoard.java
More file actions
237 lines (211 loc) · 7.95 KB
/
Copy pathBoard.java
File metadata and controls
237 lines (211 loc) · 7.95 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
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
package eightPuzzle;
import java.util.ArrayList;
import java.util.Iterator;
import edu.princeton.cs.algs4.StdRandom;
public class Board {
private final int dimension;
private final int[][] grids;
// construct a board from an n-by-n array of blocks
// (where blocks[i][j] = block in row i, column j)
public Board(int[][] blocks) {
dimension = blocks.length;
grids = new int[dimension][dimension];
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
grids[i][j] = blocks[i][j];
}
}
}
// board dimension n
public int dimension() {
return dimension;
}
// number of blocks out of place
public int hamming() {
int incorrectNum = 0;
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
if (grids[i][j] == 0) {
continue;
}
if (grids[i][j] != (i * dimension + j + 1)) {
incorrectNum++;
}
}
}
return incorrectNum;
}
// sum of Manhattan distances between blocks and goal
public int manhattan() {
int manhattan_dis = 0;
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
if (grids[i][j] == 0) {
continue;
}
if (grids[i][j] != (i * dimension + j + 1)) {
int x_cor = grids[i][j] % dimension == 0? grids[i][j] / dimension - 1 : grids[i][j] / dimension;
int y_cor = grids[i][j] - x_cor * dimension - 1;
int x_dis = x_cor - i > 0 ? x_cor - i : i - x_cor;
int y_dis = y_cor - j > 0 ? y_cor - j : j - y_cor;
manhattan_dis += x_dis + y_dis;
}
}
}
return manhattan_dis;
}
// is this board the goal board?
public boolean isGoal() {
return manhattan() == 0;
}
// a board that is obtained by exchanging any pair of blocks
public Board twin() {
Position positionOne = new Position(StdRandom.uniform(dimension), StdRandom.uniform(dimension));
Position positionTwo = new Position(StdRandom.uniform(dimension), StdRandom.uniform(dimension));
int[][] twin = new int[dimension][dimension];
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
twin[i][j] = grids[i][j];
}
}
while (true) {
if(twin[positionOne.x][positionOne.y] == 0 || positionOne.equals(positionTwo)){
positionOne.x = StdRandom.uniform(dimension);
positionOne.y = StdRandom.uniform(dimension);
}
if(twin[positionTwo.x][positionTwo.y] == 0 || positionOne.equals(positionTwo)){
positionTwo.x = StdRandom.uniform(dimension);
positionTwo.y = StdRandom.uniform(dimension);
}
if(!positionOne.equals(positionTwo)){
break;
}
}
int tempValue = twin[positionOne.x][positionOne.y];
twin[positionOne.x][positionOne.y] = twin[positionTwo.x][positionTwo.y];
twin[positionTwo.x][positionTwo.y] = tempValue;
return new Board(twin);
}
// does this board equal y?
public boolean equals(Object y) {
if(null == y) return false;
if (this == y)
return true;
if (y.getClass() == this.getClass()) {
if (this.dimension != ((Board) y).dimension())
return false;
String other = ((Board) y).toString();
String[] x_cor = other.split("\n");
if (x_cor.length -1 != dimension)
return false;
for (int i = 0; i < x_cor.length - 1; i++) {
String[] y_cor = x_cor[i + 1].trim().split(" ");
if (y_cor.length != dimension)
return false;
for (int j = 0; j < y_cor.length; j++) {
if (this.grids[i][j] != Integer.parseInt(y_cor[j])) {
return false;
}
}
}
}
return true;
}
// all neighboring boards
public Iterable<Board> neighbors() {
return new BoardIterator();
}
private class BoardIterator implements Iterable<Board> {
private ArrayList<Board> neighbors = new ArrayList<>();
BoardIterator() {
Position zeroPosition = null;
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
if (grids[i][j] == 0) {
zeroPosition = new Position(i, j);
break;
}
}
}
// 0上放的Board
if (zeroPosition.x > 0) {
neighbors.add(oneNeighbour(zeroPosition, new Position(zeroPosition.x - 1, zeroPosition.y)));
}
// 0右侧的Board
if (zeroPosition.y < dimension - 1) {
neighbors.add(oneNeighbour(zeroPosition, new Position(zeroPosition.x, zeroPosition.y + 1)));
}
// 0下方的Board
if (zeroPosition.x < dimension - 1) {
neighbors.add(oneNeighbour(zeroPosition, new Position(zeroPosition.x + 1, zeroPosition.y)));
}
// 0左方的Board
if (zeroPosition.y > 0) {
neighbors.add(oneNeighbour(zeroPosition, new Position(zeroPosition.x, zeroPosition.y - 1)));
}
}
@Override
public Iterator<Board> iterator() {
return neighbors.iterator();
}
private Board oneNeighbour(Position froPos, Position toPos) {
int[][] copyGrids = new int[dimension][dimension];
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
copyGrids[i][j] = grids[i][j];
}
}
int tempValue = copyGrids[froPos.x][froPos.y];
copyGrids[froPos.x][froPos.y] = copyGrids[toPos.x][toPos.y];
copyGrids[toPos.x][toPos.y] = tempValue;
return new Board(copyGrids);
}
}
// string representation of this board (in the output format specified below)
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(dimension).append("\n");
for (int i = 0; i < dimension; i++) {
sb.append(" ");
for (int j = 0; j < dimension; j++) {
sb.append(grids[i][j]).append(" ");
}
sb.append("\n");
}
return sb.toString();
}
// unit tests (not graded)
public static void main(String[] args) {
int[][] test = { { 0, 1, 3 }, { 4, 2, 5 }, { 7, 8, 6 } };
int[][] testeq = { { 0, 1, 3 }, { 4, 2, 5 }, { 7, 8, 6 } };
int[][] tess = {{0,2},{1,3}};
Board board = new Board(test);
Board boardd = new Board(testeq);
System.out.println(board.equals(boardd));
System.out.println(board);
System.out.println(board.twin());
/* System.out.println(board.dimension());
System.out.println(board.hamming());
System.out.println(board.manhattan());
System.out.println(board);
System.out.println(board.twin());
System.out.println(boardd);
System.out.println(boardd.twin());*/
}
private class Position {
private int x;
private int y;
Position(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (super.equals(obj))
return true;
if (obj.getClass().equals(Position.class))
return this.x == ((Position) obj).x && this.y == ((Position) obj).y;
return false;
}
}
}