File tree Expand file tree Collapse file tree 3 files changed +47
-29
lines changed
src/main/java/com/dodecaedro/backtrack/nqueens Expand file tree Collapse file tree 3 files changed +47
-29
lines changed Original file line number Diff line number Diff line change 7
7
<version >1.0-SNAPSHOT</version >
8
8
<name >Algorithms</name >
9
9
<url >http://maven.apache.org</url >
10
-
10
+
11
11
<properties >
12
12
<project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
13
13
</properties >
14
-
14
+
15
15
<dependencies >
16
16
<dependency >
17
17
<groupId >junit</groupId >
20
20
<scope >test</scope >
21
21
</dependency >
22
22
</dependencies >
23
+
24
+
25
+ <build >
26
+ <plugins >
27
+ <plugin >
28
+ <groupId >org.apache.maven.plugins</groupId >
29
+ <artifactId >maven-compiler-plugin</artifactId >
30
+ <version >2.3.2</version >
31
+ <configuration >
32
+ <source >1.6</source >
33
+ <target >1.6</target >
34
+ </configuration >
35
+ </plugin >
36
+ </plugins >
37
+ </build >
38
+
23
39
</project >
Original file line number Diff line number Diff line change 7
7
<version >1.0-SNAPSHOT</version >
8
8
<name >NQueens-backtrack</name >
9
9
<url >http://maven.apache.org</url >
10
-
10
+
11
11
<properties >
12
12
<project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
13
13
</properties >
14
-
14
+
15
15
<dependencies >
16
16
<dependency >
17
17
<groupId >junit</groupId >
18
18
<artifactId >junit</artifactId >
19
19
<version >4.11</version >
20
20
<scope >test</scope >
21
21
</dependency >
22
-
22
+
23
23
<dependency >
24
24
<groupId >com.dodecaedro</groupId >
25
25
<artifactId >Algorithms</artifactId >
26
26
<version >1.0-SNAPSHOT</version >
27
27
</dependency >
28
-
28
+
29
29
</dependencies >
30
+
31
+ <build >
32
+ <plugins >
33
+ <plugin >
34
+ <groupId >org.apache.maven.plugins</groupId >
35
+ <artifactId >maven-compiler-plugin</artifactId >
36
+ <version >2.3.2</version >
37
+ <configuration >
38
+ <source >1.6</source >
39
+ <target >1.6</target >
40
+ </configuration >
41
+ </plugin >
42
+ </plugins >
43
+ </build >
44
+
30
45
</project >
Original file line number Diff line number Diff line change @@ -26,33 +26,20 @@ public boolean isInSameYColumn(Queen otherQueen) {
26
26
}
27
27
28
28
public boolean isInSameDiagonal (Queen otherQueen ) {
29
- // first diagonal \
29
+ // based on y = x + a
30
+ // if they're on the same diagonal, y - x = a
31
+
32
+ // first diagonal /
30
33
if ((this .positionX -this .positionY )==(otherQueen .positionX -otherQueen .positionY )) {
31
34
return true ;
32
35
}
33
- // now test /
34
- // down and left
35
- int posX = this .positionX ;
36
- int posY = this .positionY ;
37
- while (posX >= 0 && posY <= NQueensNode .SIZE ) {
38
- posX --;
39
- posY ++;
40
- if (posX == otherQueen .positionX && posY == otherQueen .positionY ) {
41
- return true ;
42
- }
43
- }
44
-
45
- // up and right
46
- posX = this .positionX ;
47
- posY = this .positionY ;
48
- while (posX <= NQueensNode .SIZE && posX <= NQueensNode .SIZE ) {
49
- posX ++;
50
- posY --;
51
- if (posX == otherQueen .positionX && posY == otherQueen .positionY ) {
52
- return true ;
53
- }
54
- }
55
36
37
+ // in this case, it's y = -x +a
38
+ // diagonal \
39
+ if ((this .positionX +this .positionY )==(otherQueen .positionX +otherQueen .positionY )) {
40
+ return true ;
41
+ }
42
+
56
43
// not in any same diagonal
57
44
return false ;
58
45
}
You can’t perform that action at this time.
0 commit comments