zoukankan      html  css  js  c++  java
  • 51. N-Queens

    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.."]
    ]

     什么时候跳过的改动

     if (!isValid(cols, colIndex)) {
                    continue;
                }
     public List<List<String>> solveNQueens(int n) {
             List<List<String>> results = new ArrayList<>();
            if (n <= 0) {
                return results;
            }
            List<Integer> list = new ArrayList<Integer>();
            search(results, list, n);
            return results;
        }
        
        /*
         * results store all of the chessboards
         * cols store the column indices for each row
         */
        private void search(List<List<String>> results,
                            List<Integer> cols,
                            int n) {
            if (cols.size() == n) {
                results.add(drawChessboard(cols));
                return;
            }
            
            for (int colIndex = 0; colIndex < n; colIndex++) {
                if (!isValid(cols, colIndex)) {
                    continue;
                }
                cols.add(colIndex);
                search(results, cols, n);
                cols.remove(cols.size() - 1);
            }
        }
        
        private ArrayList<String> drawChessboard(List<Integer> cols) {
            ArrayList<String> chessboard = new ArrayList<>();
            for (int i = 0; i < cols.size(); i++) {
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < cols.size(); j++) {
                    sb.append(j == cols.get(i) ? 'Q' : '.');
                }
                chessboard.add(sb.toString());
            }
            return chessboard;
        }
        
        private boolean isValid(List<Integer> cols, int column) {
            int row = cols.size();
            for (int rowIndex = 0; rowIndex < cols.size(); rowIndex++) {
                if (cols.get(rowIndex) == column || rowIndex + cols.get(rowIndex) == row + column || 
                    rowIndex - cols.get(rowIndex) == row - column) {
                    return false;
                }
                
            }
            return true;
    
        }
    }
    

     

  • 相关阅读:
    【Linux】freetds安装配置连接MSSQL
    【MySQL】Sysbench性能测试
    【MySQL】mysql buffer pool结构分析
    【MySQL】MySQL锁和隔离级别浅析一
    Spring Boot 1.4测试的改进
    Spring Boot 定时任务的使用
    linux:nohup 不生成 nohup.out的方法
    Spring Boot应用的后台运行配置
    深入理解Session与Cookie(一)
    学习Maven之Cobertura Maven Plugin
  • 原文地址:https://www.cnblogs.com/apanda009/p/7230107.html
Copyright © 2011-2022 走看看