-
Notifications
You must be signed in to change notification settings - Fork 2
/
Board.java
175 lines (154 loc) · 4.86 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Board {
private final int n;
private int[][] board;
private int freeRow;
private int freeColumn ;
public Board(int[][] blocks) {
n = blocks[0].length;
copyBoard(blocks);
}
private void copyBoard(int[][] blocks) {
board = new int[n][];
for (int i = 0; i < blocks.length; i++) {
board[i] = new int[blocks[i].length];
for (int j = 0; j < blocks[i].length; j++) {
board[i][j] = blocks[i][j];
if (board[i][j] == 0) {
freeRow = i;
freeColumn = j;
}
}
}
}
@Override
public boolean equals(Object that) {
if ((that == null) || (getClass() != that.getClass()) )
return false;
Board other = (Board) that;
return Arrays.deepEquals(this.board, other.board);
}
public int dimension() {
return n;
}
public int hamming() {
int hamming = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i == n - 1) && (j == n - 1))
break;
if (board[i][j] != findTargetValueOf(i, j))
hamming++;
}
}
return hamming;
}
private int findTargetValueOf(int i, int j) {
return i * n + j + 1;
}
public int manhattan() {
int manhattan = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
manhattan += manhattanDistanceOf(i, j);
}
}
return manhattan;
}
private int manhattanDistanceOf(int i, int j) {
int currentVal = board[i][j];
if (currentVal == 0)
return 0;
int verticalMoves = Math.abs(findCorrectRowOf(currentVal) - i);
int horizontalMoves = Math.abs(findCorrectColumnOf(currentVal) - j);
return verticalMoves + horizontalMoves;
}
private int findCorrectRowOf(int currentVal) {
int row = 0;
if (currentVal != 0)
row = (currentVal - 1) / n;
return row;
}
private int findCorrectColumnOf(int currentVal) {
int column = 0;
if (currentVal != 0) {
int row = findCorrectRowOf(currentVal);
if (row != 0)
column = currentVal % (row * n) - 1;
else
column = currentVal - 1;
}
if (column < 0)
column = n - 1;
return column;
}
public Board twin() {
int[][] twinBoard = Arrays.copyOf(board, board.length);
for (int i = 0; i < n; i++)
twinBoard[i] = Arrays.copyOf(board[i], board[i].length);
int toBeChanged = twinBoard[0][0];
if (toBeChanged != 0) {
if (twinBoard[0][1] != 0) {
twinBoard[0][0] = twinBoard[0][1];
twinBoard[0][1] = toBeChanged;
}
else {
twinBoard[0][0] = twinBoard[1][0];
twinBoard[1][0] = toBeChanged;
}
}
else {
toBeChanged = twinBoard[1][0];
twinBoard[1][0] = twinBoard[1][1];
twinBoard[1][1] = toBeChanged;
}
return new Board(twinBoard);
}
public Iterable<Board> neighbors() {
List<Board> neighbors = new ArrayList<>(4);
if ((freeColumn - 1) >= 0) {
exchangeWithBlank(freeRow, freeColumn - 1);
neighbors.add(new Board(board));
exchangeWithBlank(freeRow, freeColumn + 1);
}
if ((freeColumn + 1) < n) {
exchangeWithBlank(freeRow, freeColumn + 1);
neighbors.add(new Board(board));
exchangeWithBlank(freeRow, freeColumn - 1);
}
if ((freeRow + 1) < n) {
exchangeWithBlank(freeRow + 1, freeColumn);
neighbors.add(new Board(board));
exchangeWithBlank(freeRow - 1, freeColumn);
}
if ((freeRow - 1) >= 0) {
exchangeWithBlank(freeRow - 1, freeColumn);
neighbors.add(new Board(board));
exchangeWithBlank(freeRow + 1, freeColumn);
}
return neighbors;
}
private void exchangeWithBlank(int i, int j) {
board[freeRow][freeColumn] = board[i][j];
board[i][j] = 0;
freeColumn = j;
freeRow = i;
}
public boolean isGoal() {
return hamming() == 0;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(n).append("\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
s.append(String.format("%2d ", board[i][j]));
}
s.append("\n");
}
return s.toString();
}
public static void main(String[] args) { }
}