-
Notifications
You must be signed in to change notification settings - Fork 28
/
EightQueensMod1Solution.java
168 lines (131 loc) · 3.24 KB
/
EightQueensMod1Solution.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
package ds_009_backtracking;
public class EightQueensMod1Solution {
private static int count = 0;
public static void main(String[] args) {
boolean[] occupied = new boolean[64];
int[] numberOfAttacks = new int[64];
boolean result = findSolution(occupied, numberOfAttacks, 0);
if(result) {
printChessBoard(occupied);
printChessBoard(numberOfAttacks);
}
System.out.println(count + " nodes visited");
}
private static boolean findSolution(boolean[] occupied, int[] numberOfAttacks, int queensCount) {
if(queensCount == 8) {
return true;
}
for(int i = 0; i < occupied.length; i++) {
if(!occupied[i] && numberOfAttacks[i] == 0) {
count++;
occupied[i] = true;
countAttacks(i, occupied, numberOfAttacks, +1);
if(findSolution(occupied, numberOfAttacks, queensCount+1)) {
return true;
}
countAttacks(i, occupied, numberOfAttacks, -1);
occupied[i] = false;
}
}
return false;
}
private static void countAttacks(int q, boolean[] occupied, int[] numberOfAttack, int amount) {
countHorizontal(q, occupied, numberOfAttack, amount);
countVertical(q, occupied, numberOfAttack, amount);
countLeftCross(q, occupied, numberOfAttack, amount);
countRightCross(q, occupied, numberOfAttack, amount);
}
private static void countHorizontal(int q, boolean[] occupied, int[] numberOfAttack, int amount) {
// 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;
}
numberOfAttack[i]+=amount;
}
}
private static void countVertical(int q, boolean[] occupied, int[] numberOfAttack, int amount) {
int startCol = q%8;
int endCol = startCol + 8*(8-1);
for(int i = startCol; i <= endCol; i+= 8) {
if(i == q) {
continue;
}
numberOfAttack[i]+=amount;
}
}
/*
* \
* \
* \
*/
private static void countLeftCross(int q, boolean[] occupied, int[] numberOfAttack, int amount) {
int i = q;
// Upper Cross
while(i%8 != 0) {
i -= 9;
if(i < 0 || i > 63) {
break;
}
numberOfAttack[i]+=amount;
}
i = q;
// Lower Cross
while(i%8 != 7) {
i += 9;
if(i < 0 || i > 63) {
break;
}
numberOfAttack[i]+=amount;
}
}
/*
* /
* /
* /
*/
private static void countRightCross(int q, boolean[] occupied, int[] numberOfAttack, int amount) {
int i = q;
// Upper Cross
while(i%8 != 7 && i >= 0) {
i -= 7;
if(i < 0 || i > 63) {
break;
}
numberOfAttack[i]+=amount;
}
i = q;
// Lower Cross
while(i%8 != 0 && i < 64) {
i += 7;
if(i < 0 || i > 63) {
break;
}
numberOfAttack[i]+=amount;
}
}
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");
}
}
}
private static void printChessBoard(int[] numberOfAttacks) {
for(int i = 0; i < numberOfAttacks.length; i++) {
System.out.printf("%d ", numberOfAttacks[i]);
if(i%8 == 7) {
System.out.print("\n");
}
}
}
}