please Complete the 8 queens 2 dimensional array program in C ++ with backtracki
ID: 3542398 • Letter: P
Question
please Complete the 8 queens 2 dimensional array program in C ++ with backtrackingint b[8][8]={0};
int counter=0;
int r;
int c=0;
b[0][0]=1;
NC: c++;
if (c == 8)
goto printBoard;
r=-1
THE ROWS
NR: r++;
if (r==8)
goto backTrack;
for (int i=0; i<c ; i++)
if (b[r][i]==1) goto NR;
for (int i=1; (r-i)>=0 && (c-i)>=0 ; i++)
if (b[r-i][c-i]==1) goto NR;
for (int i=1; (r+i)<8 && (c-i)>=0 ; i++)
if (b[r+i][c-i]==1) goto NR;
b[r][c]=1;
goto NC;
backTrack: c--;
if ( c == -1 )
{ //what goes below?
cout << counter << " solutions found.";
cout << endl;
return 0;
}
r=0;
while (b[r][c]!=1)
r++;
b[r][c]=0;
goto NR; //what goes here?
RD
printBoard: counter++;
cout << "Solution #"<<counter<<":"<<endl;
for(int i=0 ; i<8 ; i++)
{
for (int j=0 ; j<8 ; j++)
if ( b[i][j] == 1 )
cout << "Q";
else
cout << "-";
cout <<endl;
}
cout <<endl;
goto backTrack; // what goes here?
please Complete the 8 queens 2 dimensional array program in C ++ with backtracking
int b[8][8]={0};
int counter=0;
int r;
int c=0;
b[0][0]=1;
NC: c++;
if (c == 8)
goto printBoard;
r=-1
THE ROWS
NR: r++;
if (r==8)
goto backTrack;
for (int i=0; i<c ; i++)
if (b[r][i]==1) goto NR;
for (int i=1; (r-i)>=0 && (c-i)>=0 ; i++)
if (b[r-i][c-i]==1) goto NR;
for (int i=1; (r+i)<8 && (c-i)>=0 ; i++)
if (b[r+i][c-i]==1) goto NR;
b[r][c]=1;
goto NC;
backTrack: c--;
if ( c == -1 )
{ //what goes below?
cout << counter << " solutions found.";
cout << endl;
return 0;
}
r=0;
while (b[r][c]!=1)
r++;
b[r][c]=0;
goto NR; //what goes here?
RD
printBoard: counter++;
cout << "Solution #"<<counter<<":"<<endl;
for(int i=0 ; i<8 ; i++)
{
for (int j=0 ; j<8 ; j++)
if ( b[i][j] == 1 )
cout << "Q";
else
cout << "-";
cout <<endl;
}
cout <<endl;
goto backTrack; // what goes here?
Explanation / Answer
here is the code:-
#include <iostream>
using namespace std;
void EightQueens2D () {
// insert code here...
int b[8][8]={0},row,col=0,i;
b[0][0]=1;
Nc: col++;
if (col==8){
goto print;
}
row=-1;
Nr: row++;
if (row==8) {
goto backtrack;
}
// row conflict
for (i=0; i < col; i++) {
if (b[row][i] == 1)
goto Nr;
}
// up diagonal test
for (i=1; (row-i>=0) && (col-i>=0); i++) {
if (b[row-i][col-i]==1)
goto Nr;
}
// down diagonal test
for (i=1; (row-i)<=8 && (col-i) >=0; i++) {
if (b[row+i][col-i]==1)
goto Nr;
b[row][col]=1;
goto Nc;
}
backtrack: col--;
//find the queen
if (col==-1) {
return;
}
while (b[row][col]!=1) {
row++;
b[row][col]=0;
goto Nr;
}
print:
print(b);
cout << endl;
goto backtrack;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.