Java 如何检查nqueen中的对角线?

Java 如何检查nqueen中的对角线?,java,function,testing,methods,Java,Function,Testing,Methods,我正在用java编写NQueens程序,但是我在检查对角线时遇到了问题。现在我只是以某种方式穿过数组,所以它会进入下一个对角线点,但在通过第一种可能性后,它会让我出界。有没有其他方法可以做到这一点?我做错了吗 这里是给我数组越界异常的部分 private boolean checkLowerDiag(int row, int col) { if(col == 0) { return false; } else {

我正在用java编写NQueens程序,但是我在检查对角线时遇到了问题。现在我只是以某种方式穿过数组,所以它会进入下一个对角线点,但在通过第一种可能性后,它会让我出界。有没有其他方法可以做到这一点?我做错了吗

这里是给我数组越界异常的部分

private boolean checkLowerDiag(int row, int col)
{
    if(col == 0)
    {
        return false;             
    }
    else
    {
        for(int i = row, j = col; i < myNumsQueen && j >= 0;  i++, j--)
        {
            if(myBoard[i+1][j-1] == true) // this line is the out of bounds
            {
                return true;
            }
        }
        return false;
    }
}
private boolean checkLowerDiag(int行,int列)
{
如果(列==0)
{
返回false;
}
其他的
{
对于(int i=row,j=col;i=0;i++,j--)
{
if(myBoard[i+1][j-1]==true)//这一行是越界
{
返回true;
}
}
返回false;
}
}
这是我的全部代码

package model;

public class NQueensModel
{
    private int myNumsQueen;
    public int myPossibilities=0;
    private boolean[][] myBoard;
    private static NQueensModel myModel = new NQueensModel(4);

    public static void main (String[] args) {

        System.out.println(myModel.solvePuzzle());


        System.out.println(myModel.myPossibilities);
       // System.out.println(myPossibilities);


    }
    public NQueensModel(int nQueens)
    {
        myNumsQueen = nQueens;
        myPossibilities=0;
        myBoard = new boolean[myNumsQueen][myNumsQueen];
    }
    public boolean solvePuzzle()
    {

        return this.solvePuzzle(0);

    }
    private boolean solvePuzzle(int ncolumn)
    {
        if(ncolumn>myNumsQueen-1)
        {
            myPossibilities++;
//            return true;

        }
        int i;

        for( i =0; i<myNumsQueen;i++)
        {

            if(this.isSafeMove(i, ncolumn)==true)
            {

                this.placeQueen(i,ncolumn);
                System.out.println(i+ " " + ncolumn);
                if(this.solvePuzzle(ncolumn+1)==true)
                {

                    return true;

                }
                this.removeQueen(i, ncolumn);
                System.out.println("remove: " + i+ " " + ncolumn);
            }


        }

        return false;

    }
    private int solveCount(int count, int col)
    {
        for(int j = 0 ; j<col; j++){
            if(myModel.solvePuzzle(j)==true){
             count++;
        }
        else if(myModel.solvePuzzle(j)==false){
            return count;
        }
        else
        {
            for(int i =0 ; i<col;i++){
                count = solveCount(count, col+1);
            }
            return count;
        }
        return count;
        }
        return count;

    }
    private boolean isSafeMove(int row, int col)
    {

         if(this.checkLowerDiag(row, col)==true ||this.checkUpperDiag(row, col)==true ||this.checkLeft(row,col)==true)
         {
             return false;
         }
        else
        {

            return true;
        }


    }
    private boolean checkUpperDiag(int row, int col)
    {    
        if(row==0)
        {
            return false;
        }
        else
        {
            for(int i=row, j = col; i>=0 && j>=0; i--, j--)
            {
                if(myBoard[i][j]==true)
                {
                    return true;
                }
            }
            return false;
        }
    }
    private boolean checkLowerDiag(int row, int col)
    {

        if(col==0 )
        {           
            return false;             
        }
        else
        {

            for(int i = row, j = col; i<myNumsQueen && j>=0;  i++, j--)
            {
                if(myBoard[i+1][j-1]==true)
                {
                    return true;

                }


            }
            return false;
        }
    }
    private boolean checkLeft(int row, int col)
    {
        if(col==0)
        {
            return false;
        }
        else 
        {
            for(int i = col; i>=0; i--)
            {
                if(myBoard[row][i]==true)
                {
                    return true;
                }
            }
            return false;
        }

    }
    private boolean placeQueen(int row, int col)
    {
        myBoard[row][col] = true;
        return true;
    }
    private boolean removeQueen(int row, int col)
    {
        myBoard[row][col] = false;
        return false;
    }
//    public String toString()
//    {
//        
//    }
}
包模型;
公共类模型
{
密努姆斯奎因私人酒店;
公共int=0;
私有布尔[][]myBoard;
私有静态NQueensModel myModel=新的NQueensModel(4);
公共静态void main(字符串[]args){
System.out.println(myModel.solvePuzzle());
System.out.println(myModel.myprobabilities);
//System.out.println(我的可能性);
}
公共NQueensModel(intnqueens)
{
myNumsQueen=nQueens;
我的可能性=0;
myBoard=新布尔值[myNumsQueen][myNumsQueen];
}
公共布尔解谜()
{
返回此。0;
}
私有布尔解算器(int-ncolumn)
{
如果(n列>myNumsQueen-1)
{
myc++;
//返回true;
}
int i;
对于(i=0;i=0;i--)
{
如果(myBoard[行][i]==true)
{
返回true;
}
}
返回false;
}
}
专用布尔placeQueen(整数行,整数列)
{
myBoard[行][列]=真;
返回true;
}
私有布尔removeQueen(整数行,整数列)
{
myBoard[row][col]=假;
返回false;
}
//公共字符串toString()
//    {
//        
//    }
}

提示:当j变为0时会发生什么?当j变为0时,表示它位于电路板的第一列