-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
241 lines (194 loc) · 6.75 KB
/
Copy pathBoard.java
File metadata and controls
241 lines (194 loc) · 6.75 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 puzzle;
import edu.princeton.cs.algs4.Queue;
public class Board{
private int[][] blocks;
private int dim;
private int mDistance;
private int indexRow;//row of the empty block;
private int indexColumn;//column of the empty block;
public Board(int[][] blocks) // construct a board from an n-by-n array of blocks
// (where blocks[i][j] = block in row i, column j)
{
dim = blocks.length;
this.blocks = new int[dim][dim];
this.blocks = blocks;
this.mDistance = manhattan();
}
private Board(Board board)
{
this.dim = board.dim;
this.blocks = new int[dim][dim];
for (int i = 0; i < dim; i++)
{
for(int j = 0; j<dim; j++)
{
this.blocks[i][j] = board.blocks[i][j];
}
}
this.mDistance = board.mDistance;
this.indexRow = board.indexRow;
this.indexColumn = board.indexColumn;
//this.hDistance = hamming(this.blocks);
}
public int dimension() // board dimension n
{
return dim;
}
public int hamming() // number of blocks out of place
{
int sum = 0;
for (int i = 0; i <dim; i++)
{
for(int j = 0; j<dim; j++)
{
if( i*dim+j+1 != blocks[i][j])
{
sum++;
}
}
}
return sum-1;//Exclude the empty element 0;
}
public int manhattan() // sum of Manhattan distances between blocks and goal
{
if (this.mDistance!=0)
return this.mDistance;
int sum = 0;
for(int i = 0; i<dim; i++)
{
for(int j=0; j<dim; j++)
{
if(blocks[i][j]!=0)
{
int row = (blocks[i][j]-1)/dim;
int col = (blocks[i][j]-1)-row*dim;
sum += Math.abs(i-row)+ Math.abs(j-col);
}
else
{
this.indexRow = i;
this.indexColumn = j;
}
}
}
return sum;
}
public boolean isGoal() // is this board the goal board?
{
if(mDistance==0) return true;
else return false;
}
public Board twin() // a board that is obtained by exchanging any pair of blocks
{
Board temp = new Board(this);
if(indexRow!=0)
{
exch(temp.blocks, 0, 0, 0, 1);
temp.mDistance += mTwinDifference(this.blocks,0,0,0,1);
}
else
{
exch(temp.blocks, 1, 0, 1, 1);
temp.mDistance += mTwinDifference(this.blocks,1, 0, 1, 1);
}
return temp;
}
private int mTwinDifference(int[][] blocks, int i1,int j1,int i2,int j2) //Calculate the Manhattan Distance Difference when (i1,j1) and (i2,j2) are swapped
{
int md1;
int md2;
int diff;
int row1 = (blocks[i1][j1]-1)/dim;
int col1 = blocks[i1][j1]-row1*dim-1;
int row2 = (blocks[i2][j2]-1)/dim;
int col2 = blocks[i2][j2]-row2*dim-1;
md1 = Math.abs(j1-col1)+Math.abs(j2-col2);
md2 = Math.abs(j2-col1)+Math.abs(j1-col2);
diff = md2-md1;
return diff;
}
private void exch(int[][] blocks, int i1, int j1, int i2, int j2)
{
int temp;
temp = blocks[i1][j1];
blocks[i1][j1] = blocks[i2][j2];
blocks[i2][j2] = temp;
}
public boolean equals(Object y) // does this board equal y?
{
if(y==null||y.getClass()!=this.getClass()) return false;
Board that = (Board) y;
if (this.blocks.length!=that.blocks.length)
return false;
for(int i = 0; i<dim; i++)
{
for(int j = 0; j<dim; j++)
{
if(that.blocks[i][j]!=this.blocks[i][j])
return false;
}
}
return true;
}
public Iterable<Board> neighbors() // all neighboring boards
{
Queue<Board> queue = new Queue<Board>();
Board neighbor;
int row;
int col;
if(indexRow!=0)
{
neighbor = new Board(this);
exch(neighbor.blocks, indexRow,indexColumn, indexRow-1, indexColumn);
row = (neighbor.blocks[indexRow][indexColumn]-1)/dim;
neighbor.mDistance = this.mDistance + Math.abs(indexRow-row) - Math.abs(indexRow-1-row);
neighbor.indexRow -= 1;
queue.enqueue(neighbor);
}
if(indexColumn!=0)
{
neighbor = new Board(this);
exch(neighbor.blocks, indexRow,indexColumn, indexRow, indexColumn-1);
row = (neighbor.blocks[indexRow][indexColumn]-1)/dim;
col = neighbor.blocks[indexRow][indexColumn]-row*dim-1;
neighbor.mDistance = this.mDistance +Math.abs(indexColumn-col) - Math.abs(indexColumn-1-col);
neighbor.indexColumn -= 1;
queue.enqueue(neighbor);
}
if(indexRow!=dim-1)
{
neighbor = new Board(this);
exch(neighbor.blocks, indexRow,indexColumn, indexRow+1, indexColumn);
row = (neighbor.blocks[indexRow][indexColumn]-1)/dim;
neighbor.mDistance =this.mDistance + Math.abs(indexRow-row) - Math.abs(indexRow+1-row);
neighbor.indexRow += 1;
queue.enqueue(neighbor);
}
if(indexColumn!=dim-1)
{
neighbor = new Board(this);
exch(neighbor.blocks, indexRow,indexColumn, indexRow, indexColumn+1);
row = (neighbor.blocks[indexRow][indexColumn]-1)/dim;
col = neighbor.blocks[indexRow][indexColumn]-row*dim-1;
neighbor.mDistance = this.mDistance +Math.abs(indexColumn-col) - Math.abs(indexColumn+1-col);
neighbor.indexColumn += 1;
queue.enqueue(neighbor);
}
return queue;
}
public String toString() // string representation of this board (in the output format specified below)
{
String s;
s =String.format("%2d",dim) + "\n";
for (int i = 0; i < dim; i++)
{
for(int j = 0; j < dim; j++)
{
s = s +String.format("%2d ", blocks[i][j]);
if (j == dim-1)
s += "\n";
}
}
return s;
}
}