[英]Java Method that checks for objects diagonally in 2D array
I am doing an N Queens Program in Java. 我正在用Java执行N Queens程序。 I was able to print all the solutions where each Queen is on a different row and column.
我能够打印所有解决方案,其中每个皇后位于不同的行和列。 Now I need to keep track of the diagonals for collisions.
现在,我需要跟踪碰撞的对角线。 So there are 2n-1 diagonal lines on a 2D array.
因此,二维数组上有2n-1条对角线。 The algorithm wants us to there are 2n-1 negative diagonal lines and 2n - 1 positive diagonal lines on the chessboard.
该算法希望我们在棋盘上有2n-1条负对角线和2n-1条正对角线。 There is an array of size 2n-1, called d1, that keeps track of the number of queens, ie, the number of collisions, on each of the 2n-1 negative diagonal lines.
有一个大小为2n-1的数组,称为d1,用于跟踪2n-1个负对角线上的皇后数量,即碰撞数量。 If there are k queens on the mth negative diagonal line, there are k-1 collisions on this diagonal line.
如果在第m条负对角线上有k个皇后,则在该对角线上有k-1个碰撞。 The number k is written into the mth element of the d1 array.
数字k被写入d1数组的第m个元素。 Similarly, we choose another array with size 2n-1, called d2, for 2n-1 positive diagonal lines.
同样,我们为2n-1条正对角线选择另一个大小为2n-1的数组,称为d2。
Here is my method for D2, but I am completely lost. 这是我的D2方法,但是我完全迷路了。 I know that all the up diagonals are row + col, but that is it.
我知道所有对角线都是row + col,但是仅此而已。
public void D2(){
int[] upDiag = new int[2*board.length - 1];
int numberOfCollisions = 0;
for(int row = 0; row < board.length; row++){
for(int col = 0; col < board.length; col++){
if(isQueen(row, col)){
upDiag[numberOfCollisions++];
}
}
}
}
I've written a three-part series on the Eight-Queens/N-Queens Problem. 我已经写了一个八部分的八皇后/ N皇后问题系列。
Here's a general outline of the problem, and a recursive solution. 这是问题的大致概述以及递归解决方案。
Here's a genetic algorithm solution. 这是一个遗传算法解决方案。
Here's a simulated annealing solution. 这是一个模拟的退火解决方案。
For the collision checking itself, something like this works very well: 对于碰撞检查本身,类似以下的方法非常有效:
double assessFitness(Integer[] candidate) {
int collisions = 0;
final int MAXIMUM_COLLISIONS = calculateMaxCollisions();
for (int i = 0; i < GRID_SIZE - 1; i++) {
for (int j = i + 1; j < GRID_SIZE; j++) {
if ((candidate[i].equals(candidate[j])) || j - i == Math.abs(candidate[i] - candidate[j]))
collisions++;
}
}
return (MAXIMUM_COLLISIONS - collisions) / (double) MAXIMUM_COLLISIONS;
}
Note that this is adapted from my genetic algorithm solution. 请注意,这是根据我的遗传算法解决方案改编而成。 I do explain why I return a value that scales from 0 to 1 in the blog article, but in your case a slight modification would yield the result you're looking for:
我确实解释了为什么我在博客文章中返回一个从0到1缩放的值,但是在您的情况下,稍加修改就会产生您想要的结果:
int countCollisions(Integer[] candidate) {
int collisions = 0;
final int MAXIMUM_COLLISIONS = calculateMaxCollisions();
for (int i = 0; i < GRID_SIZE - 1; i++) {
for (int j = i + 1; j < GRID_SIZE; j++) {
if ((candidate[i].equals(candidate[j])) || j - i == Math.abs(candidate[i] - candidate[j]))
collisions++;
}
}
return collisions;
}
In order for this to work, you do need to calculate the maximum allowable number of collisions for your N-Queens problem. 为了使它起作用,您确实需要计算您的N-Queens问题的最大允许碰撞次数。
private int calculateMaxCollisions() {
int sum = 0;
for (int i = GRID_SIZE - 1; i > 0; i--) {
sum += i;
}
return sum;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.