forked from DPrinceKumar/HacktoberFest2020-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
8 Puzzle.java
253 lines (208 loc) · 6.71 KB
/
8 Puzzle.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
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.Stack;
import java.util.Arrays;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jannik.Richter
*/
public class Board {
private class Coord {
public int y;
public int x;
public Coord(int y, int x){
this.y = y;
this.x = x;
}
}
private final int dim;
private final int[][] blocks;
/**
* @description construct a board from an n-by-n array of blocks
* (where blocks[i][j] = block in row i, column j)
* @param blocks
*/
public Board(int[][] blocks) {
if(blocks == null) {
throw new IllegalArgumentException();
}
// initialize Board
this.dim = blocks[0].length;
this.blocks = blocks.clone();
getBlank();
}
private Coord getBlank(){
for(int i = 0; i < dim; i ++){
for(int j = 0; j < dim; j++){
if(blocks[i][j]==0){
return new Coord(i,j);
}
}
}
// if no blank space, basically not possible
return null;
}
/**
* @description board dimension n
* @return
*/
public int dimension() {
return dim;
}
/**
* @description number of blocks out of place
* Quadratic array accesses
* @return
*/
public int hamming() {
int count = 0; // number of blocks out of place
int index = 1; // position correct number allocation
for(int i = 0; i < dim; i++) {
for(int j = 0; j < dim; j++) {
if(blocks[i][j] != index && blocks[i][j] != 0) { // do not compute if blank space
count++;
}
index++;
}
}
return count;
}
/**
* @description sum of Manhattan distances between blocks and goal
* Quadratic array accesses
* @return
*/
public int manhattan() {
int count = 0; // number of wrong positions
for(int i = 0; i < dim; i++) {
for(int j = 0; j < dim; j++) {
if(blocks[i][j] != 0) { // if not empty
int adjusted = blocks[i][j] - 1; // adjust to allow for additional 1 to be in [0][0]
int x = adjusted % dim; // correct x position
int y = (int) Math.floor(adjusted / dim); // correct y position
count += Math.abs(y - i) + Math.abs(x - j);
}
}
}
return count;
}
/**
* @description is this board the goal board?
* Worst case quadratic array accesses 0(n^2)
* @return
*/
public boolean isGoal() {
for(int i = 0; i < dim; i++) {
for(int j = 0; j < dim; j++) {
if(blocks[i][j] != 0) { // if not empty
int loc = (i * dim) + j + 1; // correct location
if(blocks[i][j] != loc) {
return false;
}
}
}
}
return true;
}
/**
* @description a board that is obtained by exchanging any pair of blocks
* @return
*/
public Board twin() {
Board twin = new Board(blocks);
Coord blank = getBlank();
for(int y = 0; y < dim; y++) {
for(int x = 0; x < dim - 1; x++) {
if((blank.y != y || blank.x != x) && (blank.y != y || blank.x != x + 1)) {
return new Board(exch(y, x, y, x + 1 ));
}
}
}
return twin;
}
private int[][] exch(int row, int col, int row1, int col1) {
int[][] cpy = copy(blocks);
int tmp = cpy[row][col];
cpy[row][col] = cpy[row1][col1];
cpy[row1][col1] = tmp;
return cpy;
}
/**
* @description does this board equal y?
* @param other
* @return
*/
@Override
public boolean equals(Object other) {
if(other == this) return true;
if(other == null) return false;
if(other.getClass() != this.getClass()) return false;
Board that = (Board) other;
if(that.blocks.length != this.blocks.length) return false;
if(that.blocks[0].length != this.blocks[0].length) return false;
for(int i = 0; i < this.blocks.length; i++) {
for(int j = 0; j < this.blocks[0].length; j++){
if(this.blocks[i][j] != that.blocks[i][j]) {
return false;
}
}
}
return true;
}
private int[][] copy(int[][] a){
int[][] b = new int[dim][dim];
for(int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
b[i][j] = a[i][j];
}
}
return b;
}
/**
* @description all neighboring boards
* @return
*/
public Iterable<Board> neighbors() {
Stack<Board> boards = new Stack<>();
Coord blank = getBlank();
if(blank.x > 0) {
boards.push(new Board(exch(blank.y, blank.x, blank.y, blank.x - 1)));
}
if(blank.x < dim - 1) {
boards.push(new Board(exch(blank.y, blank.x, blank.y, blank.x + 1)));
}
if(blank.y > 0) {
boards.push(new Board(exch(blank.y, blank.x, blank.y - 1, blank.x)));
}
if(blank.y < dim - 1) {
boards.push(new Board(exch(blank.y, blank.x, blank.y + 1, blank.x)));
}
return boards;
}
/**
* @description string representation of this board (in the output format specified below)
* @return
*/
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append(dim).append("\n");
for (int i = 0; i < dim; i++){
for(int j = 0; j < dim; j++){
s.append(String.format("%2d ", blocks[i][j]));
}
s.append("\n");
}
return s.toString();
}
/**
* @description unit tests
* @param args
*/
public static void main(String[] args) {
}
}