zoukankan      html  css  js  c++  java
  • leetcode — n-queens

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * Source : https://oj.leetcode.com/problems/n-queens/
     *
     * Created by lverpeng on 2017/7/18.
     *
     * The n-queens puzzle is the problem of placing n queens on an n×n chessboard
     * such that no two queens attack each other.
     *
     * Given an integer n, return all distinct solutions to the n-queens puzzle.
     *
     * Each solution contains a distinct board configuration of the n-queens' placement,
     * where 'Q' and '.' both indicate a queen and an empty space respectively.
     *
     * For example,
     * There exist two distinct solutions to the 4-queens puzzle:
     *
     * [
     *  [".Q..",  // Solution 1
     *   "...Q",
     *   "Q...",
     *   "..Q."],
     *
     *  ["..Q.",  // Solution 2
     *   "Q...",
     *   "...Q",
     *   ".Q.."]
     * ]
     *
     */
    public class NQueens {
    
    
        public List<String> solve (int n) {
            int[][] board = new int[n][n];
            List<String> result = new ArrayList<String>();
            revursion(board, 0, result);
            return result;
        }
    
        /**
         * n皇后问题,皇后所在位置的行、列、对角线都不能有其他皇后存在
         * 使用递归解决
         *
         * @param board
         * @param row
         * @param result
         */
        public void revursion (int[][] board, int row, List<String> result) {
            if (row == board.length) {
                // 找到解
                StringBuilder stringBuilder = new StringBuilder();
                if (result.size() > 0) {
                    stringBuilder.append("
    ");
                }
                stringBuilder.append("[");
                for (int i = 0; i < board.length; i++) {
                    stringBuilder.append(""");
                    for (int j = 0; j < board.length; j++) {
                        if (board[i][j] == 1) {
                            stringBuilder.append("Q");
                        } else {
                            stringBuilder.append(".");
                        }
                    }
                    stringBuilder.append("",
    ");
                }
                stringBuilder = stringBuilder.delete(stringBuilder.length() - 3, stringBuilder.length());
                stringBuilder.append("]");
                result.add(stringBuilder.toString());
            }
    
            for (int i = 0; i < board.length ; i++) {
                if (isValiad (board, row, i)) {
                    board[row][i] = 1;
                    revursion(board, row + 1, result);
                    board[row][i] = 0;
                }
            }
    
        }
    
    
        private boolean isValiad (int[][] board, int row, int col) {
            for (int i = 0; i < row; i++) {
                if (board[i][col] == 1 || (col - row + i >= 0 && board[i][col - row + i] == 1) || (col + row - i < board.length && board[i][col + row - i] == 1)) {
                    return false;
                }
           }
           return true;
        }
    
    
        public static void main(String[] args) {
            NQueens nQueens = new NQueens();
            List<String> list = nQueens.solve(4);
            System.out.println("=======" + list.size() + "=======");;
            System.out.println(Arrays.toString(list.toArray(new String[list.size()])));
    
            list = nQueens.solve(8);
            System.out.println("=======" + list.size() + "=======");
            System.out.println(Arrays.toString(list.toArray(new String[list.size()])));
        }
    
    }
    
  • 相关阅读:
    spring 环绕通知 ProceedingJoinPoint 执行proceed方法的作用是什么
    SpringMVC之RequestContextHolder分析
    MySQL中索引不会被用到的情况
    使用Stream快速对List进行一些操作
    Vue中this.$refs[name].resetFields();的使用
    好看的字体
    转,javascript中call()、apply()、bind()的用法终于理解
    vue中的$props
    手机端页面自适应解决方案-rem布局
    查看项目里特定npm包的版本号
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7518778.html
Copyright © 2011-2022 走看看