c - NxN 棋盘上的 N 个皇后

标签 c

#include <stdio.h>
#include <math.h>

int n=4;
int GetQueenSettings(int board[4][4],int currentRow,int n)
{
    //decide when the recursion stops
    if(currentRow==n)
        return 1; //successful setting
    //otherwise we set column by column in this row and continue
    int TotalSettingCount=0;
    for(int i=0;i<n;i++)
    {
        //make sure it can be set (it is unset at that moment)
        if(board[currentRow][i]==0)
        {
            board[currentRow][i]==1+currentRow;
            //use row related info for settings
            //now set invalid positions for remaining rows
            setInvalid(board,currentRow,n,i);
            //recover after this before trying the next
            TotalSettingCount += GetQueenSettings(board,currentRow+1,n);
            board[currentRow][i]=0;
            RecoverBoard(board,currentRow,n);
        }
    }
    return TotalSettingCount;
}

void setInvalid(int board[4][4],int currentRow,int n,int i)
{
    //vertical and diagonal elements
    for(int row=currentRow+1;row<n;row++) //start from the next line
    {
        //firstly make sure board can be set
        if(board[row][i]==0)//vertical position
            board[row][i]=-(1+currentRow);
        //now check diagonal
        int rowGap=row-currentRow;
        if(i-rowGap>=0 && board[row][i-rowGap]==0)
        {
            //left bottom diagonal position
            board[row][i-rowGap]=-(1+currentRow);
        }
        if(i+rowGap<n && board[row][i+rowGap]==0)
        {
            //bottom right diagonal position
            board[row][i+rowGap]=-(1+currentRow);
        }

    }
}

void RecoverBoard(int board[4][4],int currentRow,int n)
{
    //recover is to check all remaining rows if index is higher than current row(setters)
    //OR less than -currentRow(invalids)!
    for(int row=currentRow+1;row<n;row++)
    {
        for(int col=0;col<n;col++)
        {
            if(board[row][col]>currentRow || board[row][col]< -currentRow)
                board[row][col]=0;
        }
    }
}
int main()
{

    int board[n][n];
    printf("Number of settings:-> %d",GetQueenSettings(board,0,n));

    return 0;
}

NxN棋盘上放置了N个皇后,彼此互不干扰。当我运行此代码时,我得到的答案是 0 而不是 2 。我也无法找出将数组板传递给具有可变大小的函数的方法(大小将由用户给出)。我做错了什么?!

最佳答案

您应该初始化您的board 。事实上,你从一个充满垃圾值的棋盘开始。您正在使用可变长度数组作为板。此类数组无法初始化,因此您必须使用循环或 memset 将板设置为全零。来自<string.h> :

int board[n][n];

memset(board, 0, sizeof(board));

当维度作为早期参数传入时,您可以将可变长度数组传递给函数,例如:

int GetQueenSettings(int n, int board[n][n], int currentRow) { ... }

同时修复 =/==切换 setInvalid :

    if (board[row][i] == 0)
        board[row][i] = -(1 + currentRow);

最后确保所有函数在调用时都有正确的原型(prototype)。

关于c - NxN 棋盘上的 N 个皇后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29185362/

相关文章:

c - K&R 指针操作

c++ - 如何使智能感知将当前源文件视为独立的翻译单元

c - 为什么 printf( ) 在重新打开标准输出流后工作异常

c - 达到限制后恢复 [vf]?nprintf

c - adcx 和 adox 的测试用例

c++ - 默认的构造函数和析构函数是内联的吗?

c - 如何在 Linux 中转换来自 LM92 的温度数据

C 信号量未按预期锁定

c - 使用memset和malloc会冲突吗?

c - Travis CI如何添加C库?