-
Notifications
You must be signed in to change notification settings - Fork 28
/
CustomEightQueens.java
183 lines (143 loc) · 2.9 KB
/
CustomEightQueens.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
package ds_009_backtracking;
public class CustomEightQueens {
public static void main(String[] args) {
boolean[] occupied;
int start = 0;
int queens = 0;
do{
occupied = new boolean[64];
queens = getEightQueens(start++, occupied);
if(queens == 8) {
System.out.println("\t\tFOUND SOLUTION");
break;
}
} while(queens < 7 && start < (64-8));
printChessBoard(occupied);
System.out.println("Queens: " + queens);
}
private static int getEightQueens(int start, boolean[] occupied) {
int queenCount = 0;
int q = start-1;
do{
q = getNextOccupied(q+1, occupied);
if(q != -1) {
queenCount++;
}
} while(q != -1);
return queenCount;
}
private static int getNextOccupied(int startIndex, boolean[] occupied) {
for(int i = startIndex; i < 64; i++) {
if(checkOccupied(i, occupied)) {
occupied[i] = true;
return i;
}
}
return -1;
}
private static boolean checkOccupied(int q, boolean[] occupied) {
return checkHorizontal(q, occupied) && checkVertical(q, occupied)
&& checkLeftCross(q, occupied) && checkRightCross(q, occupied);
}
private static boolean checkHorizontal(int q, boolean[] occupied) {
// get line number
int lineNumber = q/8;
int startOfLine = lineNumber*8;
int endOfLine = (lineNumber+1)*8 -1;
// check left
for(int i = startOfLine; i <= endOfLine; i++) {
if(i == q) {
continue;
}
if(occupied[i]) {
return false;
}
}
return true;
}
private static boolean checkVertical(int q, boolean[] occupied) {
int startCol = q%8;
int endCol = startCol + 8*(8-1);
for(int i = startCol; i <= endCol; i+= 8) {
if(i == q) {
continue;
}
if(occupied[i]) {
return false;
}
}
return true;
}
/*
* \
* \
* \
*/
private static boolean checkLeftCross(int q, boolean[] occupied) {
int i = q;
// Upper Cross
while(i%8 != 0) {
i -= 9;
if(i < 0 || i > 63) {
break;
}
if(occupied[i]) {
return false;
}
}
i = q;
// Lower Cross
while(i%8 != 7) {
i += 9;
if(i < 0 || i > 63) {
break;
}
if(occupied[i]) {
return false;
}
}
return true;
}
/*
* /
* /
* /
*/
private static boolean checkRightCross(int q, boolean[] occupied) {
int i = q;
// Upper Cross
while(i%8 != 7 && i >= 0) {
i -= 7;
if(i < 0 || i > 63) {
break;
}
if(occupied[i]) {
return false;
}
}
i = q;
// Lower Cross
while(i%8 != 0 && i < 64) {
i += 7;
if(i < 0 || i > 63) {
break;
}
if(occupied[i]) {
return false;
}
}
return true;
}
private static void printChessBoard(boolean[] occupied) {
for(int i = 0; i < occupied.length; i++) {
if(occupied[i]) {
System.out.print("Q ");
} else {
System.out.print(". ");
}
if(i%8 == 7) {
System.out.print("\n");
}
}
}
}