[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;
            }
        }
    }
};

 

posted @ 2015-08-08 23:44  vincently  阅读(271)  评论(0编辑  收藏  举报