-
Notifications
You must be signed in to change notification settings - Fork 0
/
NQueens.java
288 lines (220 loc) · 6.5 KB
/
NQueens.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import java.io.*;
import java.util.*;
// attempt to find a solution for N x N queen problems. Initialize the board so that
// each queen is in a different column and a different row. Use minimal-conflict heuristic
// to update board at each step. Check if number of conflicts is zero. If so, done. Repeat.
//
// This code often gets stuck in repeating board patterns, but sometimes finds solutions
// pretty quickly. It found a 50 x 50 solution in 358 steps.
public class NQueens {
private static final byte EMPTY = (byte)0;
private static final byte QUEEN = (byte)1;
private byte[] column;
public NQueens(int n) {
column = new byte[n];
List<Queen> queenList = new ArrayList<Queen>();
List<Integer> rowList = new ArrayList<Integer>();
int i;
for (i=0; i<n; i++) {
rowList.add(new Integer(i));
}
for (i=0; i<n; i++) {
queenList.add(new Queen(removeRandomItem(rowList), i));
}
dumpBoard(queenList);
for (i=0; i<Integer.MAX_VALUE; i++) {
evaluate(queenList);
if (solutionIsValid(queenList)) {
break;
}
rearrange(queenList);
}
System.out.println("\nsteps: " + i + "\n");
dumpBoard(queenList);
}
private void dumpBoard(List<Queen> list) {
if (list.isEmpty()) return;
List<Queen> slist = cloneList(list);
Collections.sort(slist);
int n = list.size();
for (int y=0; y<n; y++) {
for (int x=0; x<n; x++) {
Queen queen = null;
if (!slist.isEmpty()) {
queen = slist.get(0);
}
if (queen != null && queen.getColumn() == x && queen.getRow() == y) {
System.out.print("Q");
slist.remove(0);
} else {
System.out.print(".");
}
}
System.out.println();
}
System.out.println();
}
// evaluate a board position. Start by creating a list of all distinct pairs.
// Queen pairs that attack each other and queen triplets that are colinear accumulate strikes
private void evaluate(List<Queen> list) {
for (Queen q : list) {
q.strikes = 0;
}
List<Line> lineList = enumerateLines(list);
for (Line line : lineList) {
if (line.isAttackLine()) {
line.q1.strikes++;
line.q2.strikes++;
} else {
for (Queen q3 : list) {
if (q3.point.equals(line.q1.point) || q3.point.equals(line.q2.point)) continue;
if (line.isColinear(q3)) {
line.q1.strikes++;
line.q2.strikes++;
q3.strikes++;
}
}
}
}
}
private List<Queen> cloneList(List<Queen> list) {
List<Queen> result = new ArrayList<Queen>();
for (Queen q : list) {
result.add(new Queen(q));
}
return result;
}
// create a list of boards based on an input board, one board for each available row value
// for max strike queen
private List<List<Queen>> createColumnOptions(List<Queen> list, int obstructingQueenIndex) {
Queen queen, obstructingQueen;
int i;
List<List<Queen>> result = new ArrayList<List<Queen>>();
obstructingQueen = list.get(obstructingQueenIndex);
for (i=0; i<list.size(); i++) {
queen = list.get(i);
if (queen.getColumn() == obstructingQueen.getColumn()) {
column[queen.getRow()] = QUEEN;
}
}
for (i=0; i<column.length; i++) {
if (column[i] == EMPTY) {
List<Queen> ql = cloneList(list);
ql.get(obstructingQueenIndex).setRow(i);
result.add(ql);
}
}
return result;
}
// change the board by moving the queen with the most strikes to
// a square in the same column that has the least strikes
private void rearrange(List<Queen> list) {
int obstructingQueenIndex = -1;
int i, maxStrikes;
Queen queen;
for (i=0; i<column.length; i++) {
column[i] = EMPTY;
}
maxStrikes = -1;
// find the highest number of strikes
for (i=0; i<list.size(); i++) {
queen = list.get(i);
if (queen.strikes > maxStrikes) {
maxStrikes = queen.strikes;
}
}
List<Queen> maxList = new ArrayList<Queen>();
// create a list of queens that have maxStrikes strikes.
// This list will often have few or even just one element.
for (Queen q : list) {
if (q.strikes == maxStrikes) {
maxList.add(q);
}
}
// randomly choose one list element. This helps reduce the
// chance of the code getting trapped in a loop of recurring
// steps
int index = (int)(Math.random() * maxList.size());
queen = maxList.get(index);
for (i=0; i<list.size(); i++) {
Queen q = list.get(i);
if (q == queen) {
obstructingQueenIndex = i;
break;
}
}
// create a list of boards that enumerates all possible row positions for the
// queen with the most strikes
List<List<Queen>> optionList = createColumnOptions(list, obstructingQueenIndex);
for (List<Queen> option : optionList) {
evaluate(option);
}
int listIndex = -1;
int minStrikes = list.size() + 1;
// iterate over the variants of the max strike queen in the board versions
// and find the lowest strike count
for (i=0; i<optionList.size(); i++) {
List<Queen> option = optionList.get(i);
queen = option.get(obstructingQueenIndex);
if (queen.strikes < minStrikes) {
minStrikes = queen.strikes;
}
}
List<List<Queen>> minList = new ArrayList<List<Queen>>();
// create list of all queens with that strike value
for (List<Queen> option : optionList) {
queen = option.get(obstructingQueenIndex);
if (queen.strikes == minStrikes) {
minList.add(option);
}
}
index = (int)(Math.random() * minList.size());
// randomly choose one queen from that list
for (i=0; i<optionList.size(); i++) {
if (optionList.get(i) == minList.get(index)) {
listIndex = i;
break;
}
}
// update row of max strike queen in original board
queen = list.get(obstructingQueenIndex);
queen.setRow(optionList.get(listIndex).get(obstructingQueenIndex).getRow());
}
private boolean solutionIsValid(List<Queen> list) {
for (Queen q : list) {
if (q.strikes > 0) return false;
}
return true;
}
private List<Line> enumerateLines(List<Queen> list) {
List<Line> result = new ArrayList<Line>();
for (int i=0; i<list.size()-1; i++) {
for (int j=i+1; j<list.size(); j++) {
result.add(new Line(list.get(i), list.get(j)));
}
}
return result;
}
private int removeRandomItem(List<Integer> list) {
if (list.isEmpty()) return -1;
int index = (int)(Math.random() * list.size());
return list.remove(index).intValue();
}
private static void usage() {
System.err.println("usage: NQueens <n>");
System.exit(-1);
}
public static void main(String[] arg) {
if (arg.length == 0) usage();
int n = -1;
try {
n = Integer.parseInt(arg[0]);
} catch (NumberFormatException e) {
usage();
}
if (n < 0) {
usage();
}
new NQueens(n);
}
}