Skip to content

Commit dcf5afc

Browse files
committed
nqueens
refactor diagonal checking add maven jdk compiler version
1 parent 1a188e3 commit dcf5afc

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

Algorithms/pom.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<version>1.0-SNAPSHOT</version>
88
<name>Algorithms</name>
99
<url>http://maven.apache.org</url>
10-
10+
1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1313
</properties>
14-
14+
1515
<dependencies>
1616
<dependency>
1717
<groupId>junit</groupId>
@@ -20,4 +20,20 @@
2020
<scope>test</scope>
2121
</dependency>
2222
</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+
2339
</project>

NQueens-backtrack/pom.xml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,39 @@
77
<version>1.0-SNAPSHOT</version>
88
<name>NQueens-backtrack</name>
99
<url>http://maven.apache.org</url>
10-
10+
1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1313
</properties>
14-
14+
1515
<dependencies>
1616
<dependency>
1717
<groupId>junit</groupId>
1818
<artifactId>junit</artifactId>
1919
<version>4.11</version>
2020
<scope>test</scope>
2121
</dependency>
22-
22+
2323
<dependency>
2424
<groupId>com.dodecaedro</groupId>
2525
<artifactId>Algorithms</artifactId>
2626
<version>1.0-SNAPSHOT</version>
2727
</dependency>
28-
28+
2929
</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+
3045
</project>

NQueens-backtrack/src/main/java/com/dodecaedro/backtrack/nqueens/Queen.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,20 @@ public boolean isInSameYColumn(Queen otherQueen) {
2626
}
2727

2828
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 /
3033
if ((this.positionX-this.positionY)==(otherQueen.positionX-otherQueen.positionY)) {
3134
return true;
3235
}
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-
}
5536

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+
5643
// not in any same diagonal
5744
return false;
5845
}

0 commit comments

Comments
 (0)