Я перевел программу из более ранней версии, которая использовала операторы go, в операторы, но где-то мои логи c запутались, потому что они печатают бесконечное количество досок l oop, где единственным элементом является ферзь в ряду 0, столбец 0.
Как мне найти ошибку?
bool rowCheck(int board[], int column);
bool diagonalCheck(int board[], int column);
void print (int board[]);
int main(){
int queens[8];
int col = 0;
queens[0] = 0;
while(col > -1){
//if current column moves beyond 8th column, print solution and
if(col == 8){
print(queens);
col--;
}
//if current row moves beyond the 8th row, resets row to -1 and moves back to previous column;
if(queens[col] == 8){
queens[col] = -1;
col--;
}
//if the current board checks true for all columns prior, moves to next column
else if( rowCheck(queens, col) && diagonalCheck(queens, col) ){
col++;
}
//moves queen to the next row of current column
else{
queens[col]++;
}
}
return 0;
}
//checks previous rows for an adjacent queen
bool rowCheck(int board[] , int column){
for(int i = 0; i < column; i++){
if(board[i] == board[column])
return false;
}
return true;
}
//checks previous rows for queens diagonally
bool diagonalCheck(int board[], int column){
for(int i = 0; i < column; i++){
if((column - i) == abs(board[column] - board[i]))
return false;
}
return true;
}
//print
void print(int board[]){
static int solution = 0;
solution++;
cout << "Solution " << solution << endl;
for(int row = 0; row < 8; row++){
for(int column = 0; column < 8; column++)
if(board[column] == row)
cout << "1 ";
else
cout << "0 ";
cout << endl;
}
cout << endl;
}