Thursday, 1 May 2014

Queen Problem C Program

/*Queen Problem C Program*/
#include<stdio.h>
#include<conio.h>
void queen(int row,int n);
int place(int row,int column);
int board[20];
void main()
{
int n,row,column;
clrscr();
printf("enter the value of n:");
scanf("%d\n",&n);
queen(row,n);
place(row,column);
getch();
}
void queen(int row, int n)
{
 int column; int board[10];
 for(column=1;column<=n;column++)
    {
     if(place(row,column))
       {
board[row]=column;
if(row==n)
{
printf("%d",board[n]);
}
else
{
queen(row+1,n);
}
       }
    }
}

int place(int row, int column)
{
 int i;
 for(i=1;i<=row-1;i++)
    {
     if(board[i]==column)
       {return 0;}
     else if(abs(board[i]-column)==abs(i-row))
   {return 0;}

    }
    return 1;
}

No comments:

Post a Comment