zoukankan      html  css  js  c++  java
  • [LeetCode] 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.."]
    ]
    
    分析:1. 主要思路见N皇后问题的两个最高效的算法 。主要代码与博主的第一段非递归代码类似。原博的第32行判断似乎有问题。这是回溯法的非递归版本。
     
     1 class Solution {
     2 public:
     3     vector<vector<string> > result;
     4     int queen_num_;
     5     vector<vector<string>> solveNQueens(int n) {
     6         vector<int> a(n, -1);
     7         queen_num_ = n;
     8         solveNQueens(a);
     9         
    10         return result;
    11     }
    12     
    13     bool IsValid(const vector<int>& a, int row, int col) {
    14         for (int i = 0; i < row; i++) {
    15             if (col == a[i] || abs(i - row) == abs(a[i] - col))
    16                 return false;
    17         }
    18 
    19         return true;
    20     }
    21 
    22     void solveNQueens(vector<int>& a) {
    23         int i = 0, j = 0;
    24         while (i < queen_num_) {
    25             while (j < queen_num_) {
    26                 if (IsValid(a, i, j)) {
    27                     a[i] = j;
    28                     j = 0;
    29                     break;
    30                 } else {
    31                     j++;
    32                 }
    33             }
    34 
    35             if (a[i] == -1) {
    36                 if (i == 0) {
    37                     return;
    38                 } else {
    39                     i--;
    40                     j = a[i] + 1;
    41                     a[i] = -1;
    42                     continue;
    43                 }
    44             }
    45 
    46             if (i == queen_num_  - 1) {
    47                 string s(queen_num_, '.');
    48                 vector<string> sol(queen_num_, s);
    49 
    50                 for (int i = 0; i < queen_num_; i++) 
    51                     sol[i][a[i]] = 'Q';
    52 
    53                 result.push_back(sol);
    54                 j = a[i] + 1;
    55                 a[i] = -1;
    56                 continue;
    57             }
    58          i++;
    59         }//while (i < queen_num_);
    60     }//solveNQueens
    61 };

      2. 回溯法的递归版本,使用了剪枝函数。参考资料:http://www.cnblogs.com/wuyuegb2312/p/3273337.html#intro

    class Solution {
    public:
        int queen_num_;
        vector<vector<string> > result;
        
        vector<vector<string>> solveNQueens(int n) {
            vector<int> a(n, -1);
            queen_num_ = n;
            solveNQueens(a, 0);
            
            return result;
        }
        
        bool IsValid(vector<int>& a, int row, int col) {
            int i;
            for (i = 0; i < row; i++) {
                if (col == a[i]|| abs(i - row) == abs(a[i] - col))
                    return false;
            }
            
            return true;
        }
        
        void ProcessSolution(vector<int> &a) {
            string s(queen_num_, '.');
            vector<string> sol(queen_num_, s);
            
            for (int i = 0; i < queen_num_; i++) {
                sol[i][a[i]] = 'Q';
            }
            
            result.push_back(sol);
        }
        
        bool IsSolution(int row) {
            return row == queen_num_;
        }
        
        void solveNQueens(vector<int>& a, int row) {
            if (IsSolution(row)){
                ProcessSolution(a);
            } else {
                int col;
                for (col = 0; col < queen_num_; col++) {
                    if (!IsValid(a, row, col))  //剪枝函数
                        continue;
                    a[row] = col;
                    solveNQueens(a, row + 1);
                    a[row] = -1;
                }
            }
        }
    };
  • 相关阅读:
    神不在的星期天
    炸弹问题——一种会引发死锁的情景模式
    JavaScript的类和继承
    没有功劳也有苦劳
    使用http代理服务器解决本地客户端页面的跨域AJAX问题
    04数组 字符数组
    01 数据类型 、常量变量、运算符、表达式、格式化输入输出
    03循环结构
    .NET编译、WOW64注册表访问、同时在X86和X64平台上部署应用程序
    Vistual Studio 2008中的智能感应功能
  • 原文地址:https://www.cnblogs.com/vincently/p/4714230.html
Copyright © 2011-2022 走看看