N * N皇后算法得到的坐标(N * N queen algorithm getting the c

2019-09-26 13:58发布

我试图实现N*N一个小的转折之女王算法。 在这个版本的女王也可以移动像一个骑士...

一切工作正常,但我试图让所有可能的解决方案的坐标。 问题是,如果我把它里面col == n它只是打印的最后一个。 的任何想法如何解决这个问题?

  static void placement(int col, int queens[], int n){
    //int solution =0; 
    for (int row = 1; row <= n; row++) {
      queens[col] = row;
      if((check_Queen(row,col,queens)) ==  true)
      {
        if((check_KnightMove(row,col,queens)) == true)
        {
          if(col == n)
          {
            System.out.println("("+row + "," + col);
            System.out.println("solution=" + solution);
            solution++;
          }
          else
          { 
            placement(col+1,queens,n);   
          }
        }
      }
    }
    queens[col] = 0;
  }

  public static void main(String[] args) {
    int solution =0;
    Scanner scanner=new Scanner(System.in);
    System.out.print("Please enter N");
    int n = scanner.nextInt();// TODO Auto-generated method stub
    int queens[] = new int[n+1];
    placement(1,queens,n);
    System.out.println("nQueens: solution=" + solution);
  }
      static boolean check_Queen(int row, int col, int queens[])
{

    //boolean flag = false;
    for(int i =1; i<col; i++)
    {
        if (queens[col-i] == row   ||
                 queens[col-i] == row-i ||
                 queens[col-i] == row+i) {
                //flag = false;
                return false;
             }

    }
    return true;


}
           static boolean  check_KnightMove(int row, int col, int queens[])
           {
    if(col>=2&&(queens[col-2] == (row -1) || queens[col-2] == (row+1) || queens[col-1] == (row-2) || queens[col-1] == (row+2)))
    {
        return false;
    }
    return true;

}


}

Answer 1:

这是很难说什么是错在你的解决方案,不知道如何check_Queencheck_KnightMove定义。 这是我会怎么解决的任务:

public class Queens
{
    static void printSolution (int [] queens)
    {
        int l = queens.length;
        for (int i = 0; i < l; i++)
        {
            for (int j = 0; j < l; j++)
            {
                System.out.print (queens [i] == j ? 'Q' : '.');
                System.out.print (' ');
            }
            System.out.println ();
        }
        System.out.println ();
    }

    static int placement (int [] queens, int c)
    {
        if (c == queens.length)
        {
            printSolution (queens);
            return 1;
        }
        else
        {
            int solutionCount = 0;
            int l = queens.length;

            for (int r = 0; r < l; r++)
            {
                boolean flag = false;
                for (int i = 0; i < c; i++)
                {
                    int xd = c - i;
                    int yd = Math.abs (r - queens [i]);

                    if (yd == 0 || xd == yd)
                    {
                        flag = true;
                        break;
                    }

                    // Knight move support
                    if ((xd == 1 && yd == 2) || (xd == 2 && yd == 1))
                    {
                        flag = true;
                        break;
                    }
                }

                if (!flag)
                {
                    queens [c] = r;
                    solutionCount += placement (queens, c + 1);
                }
            }

            return solutionCount;
        }
    }

    public static void main (String [] args)
    {
        System.out.println (
            "Total solutions found: " + placement (new int [11], 0));
    }
}

在您的解决方案,方法check_KnightMove不正确。 当col == 2它不允许皇后被放置在第1行,因为它认为总是有一个大号(col: 0, row: 0)即,在板的外侧。 下面是修改后的版本:

static boolean check_KnightMove (int row, int col, int queens[])
{
    if (col >= 3
            && (queens [col - 2] == (row - 1) || queens [col - 2] == (row + 1)))
    {
        return false;
    }

    if (col >= 2
            && (queens [col - 1] == (row - 2) || queens [col - 1] == (row + 2)))
    {
        return false;
    }
    return true;
}


文章来源: N * N queen algorithm getting the coordinates