Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Summary: determine if a Chess situation is an eight queens puzzle solution. Deta

ID: 3551047 • Letter: S

Question

Summary: determine if a Chess situation is an eight queens puzzle solution.

Details:

Write a program that will read  Chess situations in a FEN format file "attack.fen" (http://en.wikipedia.org/wiki/Forsyth-Edwards_Notation) but which only contains white queens. The first field describes the positions of the pieces on a 8x8 chess board.  The  last five fields can be ignored, but they will always be "w - - 0 1"

Determine if the board describes a valid solution to the "Eight Queens Puzzle" (http://en.wikipedia.org/wiki/Eight_queens_puzzle)

For each line (situation) in "attack.fen", create a file with the same data as "attack.fen" but with the phrase " 8 queens solution" appended to each line. The output file should be called "attack.sol"

You do not have to perform error checking on the input: the file will only contain valid input, and only white queens.

Note: example input/output files will be provided for you to test your code on.

Explanation / Answer

C++ code for this is

include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>

int board[20];
int count;
void main()
{
int n,i,j;
void queen(int row, int n);
clrscr();
printf(" Program for queen's using backtracking");
printf("Enter number of queen's ");
scanf("%d",&n);
queen(1,n); //trace backtracking
getch();
}


/*This function is for printing the solution to n-queen's problem*/
void print_board(int n)
{
int i,j;
printf(" solution %d: ",++count);
//number of solution
for(i=1;i<=n;i++)
{
printf(" %d",i);
}
for(i=1;i<=n;i++)
{
printf(" %d",i);
for(j=1;j<=n;j++) //for n*n board
{
if(board[i]==j)
printf(" Q"); //Queen at i,j position
else
printf(" -"); //empty slot
}
}
printf(" Press any key to continue.....");
getch();
}
/*This function is for checking for the conflicts.If there is no conflict for the desired position it returns 1 otherwise it returns 0*/
int place(int row ,int column)
{
int i;
for(i=1;i<=row-1;i++)
{
//checking for column and diagonal conflicts
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
}
//no conflicts hence Queen can be placed
return 1;
}




/*By this function we try the next free slot and check for proper positioning of queen */
void queen(int row, int n)
{
int column;
for(column=1;column<=n;column++)
{
if(place(row,column))
{
board[row]=column; //no conflicts so place queen
if(row==n)//dead end
print_board(n); //printing the board configuration
else //try queen with next position
queen(row+1,n);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote