繁体   English   中英

N皇后区 - Java

[英]N Queens - Java

我自己解决了 N 个皇后问题,并采用了解决方案和在线中提到的不同方法。

我的代码适用于最多 4 的输入,但开始打印每个案例(即使是错误的)对于 4 之后的任何值。我已经检查了很多次,但我无法在代码中找到任何错误。

PFA 代码,看看你是否能找到错误。 谢谢!

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        Scanner scn = new Scanner(System.in);
        int n= scn.nextInt();
        int[][] arr = new int[n][n];
        printNQueens(arr,"",0);
        System.out.println();


    }

    public static void printNQueens(int[][] chess, String qsf, int row) {
        if(row==chess.length)
        {
            qsf = qsf + ".";
            System.out.println(qsf);
            return;
        }
        for(int j=0;j<chess[0].length;j++)
        {
            if(chess[row][j]==0)
            {
                int x=row,y=j;
                while(x<chess.length)
                {
                    chess[x][y] = 1;
                    x++;
                }
                x = row;
                while(x<chess.length && y>=0)
                {
                    chess[x][y] = 1;
                    x++;
                    y--;
                }
                x = row;
                y = j;
                 while(x<chess.length && y<chess[0].length)
                {
                    chess[x][y] = 1;
                    x++;
                    y++;
                }
                printNQueens(chess,qsf + row + "-" + j + ", ",row+1);
                x = row;
                y = j;
                 while(x<chess.length)
                {
                    chess[x][y] = 0;
                    x++;
                }
                x = row;
                while(x<chess.length && y>=0)
                {
                    chess[x][y] = 0;
                    x++;
                    y--;
                }
                x = row;
                y = j;
                 while(x<chess.length && y<chess[0].length)
                {
                    chess[x][y] = 0;
                    x++;
                    y++;
                }

            }
        }
    }
}
import java.io.*;

导入 java.util.*;

公共 class 主要 {

public static void main(String[] args) throws Exception {
    Scanner scn = new Scanner(System.in);
    int n= scn.nextInt();
    int[][] arr = new int[n][n];
    printNQueens(arr,"",0);
    System.out.println();


}

public static void printNQueens(int[][] chess, String qsf, int row) {
    if(row==chess.length)
    {
        qsf = qsf + ".";
        System.out.println(qsf);
        return;
    }
        for(int j=0;j<chess[0].length;j++)
        {
            if(chess[row][j]==0)
            {
                chess[row][j] = 1;
                ArrayList<Integer> ro = new ArrayList<>();
                ArrayList<Integer> co = new ArrayList<>();
                int x=row+1,y=j;
                while(x<chess.length)
                {
                    if(chess[x][y]==1)
                    {
                        ro.add(x);
                        co.add(y);
                    }
                    chess[x][y] = 1;
                    x++;
                }
                x = row+1;
                y = j - 1;
                while(x<chess.length && y>=0)
                {
                    if(chess[x][y]==1)
                    {
                        ro.add(x);
                        co.add(y);
                    }
                    chess[x][y] = 1;
                    x++;
                    y--;
                }
                x = row+1;
                y = j+1;
                 while(x<chess.length && y<chess[0].length)
                {
                    if(chess[x][y]==1)
                    {
                        ro.add(x);
                        co.add(y);
                    }
                    chess[x][y] = 1;
                    x++;
                    y++;
                }
                printNQueens(chess,qsf + row + "-" + j + ", ",row+1);
                x = row;
                y = j;
                 while(x<chess.length)
                {
                    chess[x][y] = 0;
                    x++;
                }
                x = row;
                while(x<chess.length && y>=0)
                {
                    chess[x][y] = 0;
                    x++;
                    y--;
                }
                x = row;
                y = j;
                 while(x<chess.length && y<chess[0].length)
                {
                    chess[x][y] = 0;
                    x++;
                    y++;
                }
                x = 0;
                y = 0;
                while(x<ro.size() && y<co.size())
                {
                    chess[ro.get(x)][co.get(y)] = 1;
                    x++;
                    y++;
                }

            }
        }


}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM