N-Queen Problem
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].
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the chessboard.
Output:
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.
Constraints:
1<=T<=10
1<=n<=10
Example:
Input
2
1
4
Output:
[1 ]
[2 4 1 3 ] [3 1 4 2 ]
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 10
bool isSafe(int chess[MAX][MAX],int r,int c,int no)
{
for(int i=0;i<c;i++)
if(chess[r][i])
return false ;
for(int i=r-1,j=c-1;i>=0&&j>=0;i--,j--)
if(chess[i][j])
return false ;
for(int i=r+1,j=c-1;i<no&&j>=0;i++,j--)
if(chess[i][j])
return false ;
return true ;
}
void PrintBoard(int chess[MAX][MAX],int row)
{
cout<<" [" ;
for(int i=0;i<row;i++)
for(int j=0;j<row;j++)
if(chess[j][i])
cout<<j+1<<" " ;
cout<<"]" ;
}
void NQueen(int chess[MAX][MAX],int col,int row,bool *state)
{
if(col==row)
{
PrintBoard(chess,row) ;
(*state)=true ;
}
for(int i=0;i<row;i++)
{
if(isSafe(chess,i,col,row))
{
chess[i][col]=1 ;
NQueen(chess,col+1,row,&(*state)) ;
chess[i][col]=0 ;
}
}
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
int chess[MAX][MAX] ;
for(int i=0;i<no;i++)
for(int j=0;j<no;j++)
chess[i][j]=0 ;
bool state=false ;
NQueen(chess,0,no,&state) ;
if(!state)
cout<<-1 ;
cout<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
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].
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the chessboard.
Output:
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.
Constraints:
1<=T<=10
1<=n<=10
Example:
Input
2
1
4
Output:
[1 ]
[2 4 1 3 ] [3 1 4 2 ]
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 10
bool isSafe(int chess[MAX][MAX],int r,int c,int no)
{
for(int i=0;i<c;i++)
if(chess[r][i])
return false ;
for(int i=r-1,j=c-1;i>=0&&j>=0;i--,j--)
if(chess[i][j])
return false ;
for(int i=r+1,j=c-1;i<no&&j>=0;i++,j--)
if(chess[i][j])
return false ;
return true ;
}
void PrintBoard(int chess[MAX][MAX],int row)
{
cout<<" [" ;
for(int i=0;i<row;i++)
for(int j=0;j<row;j++)
if(chess[j][i])
cout<<j+1<<" " ;
cout<<"]" ;
}
void NQueen(int chess[MAX][MAX],int col,int row,bool *state)
{
if(col==row)
{
PrintBoard(chess,row) ;
(*state)=true ;
}
for(int i=0;i<row;i++)
{
if(isSafe(chess,i,col,row))
{
chess[i][col]=1 ;
NQueen(chess,col+1,row,&(*state)) ;
chess[i][col]=0 ;
}
}
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
int chess[MAX][MAX] ;
for(int i=0;i<no;i++)
for(int j=0;j<no;j++)
chess[i][j]=0 ;
bool state=false ;
NQueen(chess,0,no,&state) ;
if(!state)
cout<<-1 ;
cout<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
Comments
Post a Comment