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()])));
        }
    
    }
    
  • 相关阅读:
    C# 打印多页tif
    页面动态加载js文件
    CPrintDialog 构造函数参数详解
    DEVMODE 结构体
    对C#对象的Shallow、Deep Cloning认识【转】
    PowerShell 启动应用程序【转】
    中文网页的字体
    css3自适应布局单位vw,vh你知道多少?
    微信小程序轮播图宽高计算
    更改wordpress的默认登录页面名称wp-login
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7518778.html
Copyright © 2011-2022 走看看