从Queens Iand到bicete几个月可以坐学步车几号车?

2132人阅读
数据结构和算法(112)
The n-queens puzzle is the problem of placing n queens on ann×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.
N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
经典的n皇后问题,可以参考任何一本算法书。一般是通过回溯法求解。这里给出一个没有专门优化过的解法。
事实上我考虑过这个问题,因为棋盘的对称性,应该可以有更好的基于群论的求解方法。这个就要咨询数学家了。
第二题只要消去具体的转化,只计算解的个数即可。当然希望性能更好一些。
class Solution {
vector&vector&string&&
void generate_solution(const vector&int&& queen)
vector&string&
for(int i = 0; i & queen.size(); ++i)
string l(queen.size(), '.');
l[queen[i]] = 'Q';
s.emplace_back(move(l));
// avoid copy!
solution.emplace_back(move(s));
void traverse_solution_space(vector&int&& queen, int placed, int total)
if (placed == total)
generate_solution(queen);
// try place the queens
for(int i = 0; i & queen.size(); ++i)
bool valid =
// verify the position
for(int j = 0; j & ++j)
if (i == queen[j] ||
// same column
abs(queen[j] - i) == placed - j) // diagonal attack
if (valid)
queen[placed] =
traverse_solution_space(queen, placed + 1, total);
vector&vector&string& & solveNQueens(int n) {
solution.clear();
vector&int& queen(n);
traverse_solution_space(queen, 0, n);
class Solution {
vector&int&
vector&int&
int valid_
void traverse_solution_space(int k, int n)
if (k == n)
for(int i = 0; i & ++i)
if (avail[i] == 0)
bool iok =
for(int j = 0; j & ++j)
if (abs(board[j] - i) == k - j)
avail[i] = 1;
board[k] =
traverse_solution_space(k + 1, n);
avail[i] = 0;
int totalNQueens(int n)
valid_solutions = 0;
board.resize(n);
avail.resize(n);
fill(begin(avail), end(avail), 0);
traverse_solution_space(0, n);
return valid_
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:150323次
积分:2992
积分:2992
排名:第8064名
原创:138篇
评论:26条
(1)(2)(3)(4)(82)(24)(1)(2)(2)(7)(4)(3)(4)有关科目的英语谜语,用2个以上的单词,或一个句子。如,I love learning about king and queens._百度知道
有关科目的英语谜语,用2个以上的单词,或一个句子。如,I love learning about king and queens.
Maths History
P.E谜底:History我只学过
提问者采纳
。。。。路过打酱油。。
提问者评价
其他类似问题
为您推荐:
其他3条回答
这个确实有点不明白哦
你在说什么都不明白
您可能关注的推广
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁厦门湖里区洪塘站到湖里教育局坐几路公交车i_百度知道
厦门湖里区洪塘站到湖里教育局坐几路公交车i
厦门湖里区洪塘站到湖里教育局坐几路公交车i
坐18路( 9站)到
下车,步行 340米,到厦门市湖里区教育局。
其他类似问题
为您推荐:
教育局的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 几个月可以坐学步车 的文章

 

随机推荐