#include
static int board[8][8];
static int arrpos[8][2];
int count=0;
int flag=0;
int check(int row,int column)
{
int i;
for(i=0;i<8;i++) { if((board[row][i]==1)&&(i!=column)) return 0; if((board[i][column]==1)&&(i!=row)) return 0; } int copyr=row-1,copyc=column+1; while((copyr>=0)&&(copyc>=0)&&(copyr<=7)&&(copyc<=7)) { if((board[copyr][copyc]==1)) return 0; copyr=copyr-1; copyc=copyc+1; } copyr=row+1,copyc=column+1; while((copyr>=0)&&(copyc>=0)&&(copyr<=7)&&(copyc<=7)) { if((board[copyr][copyc]==1)) return 0; copyr=copyr+1; copyc=copyc+1; } copyr=row-1,copyc=column-1; while((copyr>=0)&&(copyc>=0)&&(copyr<=7)&&(copyc<=7)) { if((board[copyr][copyc]==1)) return 0; copyr=copyr-1; copyc=copyc-1; } copyr=row+1,copyc=column-1; while((copyr>=0)&&(copyc>=0)&&(copyr<=7)&&(copyc<=7)) { if((board[copyr][copyc]==1)) return 0; copyr=copyr+1; copyc=copyc-1; } return 1; } void printarray() { count++; printf("solution %d:",count); int i;//printing the answer for(i=0;i<8;i++) { printf("(%d,%d)",arrpos[i][0],arrpos[i][1]); } printf("\n"); //char c; //scanf("%c",&c); } void getpos(int row,int column,int i) { if(i==8) { printarray();//print answer int lr=arrpos[7][0]; int lc=arrpos[7][1]; board[lr][lc]=0; if (board[0][7]==1) { flag++; if(flag==4) exit(1); }//end program getpos(lr,lc+1,i-1);//start next } if((row<0)||(row>7)||(column>7)||(column<0))
{
int m=i-1;
board[row-1][arrpos[m][1]]=0;
getpos(row-1,arrpos[m][1]+1,i-1);//backtrack previous
}
int mm=check(row,column);
if(mm)
{
arrpos[i][0]=row;
arrpos[i][1]=column;
board[row][column]=1;
getpos(row+1,0,i+1);
//get next row
}
else if(column<7)
getpos(row,column+1,i);//get next place
else
{
int m=i-1;
board[row-1][arrpos[m][1]]=0;
getpos(row-1,arrpos[m][1]+1,i-1);//backtrack previous
}
}
int main()
{
printf("Taking the chessboard as 8x8 from (0,0) to (7,7) as co-ordinates\nSolutions are:\n");
getpos(0,0,0);
return 0;
}