-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
165 lines (148 loc) · 4.58 KB
/
Board.java
File metadata and controls
165 lines (148 loc) · 4.58 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
/**
* Created by DmitryLabutin on 22.07.15.
*/
public class Board {
private int[][] blocks;
private int dimension;
public Board(int[][] blocks) {
this.blocks = new int[blocks.length][blocks.length];
this.dimension = blocks.length;
for (int i = 0; i < this.dimension; ++i) {
for (int j = 0; j < this.dimension; ++j) {
this.blocks[i][j] = blocks[i][j];
}
}
}
public int dimension() {
return this.dimension;
}
public int hamming() {
int result = 0;
int position = 1;
for (int i = 0; i < this.dimension(); ++i) {
for (int j = 0; j < this.dimension(); ++j) {
if (0 != blocks[i][j] && blocks[i][j] != position) {
++result;
}
++position;
}
}
return result;
}
private int distance(int i, int j, int number) {
int row = (number - 1) / this.dimension();
int col = (number - 1) % this.dimension();
return Math.abs(i - row) + Math.abs(j - col);
}
public int manhattan() {
int result = 0;
for (int i = 0; i < this.dimension(); ++i) {
for (int j = 0; j < this.dimension(); ++j) {
if (0 != blocks[i][j]) {
result += this.distance(i, j, blocks[i][j]);
}
}
}
return result;
}
public boolean isGoal() {
if (blocks[this.dimension() - 1][this.dimension() - 1] != 0) {
return false;
}
int position = 1;
for (int i = 0; i < this.dimension(); ++i) {
for (int j = 0; j < this.dimension(); ++j) {
if (blocks[i][i] != 0 && blocks[i][j] != position) {
return false;
}
++position;
}
}
return true;
}
public Board twin() {
int row = 0;
if (blocks[0][0] == 0 || blocks[0][1] == 0) {
++row;
}
int tmp = blocks[row][0];
blocks[row][0] = blocks[row][1];
blocks[row][1] = tmp;
Board res = new Board(this.blocks);
tmp = blocks[row][0];
blocks[row][0] = blocks[row][1];
blocks[row][1] = tmp;
return res;
}
public boolean equals(Object y) {
if (y == null) return false;
if (y.getClass() != this.getClass()) return false;
Board c = (Board) y;
if (this.dimension() != c.dimension()) return false;
for (int i = 0; i < this.dimension(); ++i) {
for (int j = 0; j < this.dimension(); ++j) {
if (this.blocks[i][j] != c.blocks[i][j]) return false;
}
}
return true;
}
private int[] findZero() {
int[] res = new int[2];
for (int i = 0; i < this.dimension(); ++i) {
for (int j = 0; j < this.dimension(); ++j) {
if (blocks[i][j] == 0) {
res[0] = i;
res[1] = j;
return res;
}
}
}
return null;
}
private void swap(int i1, int j1, int i2, int j2) {
int tmp = blocks[i1][j1];
blocks[i1][j1] = blocks[i2][j2];
blocks[i2][j2] = tmp;
}
public Iterable<Board> neighbors() {
Queue<Board> it = new Queue<Board>();
int[] coord = this.findZero();
if (coord == null) return it;
int row = coord[0];
int col = coord[1];
if (row > 0) {
swap(row, col, row - 1, col);
it.enqueue(new Board(blocks));
swap(row, col, row - 1, col);
}
if (row < this.dimension() - 1) {
swap(row, col, row + 1, col);
it.enqueue(new Board(blocks));
swap(row, col, row + 1, col);
}
if (col > 0) {
swap(row, col, row, col - 1);
it.enqueue(new Board(blocks));
swap(row, col, row, col - 1);
}
if (col < this.dimension() - 1) {
swap(row, col, row, col + 1);
it.enqueue(new Board(blocks));
swap(row, col, row, col + 1);
}
return it;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.dimension());
sb.append("\n");
for (int i = 0; i < this.dimension(); ++i) {
for (int j = 0; j < this.dimension(); ++j) {
sb.append(" ");
sb.append(blocks[i][j]);
}
sb.append("\n");
}
return sb.toString();
}
}