Modify the Eight Queens program 1 dimensional array no goto

Modify the Eight Queens program (1 dimensional array - no goto version) so that it prints out a chessboard with some \"fancy\" representation of a queen in the appropriate positions. In class we went over the code to print out a chessboard. You need to: Augment that code by adding two additional \"box\"es, wq which represents a picture of a queen placed in a \"white\" square, and bq. representing a picture of a queens placed in a \"black\" square. After the code that fills the array board[8][8] with the addresses of bb and wb, insert code to change eight entries in that array to reflect the positions of eight queens on the board. You will get these positions from a one dimensional array q[8] representing a solution to the eight queens problem. The change that you make in these eight places is to replace the pointer in board for one representing either a wq, or bq, as appropriate for that position. You know the eight positions because, given q[i], i represents the column and q[i] represents the row. You can tell whether its black or white by looking at the row and column indexes of its position on the board. The code described above (in 2.) goes into the print function. You pass the arrays q and board to print. In the function you modify board using q print the picture of the board \"Clean up\" array board, by restoring its original values, to get ready for the next call to print.

Solution

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

char board[10][10];
int n;

void printBoard() {
int m, n;
printf(\"\ \");

for (m = 0; m < n; m++) {
for (n = 0; n < n; n++)
printf(\"%c\\t\", board[m][n]);
printf(\"\ \ \");
}
}

int getMarkColumn(int rowIndex) {
int m;
for (m = 0; m < n; m++)
if (board[rowIndex][m] == \'Q\') {
return (m);
break;
}
}

int checkFeasible(int row, int col) {
int m, tcol;
for (m = 0; m < n; m++) {
tcol = getMarkColumn(m);
if (col == tcol || abs(row - m) == abs(col - tcol))
return 0;
}
return 1;
}

void nqueenSolver(int row) {
int m, n;
if (row < n) {
for (m = 0; m < n; m++) {
if (checkFeasible(row, m)) {
board[row][m] = \'Q\';
nqueenSolver(row + 1);
board[row][m] = \'.\';
}
}
} else {
printf(\"\ The nqueen solution is:- \");
printBoard();
}
}

void cleanup(){
   int m,n;
   for (m = 0; m < n; m++)
for (n = 0; n < n; n++)
board[m][n] = \'.\';
}
int main() {
int m, n;

printf(\"\ Enter total queens:- \");
scanf(\"%d\", &n);

for (m = 0; m < n; m++)
for (n = 0; n < n; n++)
board[m][n] = \'.\';

nqueenSolver(0);
cleanup();
return (0);
}

 Modify the Eight Queens program (1 dimensional array - no goto version) so that it prints out a chessboard with some \
 Modify the Eight Queens program (1 dimensional array - no goto version) so that it prints out a chessboard with some \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site