13
13
* @author JM
14
14
*/
15
15
public class NQueensNode implements BacktrackNode {
16
- public static final int SIZE = 4 ;
16
+ public int boardSize = 4 ;
17
17
18
18
private List <Queen > queens ;
19
19
private List <BacktrackNode > childrenNodes ;
@@ -23,9 +23,15 @@ public NQueensNode() {
23
23
childrenNodes = new ArrayList <BacktrackNode >();
24
24
}
25
25
26
+ public NQueensNode (int size ) {
27
+ this ();
28
+ setBoardSize (size );
29
+ }
30
+
26
31
private NQueensNode (NQueensNode copy ) {
27
32
this ();
28
33
this .queens .addAll (copy .queens );
34
+ setBoardSize (copy .getBoardSize ());
29
35
}
30
36
31
37
/*
@@ -37,7 +43,7 @@ private NQueensNode(NQueensNode copy) {
37
43
public boolean isLeaf () {
38
44
// it's a leaf when it has all the queens, or there's no point in
39
45
// continuing
40
- return queens .size () == SIZE || anyQueenInDanger ();
46
+ return queens .size () == boardSize || anyQueenInDanger ();
41
47
}
42
48
43
49
/*
@@ -48,7 +54,7 @@ public boolean isLeaf() {
48
54
@ Override
49
55
public boolean isGoal () {
50
56
// the goal is to have all the n queens where none endangers any other
51
- return queens .size () == SIZE && !anyQueenInDanger ();
57
+ return queens .size () == boardSize && !anyQueenInDanger ();
52
58
}
53
59
54
60
/*
@@ -58,8 +64,8 @@ public boolean isGoal() {
58
64
*/
59
65
private void generateChildrenNodes () {
60
66
List <BacktrackNode > nodes = new ArrayList <BacktrackNode >();
61
- for (int y = 1 ; y <= SIZE ; y ++) {
62
- for (int x = 1 ; x <= SIZE ; x ++) {
67
+ for (int y = 1 ; y <= boardSize ; y ++) {
68
+ for (int x = 1 ; x <= boardSize ; x ++) {
63
69
if (!hasQueen (x , y )) {
64
70
// make a copy of the current node which includes current queens
65
71
NQueensNode childrenNode = new NQueensNode (this );
@@ -86,21 +92,19 @@ public void processSolution() {
86
92
}
87
93
88
94
public boolean anyQueenInDanger () {
89
- for (Queen queen : this .queens ) {
90
- for (Queen compareQueen : this .queens ) {
91
- // don't compare a queen with herself
92
- if (!queen .equals (compareQueen )) {
93
- // is same horizontal row or vertical column?
94
- if (queen .isInSameXRow (compareQueen ) ||
95
- queen .isInSameYColumn (compareQueen )) {
96
- System .out .println ("Collision found: " + queen .toString () +
97
- " collides with: " + compareQueen .toString () + " - same line" );
95
+ /* compare every queen only with the immediate following
96
+ * commutative property applies here, so if a != b, then there's
97
+ * no need to compare b == a */
98
+ for (int queenIndex = 0 ; queenIndex < queens .size () ; queenIndex ++) {
99
+ for (int compareQueenIndex = queenIndex +1 ; compareQueenIndex < queens .size ()-1 ; compareQueenIndex ++) {
100
+ // is same horizontal row or vertical column?
101
+ if ( queens .get (queenIndex ).isInSameXRow (queens .get (compareQueenIndex )) ||
102
+ queens .get (queenIndex ).isInSameYColumn (queens .get (compareQueenIndex ))) {
103
+ return true ;
104
+ } else {
105
+ // is in the same diagonal?
106
+ if (queens .get (queenIndex ).isInSameDiagonal (queens .get (compareQueenIndex ))) {
98
107
return true ;
99
- } else {
100
- // is in the same diagonal?
101
- if (queen .isInSameDiagonal (compareQueen )) {
102
- return true ;
103
- }
104
108
}
105
109
}
106
110
}
@@ -131,4 +135,12 @@ public String toString() {
131
135
}
132
136
return builder .toString ();
133
137
}
138
+
139
+ public int getBoardSize () {
140
+ return boardSize ;
141
+ }
142
+
143
+ public void setBoardSize (int boardSize ) {
144
+ this .boardSize = boardSize ;
145
+ }
134
146
}
0 commit comments