-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.java
executable file
·158 lines (137 loc) · 4.47 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
import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.StdOut;
public class Board {
private final int[][] tiles;
private final int dim;
private int man = -1;
private int ham = -1;
public Board(int[][] tiles) {
dim = tiles.length;
this.tiles = new int[dim][dim];
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
this.tiles[i][j] = tiles[i][j];
}
}
}
public String toString() {
StringBuilder str = new StringBuilder();
str.append(dim + "\n");
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
str.append(" ").append(tiles[i][j]);
}
str.append("\n");
}
return str.toString();
}
public int dimension() {
return dim;
}
public int hamming() {
if (ham < 0) {
ham = 0;
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
if (tiles[i][j] == 0) continue;
if (tiles[i][j] != i * dim + j + 1) ham++;
}
}
}
return ham;
}
public int manhattan() {
if (man < 0) {
man = 0;
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
if (tiles[i][j] == 0) continue;
int ri = (tiles[i][j] - 1) / dim;
int rj = tiles[i][j] - ri * dim - 1;
man += Math.abs(i - ri) + Math.abs(j - rj);
}
}
}
return man;
}
public boolean isGoal() {
return manhattan() == 0;
}
public boolean equals(Object y) {
if (this == y) return true;
if (y == null) return false;
if (y.getClass() != this.getClass()) return false;
Board that = (Board) y;
if (that.dimension() != dimension()) return false;
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
if (that.tiles[i][j] != tiles[i][j]) return false;
}
}
return true;
}
public Iterable<Board> neighbors() {
Stack<Board> n = new Stack<>();
int bi = -1, bj = -1;
int[][] copy = new int[dim][dim];
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
copy[i][j] = tiles[i][j];
if (tiles[i][j] == 0) {
bi = i;
bj = j;
}
}
}
if (bi - 1 >= 0) fakeExch(copy, n, bi, bj, -1, 0);
if (bj + 1 < dim) fakeExch(copy, n, bi, bj, 0, 1);
if (bi + 1 < dim) fakeExch(copy, n, bi, bj, 1, 0);
if (bj - 1 >= 0) fakeExch(copy, n, bi, bj, 0, -1);
return n;
}
private void fakeExch(int[][] ts, Stack<Board> stack, int bi, int bj, int roffset,
int coffset) {
int tile = ts[bi + roffset][bj + coffset];
ts[bi][bj] = tile;
ts[bi + roffset][bj + coffset] = 0;
stack.push(new Board(ts));
ts[bi][bj] = 0;
ts[bi + roffset][bj + coffset] = tile;
}
public Board twin() {
int bi = -1;
int bj = -1;
int[][] copy = new int[dim][dim];
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
if (tiles[i][j] == 0) {
bi = i;
bj = j;
}
copy[i][j] = tiles[i][j];
}
}
int i1 = 0, i2 = 1, j1 = 0, j2 = 1;
if (i1 == bi && j1 == bj) i1++;
if (i2 == bi && j2 == bj) i2--;
int swap = copy[i1][j1];
copy[i1][j1] = copy[i2][j2];
copy[i2][j2] = swap;
return new Board(copy);
}
public static void main(String[] args) {
int[][] stiles = { { 1, 2, 3 }, { 0, 7, 6 }, { 5, 4, 8 } };
int[][] tiles = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
Board b = new Board(stiles);
Board c = new Board(tiles);
StdOut.println(b);
StdOut.println("Dimension: " + b.dimension());
StdOut.println("Hamming: " + b.hamming());
StdOut.println("Manhattan: " + b.manhattan());
StdOut.println("Is goal: " + b.isGoal());
StdOut.println("b equals to c: " + b.equals(c));
for (Board board : b.neighbors())
StdOut.println(board);
StdOut.println("Twin:\n" + b.twin());
}
}