Need help to run this 2-dimensional array 8 queens function. What do I have to a
ID: 3884923 • Letter: N
Question
Need help to run this 2-dimensional array 8 queens function. What do I have to add to the (print section) at the end to run this program?
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int b[8][8] = {0},r,c=0;
b[0][0] = 1; //putting 1st queen piece on upper-left corner
next_col:
++c; //advance column position
if (c == 8) //solutions for columns 0 through 7 found,
goto print; //so print
r = -1; //otherwise start at the “top” of the column
next_row:
++r; //advance row position
if (r == 8) //tried every row of current column, none work,
goto backtrack; //so we backtrack
for (int i = 0; i < c; ++i)
if (b[r][i] == 1) //if another queen on same row,
goto next_row; //try the next row
for (int i = 1; (r-i) >= 0 && (c-i) >= 0; ++i)
if (b[r-i][c-i] == 1) //if conflict found in up-diagonal,
goto next_row; //try the next row
for (int i = 1; (r+i) < 8 && (c-i) >= 0; ++i)
if (b[r+i][c-i] == 1) //if conflict found in down-diagonal,
goto next_row; //try the next row
b[r][c] = 1; //no conflicts found, so place queen
goto next_col; //move on to the next column
backtrack:
--c; //go back one columnc; //go back one columnc; //go back one columnc; //go back one column
if (c == -1) //if past first column, all solutions found,
return 0; //so terminate program
r = 0; //start looking from first row
while (b[r][c] != 1) //while we haven’t found the queen piece,
++r; //move to next row of current column
b[r][c] = 0; //remove queen piece
goto next_row; //move on to next row
print:
print(b);
goto backtrack;
Explanation / Answer
Hi, in the print function, you can print the entire board, or only where the queens are present like below,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.