1. Need help to run this following 1-dimensional array 8 queens program. How can
ID: 3886014 • Letter: 1
Question
1. Need help to run this following 1-dimensional array 8 queens program. How can I fix the (print section) and What do I have to add to the (print section) at the end to run this program? the code should give you 92 times. I mean 92 different answers. Each its own specific row.
After running this program correctly with goto statements, follow the question #2.
2.Redo the 8 queens 1-dimensional array program with backtracking REMOVING ALL "GOTOs" - but implementing the same algorithm.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
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:
++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
next_row:
++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:
--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(q); //print the board
goto backtrack; //go back to find more solutions
The program should give you something like this after run the program.
0 47 5 2 6 1 3 0 5 7 2 6 3 1 4 0 6 3 5 7 1 4 2 0 6471352 1 3 5 7 20 6 4 1 4 6 0 2 7 53 7 1 4 6 3 075 2 15 0 6 3 7 24 1 5 7 2 0 3 6 4 1e 16 25 74 03 16 47 0 3 5 2Explanation / Answer
MULTIPLE QUESTIONS ARE POSTED ! So, I am answering only the FIRST Question.
The updated program is ::
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int q[8], //1D array representation of the chess board
int x=1;
c = 0; //column index, initially 0
q[0] = 0; //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
q[c] = -1; //otherwise start at the “top” of the column
next_row:
++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:
--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:
// update this section....print(q); //print the board
for (int i = 0; i < 8; ++i)
{
cout<<"Soulution no. "<<x;
cout<<q[i];
cout<<" ";
}
goto backtrack; //go back to find more solutions
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.