This commit is contained in:
Sombit Bose 2020-04-29 22:42:56 +05:30 committed by GitHub
parent b8bcd82c26
commit c76eb387ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,20 +2,15 @@
#define n 4 #define n 4
#define inc_loop(var, start, stop, step) for (int var=start; var <= stop; var+=step) #define inc_loop(var, start, stop, step) for (int var=start; var <= stop; var+=step)
#define dec_loop(var, start, stop, step) for (int var=start; var >= stop; var-=step) #define dec_loop(var, start, stop, step) for (int var=start; var >= stop; var-=step)
void PrintSol(int Board[n][n]) {
void PrintSol(int Board[n][n]) inc_loop(i,0,n-1,1) {
{
inc_loop(i,0,n-1,1)
{
inc_loop(j,0,n-1,1) inc_loop(j,0,n-1,1)
std::cout << Board[i][j] << " "; std::cout << Board[i][j] << " ";
std::cout << std::endl; std::cout << std::endl;
} }
std::cout << std::endl; std::cout << std::endl;
if (n%2==0 || (n%2==1 && Board[n/2+1][0]!=1)) if (n%2==0 || (n%2==1 && Board[n/2+1][0]!=1)){
{ inc_loop(i,0,n-1,1){
inc_loop(i,0,n-1,1)
{
dec_loop(j,n-1,0,1) dec_loop(j,n-1,0,1)
std::cout<<Board[i][j]<<" "; std::cout<<Board[i][j]<<" ";
std::cout<<std::endl; std::cout<<std::endl;
@ -48,8 +43,7 @@ void NQueenSol(int Board[n][n], int col) {
PrintSol(Board); PrintSol(Board);
return; return;
} }
inc_loop(i,0,n-1,1) inc_loop(i,0,n-1,1) {
{
if (CanIMove(Board, i, col)) { if (CanIMove(Board, i, col)) {
Board[i][col] = 1; Board[i][col] = 1;
NQueenSol(Board, col + 1); NQueenSol(Board, col + 1);
@ -60,10 +54,8 @@ void NQueenSol(int Board[n][n], int col) {
int main() { int main() {
int Board[n][n] = {0}; int Board[n][n] = {0};
if (n%2==0) if (n%2==0) {
{ inc_loop(i,0,n/2-1,1) {
inc_loop(i,0,n/2-1,1)
{
if (CanIMove(Board, i, 0)) { if (CanIMove(Board, i, 0)) {
Board[i][0] = 1; Board[i][0] = 1;
NQueenSol(Board,1); NQueenSol(Board,1);
@ -71,10 +63,8 @@ int main() {
} }
} }
} }
else else {
{ inc_loop(i,0,n/2,1) {
inc_loop(i,0,n/2,1)
{
if (CanIMove(Board, i,0)) { if (CanIMove(Board, i,0)) {
Board[i][0] = 1; Board[i][0] = 1;
NQueenSol(Board,1); NQueenSol(Board,1);