Java 我试图编码n皇后问题,我';我是beginer,我认为主要的逻辑是正确的,但我没有按要求得到输出。有人能帮我吗?

Java 我试图编码n皇后问题,我';我是beginer,我认为主要的逻辑是正确的,但我没有按要求得到输出。有人能帮我吗?,java,debugging,backtracking,n-queens,recursive-backtracking,Java,Debugging,Backtracking,N Queens,Recursive Backtracking,我遵循的算法 Place (k, i) { For j ← 1 to k - 1 do if (x [j] = i) or (Abs x [j]) - i) = (Abs (j - k)) then return false; return true; } Place (k, i) return true if a queen can be placed in the kth row and ith

我遵循的算法

Place (k, i)  
   {  
     For j  ←  1 to k - 1  
      do if (x [j] = i)  
       or (Abs x [j]) - i) = (Abs (j - k))  
    then return false;  
     return true;  
}  
Place (k, i) return true if a queen can be placed in the kth row and ith column otherwise return is false.

x [] is a global array whose final k - 1 values have been set. Abs (r) returns the absolute value of r.

N - Queens (k, n)  
{  
   For i  ←  1 to n  
        do if Place (k, i) then  
   {  
      x [k]  ←  i;  
      if (k ==n) then  
        write (x [1....n));  
      else  
      N - Queens (k + 1, n);  
   }  
}  
Place(k,i)返回一个布尔值,如果第k个皇后可以放在列i中,则该值为true。它测试i是否不同于所有以前的成本x1、x2、…xk-1,以及是否在同一对角线上没有其他皇后

利用place给出了n皇后问题的精确解

Java代码实现:

import java.util.Scanner;
public class Main {

    static Scanner scan;
    static int n[];
    static int N;
    static int count;
    
      static boolean place(int k,int i)
     {
     for (int j =0;j<k-1;j++) {
    
    if (n[j]== i || Math.abs(n[j]-i)== Math.abs(j- k)) {
    
        return false; } }
            return true;
        }
      
      static void NQueens(int k,int n1) {
                 for (int i =0;i<n1;i++) {
                     
       if (place(k,i))
       {
           n[k]=i;
      if (k==n1) {
        
          for(int i1:n) {
            
              System.out.println(n[i1]);
              count++;
          }}
      else {
        
          NQueens(k+1,n1);}
   
       }}}
      

    public static void main(String[] args) {
        scan = new Scanner(System.in);
        System.out.println("State the value of N in this program!");
         N=scan.nextInt();
         n = new int[N];
         NQueens(0,N-1);
        if(count==0) {
            System.out.println("Solution not found");
        }
    }
}
import java.util.Scanner;
公共班机{
静态扫描;
静态int n[];
静态int N;
静态整数计数;
静态布尔位置(int k,int i)
{

对于(int j=0;j我无法理解你的程序,因为你必须有一个2d数组作为棋盘,并将其初始化为0。当皇后可以放置在单元格上时,请将其设置为1。请 阅读以下有关的文档

它用许多语言描述和解决N-Queen问题,例如Java,下面为4个Queen复制了Java

/* Java program to solve N Queen Problem using
backtracking */
public class NQueenProblem {
    final int N = 4;

    /* A utility function to print solution */
    void printSolution(int board[][])
    {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                System.out.print(" " + board[i][j]
                                + " ");
            System.out.println();
        }
    }

    /* A utility function to check if a queen can
    be placed on board[row][col]. Note that this
    function is called when "col" queens are already
    placeed in columns from 0 to col -1. So we need
    to check only left side for attacking queens */
    boolean isSafe(int board[][], int row, int col)
    {
        int i, j;

        /* Check this row on left side */
        for (i = 0; i < col; i++)
            if (board[row][i] == 1)
                return false;

        /* Check upper diagonal on left side */
        for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
            if (board[i][j] == 1)
                return false;

        /* Check lower diagonal on left side */
        for (i = row, j = col; j >= 0 && i < N; i++, j--)
            if (board[i][j] == 1)
                return false;

        return true;
    }

    /* A recursive utility function to solve N
    Queen problem */
    boolean solveNQUtil(int board[][], int col)
    {
        /* base case: If all queens are placed
        then return true */
        if (col >= N)
            return true;

        /* Consider this column and try placing
        this queen in all rows one by one */
        for (int i = 0; i < N; i++) {
            /* Check if the queen can be placed on
            board[i][col] */
            if (isSafe(board, i, col)) {
                /* Place this queen in board[i][col] */
                board[i][col] = 1;

                /* recur to place rest of the queens */
                if (solveNQUtil(board, col + 1) == true)
                    return true;

                /* If placing queen in board[i][col]
                doesn't lead to a solution then
                remove queen from board[i][col] */
                board[i][col] = 0; // BACKTRACK
            }
        }

        /* If the queen can not be placed in any row in
        this colum col, then return false */
        return false;
    }

    /* This function solves the N Queen problem using
    Backtracking. It mainly uses solveNQUtil () to
    solve the problem. It returns false if queens
    cannot be placed, otherwise, return true and
    prints placement of queens in the form of 1s.
    Please note that there may be more than one
    solutions, this function prints one of the
    feasible solutions.*/
    boolean solveNQ()
    {
        int board[][] = { { 0, 0, 0, 0 },
                        { 0, 0, 0, 0 },
                        { 0, 0, 0, 0 },
                        { 0, 0, 0, 0 } };

        if (solveNQUtil(board, 0) == false) {
            System.out.print("Solution does not exist");
            return false;
        }

        printSolution(board);
        return true;
    }

    // driver program to test above function
    public static void main(String args[])
    {
        NQueenProblem Queen = new NQueenProblem();
        Queen.solveNQ();
    }
}
// This code is contributed by Abhishek Shankhadhar

/*使用Java程序解决N Queen问题
回溯*/
公共类问题{
最终整数N=4;
/*打印解决方案的实用函数*/
无效打印解决方案(集成电路板[])
{
对于(int i=0;i=0&&j>=0;i--,j--)
如果(板[i][j]==1)
返回false;
/*检查左侧的下对角线*/
对于(i=row,j=col;j>=0&&i=N)
返回true;
*考虑此栏并尝试放置
这是一个一个排的女王*/
对于(int i=0;i
冒着听起来很傻的风险,也许您希望打印行,使数字彼此相邻

如果是,请使用System.out.print(n[i1])而不是System.out.println(n[i1])

然后在for(int i1:n)循环结束后添加一个println。 println始终在末尾添加新行