-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBoard.java
More file actions
150 lines (128 loc) · 2.83 KB
/
Copy pathBoard.java
File metadata and controls
150 lines (128 loc) · 2.83 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
public class Board {
private int [][] blocks;
private int n;
public Board(int[][] blocks) {
this.blocks = blocks;
this.n = blocks.length;
}
private int getValue(int x, int y) {
return x * n + y + 1;
}
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 (blocks[i][j] == 0)
continue;
if (blocks[i][j] != getValue(i, j))
hamming++;
}
return hamming;
}
public int manhattan() {
int manhattan = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (blocks[i][j] == 0)
continue;
int row = (blocks[i][j] - 1) / n;
int col = (blocks[i][j] - 1) % n;
manhattan += (Math.abs(i - row) + Math.abs(j - col));
}
return manhattan;
}
public boolean isGoal() {
return hamming() == 0;
}
public Board twin() {
int[][] t = copyBoard();
for (int i = 0; i < n; i++) {
boolean ok = true;
for (int j = 0; j < n; j++) {
if (t[i][j] == 0) {
ok = false;
break;
}
}
if (ok) {
int tmp = t[i][0];
t[i][0] = t[i][1];
t[i][1] = tmp;
break;
}
}
return new Board(t);
}
public boolean equals(Object v) {
boolean res;
if (v == this) return true;
if (v == null) return false;
if (!(v instanceof Board))
return false;
Board that = (Board) v;
if (that.dimension() != dimension())
return false;
for (int i = 0; i < dimension(); i++)
for (int j = 0; j < dimension(); j++)
if (that.blocks[i][j] != blocks[i][j])
return false;
return true;
}
private int[][] copyBoard() {
int[][] b = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
b[i][j] = blocks[i][j];
return b;
}
public Iterable<Board> neighbors() {
int blankX, blankY;
Queue<Board> boards = new Queue<Board>();
int tmp;
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (blocks[i][j] == 0) {
if (i != 0) {
int[][] nb = copyBoard();
nb[i][j] = nb[i - 1][j];
nb[i - 1][j] = 0;
boards.enqueue(new Board(nb));
}
if (i != n - 1) {
int[][] nb = copyBoard();
nb[i][j] = nb[i + 1][j];
nb[i + 1][j] = 0;
boards.enqueue(new Board(nb));
}
if (j != 0) {
int[][] nb = copyBoard();
nb[i][j] = nb[i][j - 1];
nb[i][j - 1] = 0;
boards.enqueue(new Board(nb));
}
if (j != n - 1) {
int[][] nb = copyBoard();
nb[i][j] = nb[i][j + 1];
nb[i][j + 1] = 0;
boards.enqueue(new Board(nb));
}
return boards;
}
return null;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(n + "\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sb.append(String.format("%2d", blocks[i][j]));
}
sb.append("\n");
}
return sb.toString();
}
}