N-Queens N皇后问题
Posted us4ever
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了N-Queens N皇后问题相关的知识,希望对你有一定的参考价值。
题目: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)
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的回溯法解决这个问题。
以上是关于N-Queens N皇后问题的主要内容,如果未能解决你的问题,请参考以下文章