C program to place N queens in N*N chess board or N*N matrix


This below given c program gives you a clear view as how to place N queens in N*N matrix.......!!!!!!!!!
check it out.......!!!!!!


#include <stdio.h>
#include <conio.h>
int chess(char Array[100][100] ,int, int row);
int check(char Array[100][100],int,int row,int direction);
int main()
{
int N;
char chess[100][100]={};
printf("enter value of N\n");
scanf("%d",&N);
chess(chess,N,0);
{
int i,x;

for(i=0;i<N;++i)/*prints the result*/
{
printf("\n\t\t\t");
for(x=0;x<N;++x)
{
if(chess[i][x]==0)
printf("*");
else
printf("%c ",chess[i][x]);
}
}
}
printf("\n");
}
int chess(char Arr[100][100] ,int N, int row)
{
int direction=0;

if(row==N)
return 1;

while(direction < N)
{
if(check(Arr,N,row,direction)) /*checks out the row*/
{
Arr[row][direction]='Q'; /*places a queen on the board*/
if(chess(Arr,N,row+1))/*recursion takes place*/
return 1;
Arr[row][direction]=0;/*corrects out the change made*/
}/*returned 0*/
direction++;
}
return 0;
}
int check(char Arr[100][100],int N,int row,int direction)
{/*checking out just the left size*/

int r,d;
r=row;
d=direction;
while(r >= 0 && d >= 0)
{
if(Arr[r][d]=='Q')
return 0;
--r;
--d;
}

d=direction;
r=row;
while(d < N && r >= 0)
{
if(Arr[r][d]=='Q')
return 0;
++d;
--r;
}

d=direction;
r=row;
while(r >= 0)
{
if(Arr[r][d]=='Q')
return 0;
--r;
}
return 1;
}


//so this particular program will give u the required answer.......!!!!

Comments