资源 · 指南或文章
N-Queens Problem
在 n 乘 n 的棋盘上放置 n 个互不攻击的皇后,并比较三种求解搜索方法。是编程面试中很好的回溯练习。
审核日期
打开原站: N-Queens Problem (在新标签页中打开原网站)添加到路线图
把这个资源放进你正在跟进的路线图。它会作为你自己的补充资源显示;路线图的步骤不会改变,也不计入你已记录的步骤。
来源内容语言:英语
The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general n queens problem of placing n non-attacking queens on an n×n chessboard, for which solutions exist for all natural numbers n with the exception of n=2 and n=3. For example, following is a solution for 4 Queen problem. The expected output is a binary matrix which has 1s for the blocks where queens are placed. For example following is the output matrix for above 4 queen solution.
PrepKit 概览
本概览由 PrepKit 编辑根据源代码仓库撰写并审核,并非资源本身的文字。
- 适合人群
- 来源未说明适用级别
- 形式
- 课程
涵盖内容
- 给出约束条件,指出 n=2 和 n=3 无解,并用二进制矩阵展示一个 4x4 的解
- 朴素的“生成再检验”方法的伪代码
- 逐列回溯的分步伪代码
- 使用 leftDiagonal、column 和 rightDiagonal 位掩码的位运算方法,并演示 N=4 和 N=8 的解读
使用建议
这是 PrepKit 的学习建议,并非来源的说法。
编写回溯版本并打印所有 4x4 的解,再用位掩码改写,确认两个版本在 n=8 时得到的解的数量一致。
范围与局限
位运算部分提到一个从未在页面上出现的递归函数,其示意图是外部图片。没有给出复杂度。
获取方式与形式
GitHub 上 trekhleb/javascript-algorithms(MIT)中的 README。英文,免费阅读。
PrepKit 于 2026年10月2日 审核。 资源来源提交: 85293e3e2b88
本概览的依据
- src/algorithms/uncategorized/n-queens/README.md
收录此条目的页面
订阅 PrepKit 通讯
可选。使用 PrepKit 并不需要订阅。