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;
        }
    }
  • 相关阅读:
    float:left的妙用
    ajax实现注册并选择头像后上传
    Javascript中target事件属性,事件的目标节点的获取。
    Bootstrap解决页面缩小变形的办法
    UICollectionView布局功能
    UITaleView的基础使用及数据展示操作
    scrollView实现基础中心点缩放及与UIPageControl结合使用
    自定义UITableViewCell
    属性传值,协议传值,block传值,单例传值四种界面传值方式
    操作符:一元操作符、布尔值操作符(逻辑操作符)、加性操作符、乘性操作符、关系操作符、相等操作符、条件操作符以及赋值操作符
  • 原文地址:https://www.cnblogs.com/airwindow/p/4851863.html
Copyright © 2011-2022 走看看