please Complete the 8 queens 1 dimensional array program in C ++ with backtracki
ID: 3542399 • Letter: P
Question
please Complete the 8 queens 1 dimensional array program in C ++ with backtracking . it has 92 solution and suppose to print 92 solution . you should do the row test ,column test and diagonal test with backtracking
Technique: Backtrack Algorithm using gotos
Data Structure: 1D array
int col = 0, counter = 0;
int q[8];
q[0] = 0; //place the first queen, bypass checking
next_col: ++col;
if ( col == 8 )
goto print_board; //what happens?
q[col] = -1;
next_row: q[col]++;
if ( q[col] == 8 )
goto back_track; //what happens?
//the test
for( int i = 0 ; i < c ; ++i )
if( q[c] == q[i] || c
Explanation / Answer
here is the code:-
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int q[8];
q[0] = 0;
int c = 0;
int count = 0;
NC: c++;
if (c == 8) goto print;
q[c] = -1;
NR: q[c] =
if (q[c] == 8) goto backtrack;
for(int i = 0; i < c; i++){
if(q[i] == q[c] || abs(q[c] - q[i]) == (c - 1))
goto NR;
}
goto NC;
backtrack:
c--;
if(c = -1) return 0;
goto NR;
Print:
++count;
cout << count << endl;
for(int i = 0; i <= 7; i++){
cout << q[i];
}
cout << endl;
goto backtrack;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.