c++ - 8 Queens Variation-排列数回溯实现-错误输出

标签 c++ algorithm backtracking n-queens

我正在尝试编写一个程序,在给定初始输入的情况下,将计算 8X8 棋盘上 8 个皇后的不同排列数。出于某种原因,我的测试数据没有给我适当的输出。我想知道你们是否能指出我正确的方向。我没有编译器错误,但我在最后一个数据集上遇到段错误。

我试过调试,但我想我已经看了我的代码太久了,以至于我开始弄糊涂了。如果有人能给我一些帮助,我将不胜感激。谢谢。我认为我的问题要么在 isSafe 函数中,要么在 solveNQUtil 方法中的终止条件中。

我的测试数据,(每一行都是单独的行、列、我的输出和预期结果):

r    c    Output      Expected output
1    2       14         8
6    7       8          14
7    8       312        8
8    8       312        4
2    2       4          16
2    4       12         8
1    7       8          8
8    3    Seg Fault     16

我的代码:

#include <iostream>
#include<stdio.h>
using namespace std;
int arrangements = 0;
int column_start = 0;
/* A utility function to print solution */
void printSolution(int board[8][8])
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
            if( board[i][j] ) {
                printf(" Q ");
            }else{
                printf(" . ");
            }
            //printf(" %d ", board[i][j]);
        printf("\n");
    }
}
bool isSafe(int board[8][8], int row, int col)
{
    int i, j;
    for (i = 0; i < 8; i++){
        if (board[row][i]){
            return false;
        }
    }
    for (i = row, j = col; i >= 0 && j >= 0; i--, j--){
        if (board[i][j]){
            return false;
        }
    }
    for (i = row, j = col; i < 8 && j < 8; i++, j++){
        if (board[i][j]){
            return false;
        }
    }
    for (i = row, j = col; j >= 0 && i < 8; i++, j--) {
        if (board[i][j]){
            return false;
        }
    }
    for( i = row, j = col; j < 8 && i >= 0; i--, j++){
        if (board[i][j]){
            return false;
        }
    }
    return true;
}
void solveNQUtil(int board[8][8], int queens, int col)
{
    if (queens >= 8){
        printSolution(board);
        cout << "---------------------------------------------" << endl;
        arrangements++;
    }else{
        for (int i = 0; i < 8; i++){
            if ( isSafe(board, i, col) ){
                board[i][col] = 1;
                int tempCol = (col + 1) % 8;
                solveNQUtil(board, queens + 1, tempCol );
                //backtrack
                board[i][col] = 0; 
            }
        }  
    }
}
bool solveNQ(){
    int row = 0;
    int col = 0;
    int board[8][8];
    for(int i = 0; i < 8; i++){
        for(int j = 0; j < 8; j++){
            board[i][j] = 0;
        }
    }
    cin >> row >> col;
    board[row][col] = 1;
    column_start = col;
    solveNQUtil(board, 1, (col + 1) % 8);
    //solveNQUtil(board,0,0);
    if(arrangements == 0){
        printf("Solution does not exist");
        return false;
    }
    return true;
}
int main(){
    solveNQ();
    cout << "Arrangements: " << arrangements << endl;
    return 0;
}

最佳答案

只要你的row < 8 and col < 8您得到的输出是正确的。您给出的预期输出是错误的。

row = 8 or col = 8数组索引超出范围并尝试将值分配给未分配给数组的某些内存。

  • 如果其他内存是空闲的,它将存储在其中并且不会有任何错误,但您的输出将是错误的,因为您的第一个女王在董事会之外,但您正在告诉您的 solveNQUtil董事会中有一位女王。
  • 但如果其他内存已分配给其他内存,您将遇到段错误。

关于c++ - 8 Queens Variation-排列数回溯实现-错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29001839/

相关文章:

c++ - 外部 "C": What does and what doesn't need it?

c++ - 用于纹理上传的OpenGL PBO,无法理解一件事

用于类型检查类似于 ML 的模式匹配的算法?

algorithm - 如何使用音频重采样器对 IF 信号进行重采样

algorithm - 获取给定颜色的渐变,其中颜色位于渐变的中间

list - SWI-Prolog : How to stop the predicate when the list is empty?(包括谓词)

c++ - 私有(private)(隐藏)QSharedData

c++ - 线程 - 同步和 sleep 线程拒绝唤醒(LINUX)

algorithm - 创建 nxn 矩阵 - 数独类

javascript - Python - 如何将多重赋值更改为 JavaScript 语法并获得给定字符串的正确排列