Eight queens are to be placed on an 8 x 8 chessboard in such a way that one quee
ID: 3763462 • Letter: E
Question
Eight queens are to be placed on an 8 x 8 chessboard in such a way that one queen is to be in each row A program will store an array x[] with capacity 8 to represent such a configuration. If x[r] has value c then in row r there is a queen in column c. Write a program that asks a user to enter the columns that contain queens in the 8 rows The program makes sure there no queens in the same column. The program then prints the board For example, if the user enters: 0,3,4,0.0.7,6,7 the program should print. No Good For example. if the user enters: 1,3.4,5,2,8,6,7 the program should print: Q Q .. Q Q Q Q Q QExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
//Function prototypes
//FUNCTION printBoard
void printBoard(int b[][8]);
//FUNCTION assignVertical
void assignVertical(int b[][8], int column);
//FUNCTION assignHorizontal
void assignHorizontal(int b[][8], int row);
//FUNCTION assignDiagonal
void assignDiagonal(int b[][8], int row, int column);
//FUNCTION changeEliminationNumber
void changeEliminationNumber(int b[][8], int e[][8]);
//FUNCTION findMinEliminationNumber
int findMinEliminationNumber(int b[][8], int e[][8]);
//Function main begins program execution
int main (void)
{
int board[8][8] = {0};
int currentRow;
int currentColumn;
int eliminationNumber[8][8];
int rowRecord[8] = {0};
int columnRecord[8] = {0};
srand(time(NULL));
puts("Eight Queens");
puts("");
puts("Is it possible to place eight queens on an empty chessboard so that no queen is");
puts(""attacking" any other?");
puts("");
printf("Press any key to continue . . . ");
_getch();
Sleep(500);
system("cls");
printf("Generating chess board");
Sleep(300);
printf(" .");
Sleep(300);
printf(" .");
Sleep(300);
printf(" .");
Sleep(500);
system("cls");
changeEliminationNumber(board, eliminationNumber);//initialize elimination number
for(int counter = 1; counter <= 8; counter++)
{
printBoard(board);
if(findMinEliminationNumber(board, eliminationNumber) != 100)
{
do
{
currentRow = rand() % 8;
currentColumn = rand() % 8;
}
while(board[currentRow][currentColumn] != 0 && eliminationNumber[currentRow][currentColumn] != findMinEliminationNumber(board, eliminationNumber));
}
rowRecord[counter - 1] = currentRow;
columnRecord[counter - 1] = currentColumn;
if(board[currentRow][currentColumn] == 0)
{
assignVertical(board, currentColumn);
assignHorizontal(board, currentRow);
assignDiagonal(board, currentRow, currentColumn);
}
else
{
counter--;
}
for(int i = 1; i <= counter; i++)
{
printf("Move %d A queen lands on Row %d Column %d. ", i, rowRecord[i - 1], columnRecord[i - 1]);
}
printf("Eliminating squares");
Sleep(100);
printf(" .");
Sleep(100);
printf(" .");
Sleep(100);
printf(" .");
Sleep(100);
system("cls");
if(counter == 0)
{
break;
}
changeEliminationNumber(board, eliminationNumber);
}
for(int i = 1; i <= 8; i++)
{
printf("Move %d A queen lands on Row %d Column %d. ", i, rowRecord[i - 1], columnRecord[i - 1]);
}
return 0;
}//End function main
//User-defined function
//FUNCTION printBoard
void printBoard(int b[][8])
{
puts("");
puts(" [0][1][2][3][4][5][6][7]");
for(int row = 0; row <= 7; row++)
{
printf("[%d] ", row);
for(int column = 0; column <= 7; column++)
{
if(b[row][column] == 0)
{
printf("[ ]");
}
else if(b[row][column] == 1)
{
printf("[#]", b[row][column]);
}
}
puts("");
}
puts("");
}
//FUNCTION assignVertical
void assignVertical(int b[][8], int column)
{
for(int i = 0; i <= 7; i++)
{
b[i][column] = 1;
}
}
//FUNCTION assignHorizontal
void assignHorizontal(int b[][8], int row)
{
for(int i = 0; i <= 7; i++)
{
b[row][i] = 1;
}
}
//FUNCTION assignDiagonal
void assignDiagonal(int b[][8], int row, int column)
{
for(int i = row, j = 0; i <= 7 && column + j <= 7; i++, j++)
{
b[i][column + j] = 1;
}
for(int i = row, j = 0; i <= 7 && column - j >= 0; i++, j++)
{
b[i][column - j] = 1;
}
for(int i = row, j = 0; i >= 0 && column + j <= 7; i--, j++)
{
b[i][column + j] = 1;
}
for(int i = row, j = 0; i >= 0 && column - j >= 0; i--, j++)
{
b[i][column - j] = 1;
}
}
//FUNCTION changeEliminationNumber
void changeEliminationNumber(int b[][8], int e[][8])
{
int counter;
int row, column;
for(row = 0; row <= 7; row++){//outer for
for(column = 0; column <= 7; column++){//inner for
counter = 0;
if(b[row][column] == 0)
{
counter++;
}
for(int i = 0; i < row; i++)
{
if(b[i][column] == 0)
{
counter++;
}
}
for(int i = 7; i > row; i--)
{
if(b[i][column] == 0)
{
counter++;
}
}
for(int i = 0; i < column; i++)
{
if(b[row][i] == 0)
{
counter++;
}
}
for(int i = 7; i > column; i--)
{
if(b[row][i] == 0)
{
counter++;
}
}
for(int i = row + 1, j = 1; i <= 7 && column + j <= 7; i++, j++)
{
if(b[i][column + j] == 0)
{
counter++;
}
}
for(int i = row + 1, j = 1; i <= 7 && column - j >= 0; i++, j++)
{
if(b[i][column - j] == 0)
{
counter++;
}
}
for(int i = row - 1, j = 1; i >= 0 && column + j <= 7; i--, j++)
{
if(b[i][column + j] == 0)
{
counter++;
}
}
for(int i = row - 1, j = 1; i >= 0 && column - j >= 0; i--, j++)
{
if(b[i][column - j] == 0)
{
counter++;
}
}
e[row][column] = counter;
}//end inner for
}//end outer for
}
//FUNCTION findMinEliminationNumber
int findMinEliminationNumber(int b[][8], int e[][8])
{
int min = 100;
for(int row = 0; row <= 7; row++)
{
for(int column = 0; column <= 7; column++)
{
if(e[row][column] < min && b[row][column] == 0)
{
min = e[row][column];
}
}
}
return min;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.