首页 > 其他 > 详细

N-Queens N皇后问题

时间:2016-04-04 19:32:54      阅读:235      评论:0      收藏:0      [点我收藏+]

题目: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皇后问题

原文:http://www.cnblogs.com/us4ever/p/5352486.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!