N-Queen Problem with given an integer n, print all distinct solutions to the n-queens puzzle. - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Tuesday, 21 July 2020

N-Queen Problem with given an integer n, print all distinct solutions to the n-queens puzzle.

N-Queen Problem with given an integer n, print all distinct solutions to the n-queens puzzle.

N-Queen Problem

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, print all distinct solutions to the n-queens puzzle. Each solution contains distinct board configurations of the n-queens’ placement, where the solutions are a permutation of [1,2,3..n] in increasing order, here the number in the ith place denotes that the ith-column queen is placed in the row with that number. For eg below figure represents a chessboard [3 1 4 2].

For each test case, output your solutions on one line where each solution is enclosed in square brackets '[', ']' separated by a space . The solutions are permutations of {1, 2, 3 …, n} in increasing order where the number in the ith place denotes the ith-column queen is placed in the row with that number, if no solution exists print -1.
For Input:
3
1
5
3
Your Output is:
[1 ] 
[1 3 5 2 4 ] [1 4 2 5 3 ] [2 4 1 3 5 ] [2 5 3 1 4 ] [3 1 4 2 5 ] [3 5 2 4 1 ] [4 1 3 5 2 ] [4 2 5 3 1 ] [5 2 4 1 3 ] [5 3 1 4 2 ] 
-1

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int board[11][11],f;// boards in which we are placing our queens
//checking if i can insert in a particular row or not
bool ispossible(int n,int row,int col)
{

    for(int i=row-1;i>=0;i--){
    if(board[i][col] == 1){
      return false;
    }
  }
  //Upper Left Diagonal
  for(int i=row-1,j=col-1;i>=0 && j>=0 ; i--,j--){
    if(board[i][j] ==1){
      return false;
    }
  }
  // Upper Right Diagonal
  for(int i=row-1,j=col+1;i>=0 && j<n; i--,j++){
    if(board[i][j]==1){
      return false;
    }
  }
  return true;
}
void nqueenhelper(int n,int row)
{
   
    if(row==n)
    {
    //if we reach next to the last row means we have sucessfully placed
//print the board matrix and we need to return
    int i=0,j=0;
    cout<<"[";
    while(i<n)
    {
        j=0;
        while(j<n)
        {
        if(board[i][j]==1)
        {
            f=1;
        cout<<j+1<<" ";
        }
        j++;
        }
        i++;
    }
    cout<<"] ";
   
    return ;
    }
//place at all possible positions and if placed move further
for(int j=0;j<n;j++)
{
    //we defined  a function ispossible which takes no of boards
    //row in which we want to insert and corresponding column
    if(ispossible(n,row,j))
    {
        board[row][j]=1;
//if possible than we move further and go for next row
    nqueenhelper(n,row+1);
    //for next solution
    board[row][j]=0;
    }
}

}


int main()
 {
     int test;
     cin>>test;
     while(test--)
     {
         int n;
         cin>>n;
         f=0;//if not possible any combination
        memset(board,0,sizeof(board);//we initialize our board to 0 initially
     //for placing in every row we use another helper function
     nqueenhelper(n,0);//initially row=0
     if(f==0)
     cout<<-1;
     cout<<endl;
     }

    return 0;
}