为什么我的代码一直在进行无限测试(N 皇后问题 / Java / 蓝色)

huangapple go评论144阅读模式
英文:

why is my code endlessly testing (n queens problem / java / blue)

问题

以下是您提供的代码的翻译部分:

public class NQueensProblem {

    /**
     * Data structure for storing queen positions as a boolean array attribute of the class.
     */
    private boolean[][] queenBoard;

    /**
     * Returns a two-dimensional boolean array.
     * n is the chessboard width. The positions of the queens are marked as true (queen) or false (no queen).
     * If a solution is not possible, a NoSolutionException is thrown.
     */
    public boolean[][] solveNQueens(int n) throws NoSolutionException {
        this.queenBoard = new boolean[n][n];
        if (placeQueens(0)) {
            return queenBoard;
        } else {
            throw new NoSolutionException();
        }
    }

    /**
     * Backtracking algorithm. The dameNr parameter represents the current queen number to be placed.
     */
    private boolean placeQueens(int queenNum) {
        int row = 0;
        if (queenNum == queenBoard.length) {
            return true;
        } else {
            for (queenNum = 0; queenNum <= queenBoard.length; queenNum++) {
                if (isSafe(queenNum, row)) {
                    queenBoard[queenNum][row] = true;
                    if (placeQueens(queenNum + 1)) {
                        return true;
                    } else {
                        queenBoard[queenNum][row] = false;
                        row += 1;
                    }
                }
            }
            return false;
        }
    }

    // queenNum represents the column
    public boolean isSafe(int column, int row) {
        // Check horizontal and vertical
        for (int i = 0; i < queenBoard.length; i++) {
            if (queenBoard[i][row] || queenBoard[column][i]) {
                return false;
            }
        }

        // Check diagonals
        int tempColumn = column;
        int tempRow = row;

        // Upper right diagonal
        while (column >= 0 && column < queenBoard.length && row >= 0 && row < queenBoard.length) {
            column = tempColumn;
            row = tempRow;
            if (queenBoard[column][row]) {
                return false;
            }
            column += 1;
            row -= 1;
        }

        // Lower right diagonal
        while (column >= 0 && column < queenBoard.length && row >= 0 && row < queenBoard.length) {
            column = tempColumn;
            row = tempRow;
            if (queenBoard[column][row]) {
                return false;
            }
            column += 1;
            row += 1;
        }

        // Lower left diagonal
        while (column >= 0 && column < queenBoard.length && row >= 0 && row < queenBoard.length) {
            column = tempColumn;
            row = tempRow;
            if (queenBoard[column][row]) {
                return false;
            }
            column -= 1;
            row += 1;
        }

        // Upper left diagonal
        while (column >= 0 && column < queenBoard.length && row >= 0 && row < queenBoard.length) {
            column = tempColumn;
            row = tempRow;
            if (queenBoard[column][row]) {
                return false;
            }
            column -= 1;
            row -= 1;
        }

        return true;
    }
}

请注意,这只是您提供的代码的翻译部分,其中可能包含一些语法和逻辑错误。如果您发现问题,请在调试时进行修正。

英文:

so im currently working on an algorithm for the n queens problem and i think im done, however when i run a junit class to test the code its just endlessly working.

The blue bar just endlessly moves from left to right and so on (https://prnt.sc/ruqjef)

Im not quite sure why it is like that. The junit class is testing the code with n=8. When i try it with n=1 its working, when i try it with n>=2 its just loading/working.

I have no idea why, so i would really appreciaty some help c:

My code:

public class Damenproblem {
/**
* Datenstruktur f&#252;r das Speichern der Damenpositionen
* als Array-Attribut der Klasse
*/
private boolean[][] damenFeld;
/**
* Gibt ein zweidimensionales boolean-Array zurueck. 
* n ist die Schachbrettbreite. Auf den jeweiligen
* Positionen des Arrays sind die Positionen der Damen (true = Dame, false =
* keine Dame). Wenn sich das Problem nicht loesen laesst, wird eine
* NoSolutionException geworfen.
*/
public boolean[][] damen(int n) throws NoSolutionException{
this.damenFeld = new boolean[n][n];
if (setze(0)){
return damenFeld;
}
else {
throw new NoSolutionException();
}
}
/**
* Backtracking-Algorithmus
* Parameter dameNr gibt die laufende Nummer der zu setzenden Dame an.
*/
private boolean setze(int dameNr) {
int zeile = 0;
if(dameNr == damenFeld.length) {
return true;
} else {
for(dameNr = 0; dameNr &lt;= damenFeld.length; dameNr++) {
if(erlaubt(dameNr, zeile)) {
damenFeld[dameNr][zeile] = true;
if(setze(dameNr+1)) {
return true;
} else {
damenFeld[dameNr][zeile] = false;
zeile += 1;
}
}
}
return false;
}
}
// DameNr ist die Spalte
public boolean erlaubt(int spalte, int zeile) {
// check horizontal and vertical
int tempSpalte = spalte;
int tempZeile = zeile;
for(int i = 0; i &lt; damenFeld.length; i++) {
if(damenFeld[i][zeile] || damenFeld[spalte][i]) {
return false;
}
}
// check diagonal
// to upper right
while(spalte &gt;= 0 &amp;&amp; spalte &lt;= damenFeld.length &amp;&amp; zeile &gt;= 0 &amp;&amp; zeile &lt;= damenFeld.length){
spalte = tempSpalte;
zeile = tempZeile;
if(damenFeld[spalte][zeile]) {
return false;
}
spalte += 1;
zeile -= 1;
}
// check diagonal
// to lower right
while(spalte &gt;= 0 &amp;&amp; spalte &lt;= damenFeld.length &amp;&amp; zeile &gt;= 0 &amp;&amp; zeile &lt;= damenFeld.length){
spalte = tempSpalte;
zeile = tempZeile;
if(damenFeld[spalte][zeile]) {
return false;
}
spalte += 1;
zeile += 1;
}
// check diagonal
// to lower left
while(spalte &gt;= 0 &amp;&amp; spalte &lt;= damenFeld.length &amp;&amp; zeile &gt;= 0 &amp;&amp; zeile &lt;= damenFeld.length){
spalte = tempSpalte;
zeile = tempZeile;
if(damenFeld[spalte][zeile]) {
return false;
}
spalte -= 1;
zeile += 1;
}
// check diagonal
// to upper left
while(spalte &gt;= 0 &amp;&amp; spalte &lt;= damenFeld.length &amp;&amp; zeile &gt;= 0 &amp;&amp; zeile &lt;= damenFeld.length){
spalte = tempSpalte;
zeile = tempZeile;
if(damenFeld[spalte][zeile]) {
return false;
}
spalte -= 1;
zeile -= 1;
}
return true;
}
}

答案1

得分: 1

为了不覆盖您的dameNr参数,您方法setze()中的for循环应该如下所示:

for (zeile = 0; zeile < damenFeld.length; zeile++)

但是,您的erlaubt()检查是主要问题。我建议使用for循环,而不是使用while循环来定义新的临时参数。否则,您会一遍又一遍地使用传递的参数。

英文:

In order not to override your dameNr parameter the for loop in your method setze() should look like this:

for(zeile = 0; zeile &lt; damenFeld.length; zeile++)

Your erlaubt() check is the main problem though. I would recommend using for instead of while loops that define new temporary parameters each. Otherwise you keep using your passed parameters over an over again.

huangapple
  • 本文由 发表于 2020年4月7日 21:19:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61080967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定