N-Queens 不打印所有解决方案

我是递归和回溯的新手。我正在尝试完成 N-Queen 问题以打印所有解决方案而不仅仅是 1 并理解这些概念。

我认为我已经部分正确地实现了算法,因为我得到了一些解决方案,但没有全部打印出来。我的代码是用 Java 编写的。

  • 对于 N = 4 的值,我得到 2 个解决方案 - 这是正确的

  • 对于 N = 5 的值,我得到 5 个解决方案 - 实际上有 10 个

  • 对于 N= 8 的值,我没有打印出来

我无法弄清楚我犯了什么错误。我的想法是意识到第一个皇后必须在第一行,第二个在第二行等等。我必须弄清楚哪一列是合适的,显然要注意对角线来放置一个皇后。

感谢您为我指明正确方向的任何帮助。我的代码在下面,我尝试添加注释以帮助理解。

public class nQueens {


    static class Queen {

        public Queen( int row, int column) {

            this.row = row;

            this.column = column;

        }

        int row = -1;

        int column = -1;

    }


    static ArrayList<Queen> queens = new ArrayList<Queen>();


    public static void main(String argv[]) {

        int n = 5;

        int[][] chessBoard = new int[n][n];

        int placed = 0;

        solve(n, chessBoard, placed);

    }


    public static void solve(int n, int[][] chessBoard, int placed) {

        // this means that all the queens have been placed

        if (placed == n) {

            System.out.println("**** Solution *****");

            for (int i = 0; i < n; i++) {

                for (int j = 0; j < n; j++) {

                    System.out.print(chessBoard[i][j] + " ");

                }

                System.out.println();

            }

        } else {

            // we know that each queen can be placed on each row

            int i = placed;

            // iterate through the columns

            for (int j = 0; j < n; j++) {

                if (chessBoard[i][j] != 1) {

                    if (isSafe(i, j)) {

                        chessBoard[i][j] = 1;

             


不负相思意
浏览 77回答 2
2回答

慕娘9325324

问题是:int slope = Math.abs((queens.get(i).row - row) / (queens.get(i).column - column));if (slope == 1) {&nbsp; &nbsp; return false;}您正在转换slope为整数。这意味着即使皇后实际上不在该对角线上,斜率1.5或1.3变为1并导致您返回。false而是在除法之前转换为浮点数(请注意,java的除法是整数除法,因此您需要先将除数或被除数转换为浮点数以获得浮点输出)以允许浮点斜率:float tmp = (queens.get(i).row - row);float slope = Math.abs(tmp/ (queens.get(i).column - column));if (slope == 1) {&nbsp; &nbsp; return false;}

慕勒3428872

的替代解决方案isSafe()和class Queen让棋盘成为一个跟踪状态的类采取克隆当前板子状态设置女王并阻止她可以到达的所有领域将克隆向下传递到下一行记住每个皇后的每行列位置placer以下是在闭包中传递的通用求解器。placeRook()通过使用该方法,可以很容易地对车 ( )、骑士 (&nbsp;placeKnight()) 或主教 ( )使用相同的求解器placeBishop()。请注意,我的解决方案是用 Groovy 编写的,它也在 JVM 上运行,并且非常接近 Java。所以将算法的多汁位翻译成Java应该没有问题。class ChessBoard {&nbsp; &nbsp; int N&nbsp; &nbsp; int lastIndex&nbsp; &nbsp; private boolean[][] board&nbsp; &nbsp; int solutions&nbsp; &nbsp; ChessBoard(int n) {&nbsp; &nbsp; &nbsp; &nbsp; board&nbsp; &nbsp; &nbsp;= new boolean[n][n]&nbsp; &nbsp; &nbsp; &nbsp; N&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= n&nbsp; &nbsp; &nbsp; &nbsp; lastIndex = n - 1&nbsp; &nbsp; &nbsp; &nbsp; solutions = 0&nbsp; &nbsp; &nbsp; &nbsp; this.each { int row, int column -> board[row][column] = true }&nbsp; &nbsp; }&nbsp; &nbsp; ChessBoard(ChessBoard orig) {&nbsp; &nbsp; &nbsp; &nbsp; N&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= orig.getN()&nbsp; &nbsp; &nbsp; &nbsp; board&nbsp; &nbsp; &nbsp;= new boolean[N][N]&nbsp; &nbsp; &nbsp; &nbsp; lastIndex = N - 1&nbsp; &nbsp; &nbsp; &nbsp; solutions = 0&nbsp; &nbsp; &nbsp; &nbsp; this.each { int row, int column -> board[row][column] = orig.getField(row, column) }&nbsp; &nbsp; }&nbsp; &nbsp; void each(Closure c) {&nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { row ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { column -> c(row, column) }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; void print() {&nbsp; &nbsp; &nbsp; &nbsp; println " ${'-' * N}"&nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { row ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print "|"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { column -> print "${board[row][column] ? ' ' : 'X'}" }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println "|"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; println " ${'-' * N}"&nbsp; &nbsp; }&nbsp; &nbsp; int getN()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { return N }&nbsp; &nbsp; int getSolutions()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { return solutions }&nbsp; &nbsp; boolean getField(int row, int column) { return board[row][column] }&nbsp; &nbsp; void blockField(int row, int column)&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ((row < 0) || (row > lastIndex))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; if ((column < 0) || (column > lastIndex))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; board[row][column] = false&nbsp; &nbsp; }&nbsp; &nbsp; List<Integer> getFree(int row)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).findResults { int column -> board[row][column] ? column : null }&nbsp; &nbsp; }&nbsp; &nbsp; void placeQueen(int row, int column, boolean all = true) {&nbsp; &nbsp; &nbsp; &nbsp; if (all) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { offset ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blockField(row,&nbsp; &nbsp; offset)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blockField(offset, column)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // column&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blockField(row + offset, column + offset) // diagonals&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blockField(row + offset, column - offset)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blockField(row - offset, column + offset)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blockField(row - offset, column - offset)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blockField(row, column)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // recursive solver&nbsp; &nbsp; void solve(ChessBoard previous, List<Integer> columns, int row, Closure placer) {&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> free = previous.getFree(row)&nbsp; &nbsp; &nbsp; &nbsp; if (row < lastIndex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // recurse&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free.each { column ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChessBoard work = new ChessBoard(previous)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columns[row] = column&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placer(work, row, column, true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; solve(work, columns, row + 1, placer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // solutions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free.each { column ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChessBoard solution = new ChessBoard(N)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columns[row] = column&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { placer(solution, it, columns[it], false) }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println "Solution #${++solutions}:"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; solution.print()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // start recursion&nbsp; &nbsp; void solve(Closure placer) {&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> columns = []&nbsp; &nbsp; &nbsp; &nbsp; solve(this, columns, 0, placer)&nbsp; &nbsp; }}board = new ChessBoard(8)board.solve { ChessBoard work, int row, int column, boolean all -> work.placeQueen(row, column, all) }println "Solutions: ${board.getSolutions()}"测试运行:Solution #1:&nbsp;--------|X&nbsp; &nbsp; &nbsp; &nbsp;||&nbsp; &nbsp; X&nbsp; &nbsp;||&nbsp; &nbsp; &nbsp; &nbsp;X||&nbsp; &nbsp; &nbsp;X&nbsp; ||&nbsp; X&nbsp; &nbsp; &nbsp;||&nbsp; &nbsp; &nbsp; X || X&nbsp; &nbsp; &nbsp; ||&nbsp; &nbsp;X&nbsp; &nbsp; |&nbsp;--------...Solution #92:&nbsp;--------|&nbsp; &nbsp; &nbsp; &nbsp;X||&nbsp; &nbsp;X&nbsp; &nbsp; ||X&nbsp; &nbsp; &nbsp; &nbsp;||&nbsp; X&nbsp; &nbsp; &nbsp;||&nbsp; &nbsp; &nbsp;X&nbsp; || X&nbsp; &nbsp; &nbsp; ||&nbsp; &nbsp; &nbsp; X ||&nbsp; &nbsp; X&nbsp; &nbsp;|&nbsp;--------Solutions: 92如果我没记错的话,对于 8-Queen 问题来说,92 听起来确实是正确的。但是自从我在学校使用 Pascal 中的迭代方法解决这个问题以来已经超过 35 年了 :-)更新改进的解决方案将原始类拆分ChessBoard为跟踪状态和Solver算法皇后、白嘴鸦、主教和骑士的砂矿计算尺寸 1 到 8 的解决方案为结果生成降价表class ChessBoard {&nbsp; &nbsp; private int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;N&nbsp; &nbsp; private int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lastIndex&nbsp; &nbsp; private boolean[][] state&nbsp; &nbsp; ChessBoard(int n) {&nbsp; &nbsp; &nbsp; &nbsp; N&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= n&nbsp; &nbsp; &nbsp; &nbsp; lastIndex = N - 1&nbsp; &nbsp; &nbsp; &nbsp; state&nbsp; &nbsp; &nbsp;= new boolean[N][N]&nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { row ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { column ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setField(row, column, true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ChessBoard(ChessBoard orig) {&nbsp; &nbsp; &nbsp; &nbsp; N&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= orig.getN()&nbsp; &nbsp; &nbsp; &nbsp; lastIndex = N - 1&nbsp; &nbsp; &nbsp; &nbsp; state&nbsp; &nbsp; &nbsp;= new boolean[N][N]&nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { row ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { column ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setField(row, column, orig.getField(row, column))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; int getN() {&nbsp; &nbsp; &nbsp; &nbsp; return N&nbsp; &nbsp; }&nbsp; &nbsp; boolean getField(int row, int column) {&nbsp; &nbsp; &nbsp; &nbsp; return state[row][column]&nbsp; &nbsp; }&nbsp; &nbsp; void setField(int row, int column, boolean free = false)&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ((row < 0) || (row > lastIndex))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; if ((column < 0) || (column > lastIndex))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; state[row][column] = free&nbsp; &nbsp; }&nbsp; &nbsp; List<Integer> getFree(int row) {&nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findResults { int column ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getField(row, column) ? column : null&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // for debugging only&nbsp; &nbsp; void print() {&nbsp; &nbsp; &nbsp; &nbsp; println " ${'-' * N}"&nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { row ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print "|"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { column -> print "${getField(row, column) ? ' ' : 'X'}" }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println "|"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; println " ${'-' * N}"&nbsp; &nbsp; }}class Solver {&nbsp; &nbsp; private int&nbsp; &nbsp;N&nbsp; &nbsp; private int&nbsp; &nbsp;lastIndex&nbsp; &nbsp; private int&nbsp; &nbsp;solutions&nbsp; &nbsp; private int[] columns&nbsp; &nbsp; Solver(int n) {&nbsp; &nbsp; &nbsp; &nbsp; N&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= n&nbsp; &nbsp; &nbsp; &nbsp; lastIndex = N - 1&nbsp; &nbsp; &nbsp; &nbsp; solutions = 0&nbsp; &nbsp; &nbsp; &nbsp; columns&nbsp; &nbsp;= new int[N]&nbsp; &nbsp; }&nbsp; &nbsp; void printSolution(String label) {&nbsp; &nbsp; &nbsp; &nbsp; solutions++&nbsp; &nbsp; &nbsp; &nbsp; if (!label)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; println "${N}-${label} solution #${solutions}"&nbsp; &nbsp; &nbsp; &nbsp; println " ${'-' * N}"&nbsp; &nbsp; &nbsp; &nbsp; (0..lastIndex).each { row ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int column = columns[row]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println "|${' ' * column}X${' ' * (lastIndex - column)}|"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; println " ${'-' * N}"&nbsp; &nbsp; }&nbsp; &nbsp; int getSolutions() {&nbsp; &nbsp; &nbsp; &nbsp; return solutions&nbsp; &nbsp; }&nbsp; &nbsp; void placeQueen(ChessBoard board, int row, int column) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // only modify fields from (row+1) downwards&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (1..(lastIndex - row)).each { offset ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; board.setField(row + offset, column)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // column&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; board.setField(row + offset, column + offset) // diagonals&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; board.setField(row + offset, column - offset)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; void placeRook(ChessBoard board, int row, int column) {&nbsp; &nbsp; &nbsp; &nbsp; // only modify fields from (row+1) downwards&nbsp; &nbsp; &nbsp; &nbsp; (1..(lastIndex - row)).each { offset ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; board.setField(row + offset, column) // column&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; void placeBishop(ChessBoard board, int row, int column) {&nbsp; &nbsp; &nbsp; &nbsp; // only modify fields from (row+1) downwards&nbsp; &nbsp; &nbsp; &nbsp; (1..(lastIndex - row)).each { offset ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; board.setField(row + offset, column + offset) // diagonals&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; board.setField(row + offset, column - offset)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static void placeKnight(ChessBoard board, int row, int column) {&nbsp; &nbsp; &nbsp; &nbsp; // only modify fields from (row+1) downwards&nbsp; &nbsp; &nbsp; &nbsp; board.setField(row + 1, column - 2)&nbsp; &nbsp; &nbsp; &nbsp; board.setField(row + 1, column + 2)&nbsp; &nbsp; &nbsp; &nbsp; board.setField(row + 2, column - 1)&nbsp; &nbsp; &nbsp; &nbsp; board.setField(row + 2, column + 1)&nbsp; &nbsp; }&nbsp; &nbsp; // recursive solver&nbsp; &nbsp; void solve(ChessBoard previous, int row, Closure placer, String label) {&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> free = previous.getFree(row)&nbsp; &nbsp; &nbsp; &nbsp; if (row < lastIndex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // recurse&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free.each { column ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChessBoard work = new ChessBoard(previous)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columns[row] = column&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placer(this, work, row, column)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; solve(work, row + 1, placer, label)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // solutions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free.each { column ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columns[row] = column&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printSolution(label)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // start recursion&nbsp; &nbsp; int solve(Closure placer, String label = null) {&nbsp; &nbsp; &nbsp; &nbsp; solve(new ChessBoard(N), 0, placer, label)&nbsp; &nbsp; &nbsp; &nbsp; return solutions&nbsp; &nbsp; }}Map<String, Closure> placers = [&nbsp; &nbsp; 'Queens':&nbsp; { Solver solver, ChessBoard board, int row, int column -> solver.placeQueen(board,&nbsp; row, column) },&nbsp; &nbsp; 'Rooks':&nbsp; &nbsp;{ Solver solver, ChessBoard board, int row, int column -> solver.placeRook(board,&nbsp; &nbsp;row, column) },&nbsp; &nbsp; 'Bishops': { Solver solver, ChessBoard board, int row, int column -> solver.placeBishop(board, row, column) },&nbsp; &nbsp; 'Knights': { Solver solver, ChessBoard board, int row, int column -> solver.placeKnight(board, row, column) },]Map<String, List<Integer>> solutions = [:]// generate solutions up to maxNint&nbsp; &nbsp; &nbsp;maxN&nbsp; = 8boolean print = falseplacers&nbsp; &nbsp; .keySet()&nbsp; &nbsp; .each { String key ->&nbsp; &nbsp; &nbsp; &nbsp; Closure placer = placers[key]&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> results = []&nbsp; &nbsp; &nbsp; &nbsp; (1..maxN).each { N ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.push(new Solver(N).solve(placer, print ? key : null))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; solutions[key] = results&nbsp; &nbsp; }// generate markdown table from solutionsList lines = []&nbsp; &nbsp; (0..maxN).each { lines[it] = [it ?: 'Size'] }[&nbsp; &nbsp; 'Queens',&nbsp; &nbsp; 'Rooks',&nbsp; &nbsp; 'Bishops',&nbsp; &nbsp; 'Knights',].each { key ->&nbsp; &nbsp; List<Integer> results = solutions[key]&nbsp; &nbsp; lines[0].push(key)&nbsp; &nbsp; (1..maxN).each { lines[it].push(results[it - 1]) }}lines.each { line -> println line.join('|') }return结果表:| Size | Queens | Rooks | Bishops | Knights ||------|--------|-------|---------|---------||&nbsp; &nbsp;1&nbsp; |&nbsp; &nbsp; &nbsp; 1 |&nbsp; &nbsp; &nbsp;1 |&nbsp; &nbsp; &nbsp; &nbsp;1 |&nbsp; &nbsp; &nbsp; &nbsp;1 ||&nbsp; &nbsp;2&nbsp; |&nbsp; &nbsp; &nbsp; 0 |&nbsp; &nbsp; &nbsp;2 |&nbsp; &nbsp; &nbsp; &nbsp;2 |&nbsp; &nbsp; &nbsp; &nbsp;4 ||&nbsp; &nbsp;3&nbsp; |&nbsp; &nbsp; &nbsp; 0 |&nbsp; &nbsp; &nbsp;6 |&nbsp; &nbsp; &nbsp; &nbsp;5 |&nbsp; &nbsp; &nbsp; &nbsp;9 ||&nbsp; &nbsp;4&nbsp; |&nbsp; &nbsp; &nbsp; 2 |&nbsp; &nbsp; 24 |&nbsp; &nbsp; &nbsp; 24 |&nbsp; &nbsp; &nbsp; 52 ||&nbsp; &nbsp;5&nbsp; |&nbsp; &nbsp; &nbsp;10 |&nbsp; &nbsp;120 |&nbsp; &nbsp; &nbsp;125 |&nbsp; &nbsp; &nbsp;451 ||&nbsp; &nbsp;6&nbsp; |&nbsp; &nbsp; &nbsp; 4 |&nbsp; &nbsp;720 |&nbsp; &nbsp; &nbsp;796 |&nbsp; &nbsp; 4898 ||&nbsp; &nbsp;7&nbsp; |&nbsp; &nbsp; &nbsp;40 |&nbsp; 5040 |&nbsp; &nbsp; 5635 |&nbsp; &nbsp;67381 ||&nbsp; &nbsp;8&nbsp; |&nbsp; &nbsp; &nbsp;92 | 40320 |&nbsp; &nbsp;48042 | 1131382 ||------|--------|-------|---------|---------|
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java