Question
Complete print function of eight queens 1dimentional with gotos c++ program: The eight queens puzzle is the problem of placing eight chess queens on an
Complete print function of eight queens 1dimentional with gotos c++ program:
The eight queens puzzle is the problem of placing eight chess queens on an 8x8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal.
#include
int main() {
/*board setup section*/ int q[8], //1D array representation of the chess board c = 0; //column index, initially 0 q[0] = 0; //putting 1st queen piece on upper-left corner
next_col: /*column section*/ ++c; //advance column position if (c == 8) //solutions for columns 0 through 7 found, goto print; //so print q[c] = -1; //otherwise start at the top of the column
/*row section*/
++q[c]; //advance row position if (q[c] == 8) //tried all rows in current column, none work, goto backtrack; //so we backtrack
for (int i = 0; i < c; ++i) //Check up to, but not including, current column if (q[i] == q[c] || (c-i) == abs(q[c]-q[i])) goto next_row; //if conflict exists, go to next row goto next_col; //no conflict exists, go to next column
backtrack: /*backtrack section*/ --c; //go back one column if (c == -1) //if past first column, all solutions found, return 0; //so terminate program goto next_row; //move on to next row
print: /*print section*/ print(q); //print the board goto backtrack; //go back to find more solutions }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started