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?