Algorithms : N-Queens problem with BackTracking Algorithms

N皇后问题要求在n×n棋盘上放置n个皇后,使得它们互不攻击。给定一个整数n,返回n皇后问题的不同解决方案数量。例如,当n为4时,有两个不同解。时间复杂度为O(n!),本文探讨了使用回溯算法解决此问题的方法。
摘要由CSDN通过智能技术生成

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 the number of distinct solutions to the n-queens puzzle.

Input: 4
Output: 2
Explanation: There are two distinct solutions to the 4-queens puzzle as shown below.
[
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]

 

class Solution {
private:
    bool checkConflict(int row, int col, vector<int> & columns) {
        for (int r = 0; r < row; ++r) {
            // a the same column of at the diagonal
            if(columns[r] == col || row - r == abs(columns[r] - col))
                return false;
        }
        return true;
    }
    
    void backTracking(int row, int n, vector<int>& columns, int & count) {
        if(row == n) {
            count++;
            return;
        }
        
        // try to place che queen in the current row and check whether
        // it is valid.
        for(int col = 0; col < n; ++col) {
            columns[row] = col;
            
            if(checkConflict(row, col, columns))
                backTracking(row + 1, n, columns, count);
            
            columns[row] = -1;
        }
    }
public:
    int totalNQueens(int n) {
        int count = 0;
        vector<int> columns(n, 0);
        backTracking(0, n, columns, count);
        return count;
    }
};

The Time complexity

Assume T(N) is the time complexity for N Queen problem

It neet n*T(n-1) +n*n.

That is T(n) = n*T(n-1) +n^2

then T(n) = n*(n-1)*T(n-2) + n^2 + n*(n-1)^2

...T(n) = n*(n-1)*(n-2)*...*T(0) +  = O(n!)

 

A final test solution:

#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>
#include <stack>

using namespace std;

class Solution {
private:
    bool checkConflict(int row, int col, vector<int> & columns) {
        for (int r = 0; r < row; ++r) {
            // a the same column of at the diagonal
            if(columns[r] == col || row - r == abs(columns[r] - col))
                return false;
        }
        return true;
    }
    
    void backTracking(int row, int n, vector<int>& columns, 
                      vector<vector<int>> & results) {
        if(row == n) {
            results.push_back(columns);
            return;
        }
        
        // try to place che queen in the current row and check whether
        // it is valid.
        for(int col = 0; col < n; ++col) {
            columns[row] = col;
            
            if(checkConflict(row, col, columns)) {
                backTracking(row + 1, n, columns, results);
            }
            
            columns[row] = -1;
        }
    }
public:
    vector<vector<int>> totalNQueens(int n) {
        vector<vector<int>> results;
        vector<int> columns(n, 0);
        backTracking(0, n, columns, results);
        return results;
    }

    void printQueen(int pos, int Total) {
        for(int i = 0; i < Total; ++i) {
            cout << (i == pos ? 'X' : ' ');
        }
        cout << endl;
    }
    void printResult(vector<vector<int>> & results) {
        for(auto & solution : results) {
            cout << "-----------------------------" << endl;
            int n = solution.size();
            for(int i = 0; i < n; ++i)
                printQueen(solution[i], n);
            cout << "-----------------------------" << endl;
        }
    }
};

int main()
{
    Solution sol;

    auto result = sol.totalNQueens(5);
    sol.printResult(result);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值