using System;
public class GFG
{
static int n;
static public void Main ()
{
var TC = Console.ReadLine();
int T = Convert.ToInt32(TC);
for(int a=0;a<T;a++)
{
var no= Console.ReadLine();
n = Convert.ToInt32(no);
int [,]board = new int [n,n];
InitializeBoardWithZeros(board,n,n);
SolveNQueen(board,0);
PrintBoard(board,n,n);
}
}
static public void InitializeBoardWithZeros(int [,]board,int row,int col)
{
for(int i=0;i<col;i++)
{
for(int j=0;j<row;j++)
{
board[i,j]=0;
}
}
}
static public void PrintBoard(int [,]board,int row,int col)
{
for(int i=0;i<col;i++)
{
for(int j=0;j<row;j++)
{
Console.Write(" {0}",board[i,j]);
}
Console.WriteLine();
}
Console.WriteLine();
}
static public bool SolveNQueen(int[,]board,int col)
{
if(col>=n)
return true;
//iterating for col 0 ,then 1,2 nd so on recursively
//here i=row
for(int i=0;i<n;i++)
{
if(IsSafe(board,i,col))
{
board[i,col]=1;
if(SolveNQueen(board,col+1))
return true;
board[i,col]=0;
}
}
return false;
}
static public bool IsSafe(int[,]board,int row,int col)
{
//checking the elements in the same row - different column(horizontal line)
//here we are checking only the left side - fail first strategy
for(int i=0;i<col;i++)
{
if(board[row,i]==1)
return false;
}
//checking the upper diagonal to the left
for(int i=row,j=col;i>=0 && j>=0;i--,j--)
{
if(board[i,j]==1)
return false;
}
//checkinh the lower diagonal to the left
for(int i=row,j=col;i<n&&j>=0;i++,j--)
{
if(board[i,j]==1)
return false;
}
return true;
}
}
OUTPUT
n=5
public class GFG
{
static int n;
static public void Main ()
{
var TC = Console.ReadLine();
int T = Convert.ToInt32(TC);
for(int a=0;a<T;a++)
{
var no= Console.ReadLine();
n = Convert.ToInt32(no);
int [,]board = new int [n,n];
InitializeBoardWithZeros(board,n,n);
SolveNQueen(board,0);
PrintBoard(board,n,n);
}
}
static public void InitializeBoardWithZeros(int [,]board,int row,int col)
{
for(int i=0;i<col;i++)
{
for(int j=0;j<row;j++)
{
board[i,j]=0;
}
}
}
static public void PrintBoard(int [,]board,int row,int col)
{
for(int i=0;i<col;i++)
{
for(int j=0;j<row;j++)
{
Console.Write(" {0}",board[i,j]);
}
Console.WriteLine();
}
Console.WriteLine();
}
static public bool SolveNQueen(int[,]board,int col)
{
if(col>=n)
return true;
//iterating for col 0 ,then 1,2 nd so on recursively
//here i=row
for(int i=0;i<n;i++)
{
if(IsSafe(board,i,col))
{
board[i,col]=1;
if(SolveNQueen(board,col+1))
return true;
board[i,col]=0;
}
}
return false;
}
static public bool IsSafe(int[,]board,int row,int col)
{
//checking the elements in the same row - different column(horizontal line)
//here we are checking only the left side - fail first strategy
for(int i=0;i<col;i++)
{
if(board[row,i]==1)
return false;
}
//checking the upper diagonal to the left
for(int i=row,j=col;i>=0 && j>=0;i--,j--)
{
if(board[i,j]==1)
return false;
}
//checkinh the lower diagonal to the left
for(int i=row,j=col;i<n&&j>=0;i++,j--)
{
if(board[i,j]==1)
return false;
}
return true;
}
}
OUTPUT
n=5
1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0
Comments
Post a Comment