zoukankan      html  css  js  c++  java
  • [LeetCode#51]N-Queens

    Problem:

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

    Analysis:

    This is a very very typical DFS and back tracking problem!!!
    But for this specific problem, we could sovle it with a magic skill, which could greatly save the space complexity and time complexity. 
    Typical thinking:
    During the search process, we try to place a queen onto a position(i, j), then we check this placement againt all other placements, so as to make sure ethe current placement is valid.
    The instant idea for this is "check the new placement againt all girds before it". Since the borad is a "n*n" matrix, we have to use O(n^2) time complexity of checking a single placement. 
    
    But this problem could be elegantly solved by following observation:
    Since we only have one queue at each row, why not we just record each row's queen's column index? Why should we care about the empty space, since we actually never use them. 
    
    Great!!!! Haha!
    Step 1: compress the sparse matrix into an array.
    int[] placed = new int[n];
    Note: placed[i] denotes the there is queue at (i, placed[i]).
    
    Step 2: define a proper base case! At here, only when a path is valid, we allow it to go deeper! Thus we don't need to worry if the placement is legal when we just reach the next level. (We just need to use typical pos to check iff we have already place all queens properly)
    ----------------------------------------------------
    if (pos == n) {
        List<String> item = new ArrayList<String> ();
        for (int i = 0; i < n; i++) {
            String row = "";
            for (int j = 0; j < n; j++) {
                if (placed[i] != j)
                    row += ".";
                else
                    row += "Q";
            }
            item.add(row);
        }
        ret.add(item);
    }
    Note: the result recover process is very very interesting~~~Powerful~~~haha!
    
    Step 3:
    How to check if a placement is valid?
    We check the current placement against all previous placement(only queens)
    for (int i = 0; i < row_no; i++) {
        if (col_no == placed[i] || (Math.abs(col_no - placed[i]) == row_no - i))
            return false;
    } 
    
    Note: Since we use an array of recording our placement information, we do not need to recover the state (for other placements at the same level)
    for (int j = 0; j < n; j++) {
        if (isValid(placed, pos, j)) {
            placed[pos] = j; //other placement would automatically override placed[pos]
            helper(placed, n, pos+1, ret);
        }
    }
    But if we use arraylist, we need to do
    state.add(i);
    ...
    state.remove(state.size()-1);

    Solution:

    public class Solution {
        public List<List<String>> solveNQueens(int n) {
            List<List<String>> ret = new ArrayList<List<String>> ();
            if (n <= 0)
                return ret;
            int[] placed = new int[n];
            helper(placed, n, 0, ret);
            return ret;
        }
        
        
        private void helper(int[] placed, int n, int pos, List<List<String>> ret) {
            if (pos == n) {
                List<String> item = new ArrayList<String> ();
                for (int i = 0; i < n; i++) {
                    String row = "";
                    for (int j = 0; j < n; j++) {
                        if (placed[i] != j)
                            row += ".";
                        else
                            row += "Q";
                    }
                    item.add(row);
                }
                ret.add(item);
            }
            for (int j = 0; j < n; j++) {
                if (isValid(placed, pos, j)) {
                    placed[pos] = j;
                    helper(placed, n, pos+1, ret);
                }
            }
        }
        
        
        private boolean isValid(int[] placed, int row_no, int col_no) {
            for (int i = 0; i < row_no; i++) {
                if (col_no == placed[i] || (Math.abs(col_no - placed[i]) == row_no - i))
                    return false;
            }   
            return true;
        }
    }
  • 相关阅读:
    【转】浅析Linux中的零拷贝技术
    我们使用 Kafka 生产者在发消息的时候我们关注什么(Python 客户端 1.01 broker)
    【转】Python 访问 HDFS
    HIVE 乱码以及 HUE SQL 语句兼容性的记录(遇到应该会一直更新)
    Kafka 深入核心参数配置
    【转】Hive 修改 table、column
    Redash(开源轻量级商业智能) 生产环境部署及实践 (without docker)
    【转】Linux开机启动管理---systemd使用
    Pyspark 最近使用的一些有趣姿势的梳理
    使用jquery操作select(获取选中option的值等)
  • 原文地址:https://www.cnblogs.com/airwindow/p/4851863.html
Copyright © 2011-2022 走看看