N-Queens N皇后问题 (LeetCode 51)

题目:The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. (LeetCode 51)

《N-Queens N皇后问题 (LeetCode 51)》

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

 

参考框架:

class Solution {
public:
    vector<vector<string>> solveNQueens(int n) {
        //code
    }
};

 

1.西洋棋里皇后可以沿直线,斜线攻击。即要求把n个皇后在n×n的棋盘上不出现同行,同列,同斜线地摆放。

N皇后问题由八皇后问题延伸而来。N皇后问题仅当N=1或N>=4时有解。

2.N皇后问题是回溯法的经典案例,先试着用naive的回溯法解决这个问题。

 

to be continued

    原文作者:八皇后问题
    原文地址: https://www.cnblogs.com/us4ever/p/5352486.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞