检测N- Queens问题中解决方案数量的误差
编程问题描述
n-Queens 难题是将
n
Queens放在nx N
棋盘上的问题,因此没有两个皇后区相互攻击。给定 整数n
,返回 n-Queens难题 。1≤n≤9
是测试用例的约束。
[取自在这里]
我的尝试
我尝试使用位掩盖来解决问题。简而言之,我们尝试所有可能的组合,并在无法解决方案时回溯。
我们将女王放置划行,在每个位置时,我们都会限制某些位置/盒子,剩下的皇后区都无法放置。
现在,我们可以通过它的列 index
,对角具有相同行索引 - 列索引 - 列索引
, 抗Diagonal 具有相同的行索引 +列索引
的属性。
因此,在将女王放在任何盒子上后,我们将限制该盒子确定的柱,对角线和反对道子。这将通过为所有三个参数具有位掩码变量来完成。
这是相同的c
中的代码(该代码是在在线法官上提交的)。
int N;
int count=0;
void rowExpansion(int r, int cols, int diags, int aDiags);
int totalNQueens(int n)
{
N=n;
rowExpansion(0,0,0,0);
return count;
}
void rowExpansion(int r, int cols, int diags, int aDiags)
{
if (r<N)
{
for (register int c=0; c<N; c++)
{
// current Diagonal, current antidiagonal
int cD = r - c + N, cAd= r + c;
/* Check if (r,c) is valid, Checking ith bits of Three Bit Masks.
If any of them is set, don't include this (r,c) */
if ((cols & (1 << c)) || (diags & (1 << cD)) || (aDiags & (1 << cAd)))
continue;
//Next Row traversal with updated bit-masks
rowExpansion(r+1, cols | 1<<c, diags | 1<<cD, aDiags | 1<<cAd);
}
}
else count++;
}
该代码在控制台上工作正常,但是在提交该代码时会产生错误。对于n = 1
,代码在控制台中给出了正确的输出,但在提交时给出了错误的答案。我尝试在python
中编码相同的技术,并且效果很好。
附件是错误的屏幕截图。
这是添加的main
和其他功能的相同代码> preprex ,它在
#include <stdio.h>
int N;
int count=0;
void rowExpansion(int r, int cols, int diags, int aDiags);
int totalNQueens(int n)
{
N=n;
rowExpansion(0,0,0,0);
return count;
}
void rowExpansion(int r, int cols, int diags, int aDiags)
{
if (r<N)
{
for (register int c=0; c<N; c++)
{
// current Diagonal, current antidiagonal
int cD = r - c + N, cAd= r + c;
/* Check if (r,c) is valid, Checking ith bits of Three Bit Masks.
If any of them is set, don't include this (r,c) */
if ((cols & (1 << c)) || (diags & (1 << cD)) || (aDiags & (1 << cAd)))
continue;
//Next Row traversal with updated bit-masks
rowExpansion(r+1, cols | 1<<c, diags | 1<<cD, aDiags | 1<<cAd);
}
}
else count++;
}
void main()
{
int n;
printf("Enter Number of Queens (1-9) : ");
scanf("%d",&n);
if (n<1 || n>9)
{
printf("Wrong Input!");
}
else
{
int D[] = {0, 1, 0, 0, 2, 10, 4, 40, 92, 352};
int x = totalNQueens(n);
printf("Desired Output : %d\nGiven Output : %d\n", D[n],x);
}
}
作为背景,我主要在python
中练习,而在c
编程中的熟练程度不太熟练。
怀疑
- 此代码中有什么错误?错误是逻辑,句法还是运行时错误?
- 为什么会发生相同的代码在 console 上是成功的,但在上失败了?有人可以为此提供良好的参考吗?
- 注释中的用户指责该错误的全局变量。有人能否阐明为什么会发生这种情况,并提供有关如何摆脱此代码中全球变量的提示?
Programming Problem Description
The n-queens puzzle is the problem of placing
n
queens on ann x n
chessboard such that no two queens attack each other. Given an
integern
, return the number of distinct solutions to the
n-queens puzzle. The1 ≤ n ≤ 9
are the constraints of Test Cases.
[Taken from here]
My Attempt
I tried to solve the problem using bit-masking. In short, we try all possible combination, and backtrack whenever solution is not possible.
We place queen row-by-row, and with each placement, we restrict some positions/box where remaining queens cannot be placed.
Now, we can identify each column by it's index
, diagonal have property of same row index - column index
, and anti-diagonal have property of same row index + column index
.
Thus, after placing queen at any box, we will restrict columns, diagonals and anti-diagonals identified by that box. This will be done by having a bit-mask variable for all three parameters.
Here is the code in c
for the same (which was submitted on Online Judge).
int N;
int count=0;
void rowExpansion(int r, int cols, int diags, int aDiags);
int totalNQueens(int n)
{
N=n;
rowExpansion(0,0,0,0);
return count;
}
void rowExpansion(int r, int cols, int diags, int aDiags)
{
if (r<N)
{
for (register int c=0; c<N; c++)
{
// current Diagonal, current antidiagonal
int cD = r - c + N, cAd= r + c;
/* Check if (r,c) is valid, Checking ith bits of Three Bit Masks.
If any of them is set, don't include this (r,c) */
if ((cols & (1 << c)) || (diags & (1 << cD)) || (aDiags & (1 << cAd)))
continue;
//Next Row traversal with updated bit-masks
rowExpansion(r+1, cols | 1<<c, diags | 1<<cD, aDiags | 1<<cAd);
}
}
else count++;
}
This code was working fine on Console, but on submitting it produced error. For n=1
, code was giving correct output in Console, but giving wrong answer on submitting. I tried to code same technique in python
and it worked fine.
Attached is the screenshot of error.
Here is the same code with added main
and other functionalities to make it reprex, it gave correct output on CodeBlocks.
#include <stdio.h>
int N;
int count=0;
void rowExpansion(int r, int cols, int diags, int aDiags);
int totalNQueens(int n)
{
N=n;
rowExpansion(0,0,0,0);
return count;
}
void rowExpansion(int r, int cols, int diags, int aDiags)
{
if (r<N)
{
for (register int c=0; c<N; c++)
{
// current Diagonal, current antidiagonal
int cD = r - c + N, cAd= r + c;
/* Check if (r,c) is valid, Checking ith bits of Three Bit Masks.
If any of them is set, don't include this (r,c) */
if ((cols & (1 << c)) || (diags & (1 << cD)) || (aDiags & (1 << cAd)))
continue;
//Next Row traversal with updated bit-masks
rowExpansion(r+1, cols | 1<<c, diags | 1<<cD, aDiags | 1<<cAd);
}
}
else count++;
}
void main()
{
int n;
printf("Enter Number of Queens (1-9) : ");
scanf("%d",&n);
if (n<1 || n>9)
{
printf("Wrong Input!");
}
else
{
int D[] = {0, 1, 0, 0, 2, 10, 4, 40, 92, 352};
int x = totalNQueens(n);
printf("Desired Output : %d\nGiven Output : %d\n", D[n],x);
}
}
As background, I use to practice mostly in python
and not very much skilled in c
programming.
Doubt(s)
- What is the error in this code? Is the error logical, syntactical or runtime-error?
- Why it happens that same code on console is successful, but fails on submitting? Can someone provide good reference for the same?
- Users in comments blamed Global Variables for the error. Can someone throw more light on why this happen, and provide hint on how to get rid of global variables from this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要问题是如何使用变量
计数
。如果函数
totalnqueens
被称为多次,则它只是累积了计数,因为count
永远不会重置。OP提及的Python代码可以按预期进行“工作”,但没有提供,因此我们只能猜测它没有此错误。
我建议首先不要使用全局变量。 是可能的替代实现。
请注意,我还使用了
unsigned
类型来执行所有位操作。The main problem is how the variable
count
is used.If the function
totalNQueens
is called more than once, it just accumulates the count, becausecount
is never resetted.The OP mention a Python code that "works" as expected, but it's not provided, so we can only guess that it doesn't have this bug.
I'd suggest not to use a global variable, in the first place. The following is a possible alternative implementation.
Note that I also used an
unsigned
type to perform all the bit manipulations.重写没有外部变量的代码(
n
和count
)会导致在线判断接受它。Rewriting the code with no external variables (
N
andcount
) results in the online judging accepting it.