C语言Sudoku代码中未处理的异常

时间:2019-07-18 05:26:26

标签: c

此代码尝试解决数独难题,我对它进行了一些检查 数独,效果很好,但在其他情况下,我抛出了这个异常:

  

Project2.exe中0x00E11D29处未处理的异常:0xC00000FD:堆栈溢出(参数:0x00000001,0x00802FD4)。发生

当我调试它时,我看到它开始解决它,但是当我调试时突然停止     尝试用数字4来测试这个难题(显示在底部)。     它工作的第四个单元格 可能是什么问题?

第一个调用:f_solveSudoku(sudoku,0);

| 0  6  0 | 0  0  1 | 0  0  0 |
| 0  8  5 | 7  0  6 | 0  0  0 |
| 3  0  0 | 9  0  2 | 0  1  0 |
--------------------------------
| 2  1  0 | 0  0  9 | 0  0  0 |
| 0  5  3 | 2  0  0 | 1  0  9 |
| 0  0  0 | 0  7  0 | 0  0  0 |
--------------------------------
| 0  3  1 | 6  0  0 | 0  4  8 |
| 0  4  0 | 8  0  0 | 0  7  0 |
| 0  0  0 | 5  0  4 | 0  0  1 |


    void main(int argc, char** argv)
   {
        FILE* sudokuFile;
        int level;
    char term;
        printf("choose level press:\n1.easy\n2.medium\n3.hard\n");
        if (scanf("%d%c", &level, &term) != 2 || term != '\n' || level < 1 || level>3)
        {
            printf("failure\n");
            return;
        }
        else


printf("valid integer followed by enter key\n");
    sudokuFile = fopen(argv[level], "r");
        if (sudokuFile == NULL)
        {
            printf("file not found");
            return;
        }
        S_sudokuCell sudoku[N][N];

char c[8];
int i = 0;
fgets(c, 8, sudokuFile);


    while (i < 81 && fgets(c, 8, sudokuFile) != NULL) {
        sudoku[c[0] - '0'][c[2] - '0'].number = c[4] - '0';
    if (c[4] - '0' != 0)
        sudoku[c[0] - '0'][c[2] - '0'].isGiven = 1;
        i++;
}
    fclose(sudokuFile);

    f_solveSudoku(sudoku, 0);

}

int f_isCorrectRow(S_sudokuCell board[N][N], int cell, int number)
{
    /*static int gggg;
    printf("%d \n", gggg++);
    if (gggg == 14100)
        printf("14395");*/
    int row = cell / 9;
        for (size_t i = 0; i < N; i++)
        {
        if (board[row][i].number == number)
            return 0;

        }
        return 1;
    }
   int f_isCorrectCol(S_sudokuCell board[N][N], int cell, int number)
    {
        int col = cell % 9;
    for (size_t i = 0; i < N; i++)
        {
        if (board[i][col].number == number)
            return 0;
        }
        return 1;
    }
  

代表数独单元的结构

typedef struct S_sudokuCell
{
    unsigned short number : 4;
    unsigned short isGiven : 1;
}S_sudokuCell;




void f_solveSudoku(S_sudokuCell board[N][N], int cell)
{
    if (cell >= 81||cell<0)
        return;
    if (board[cell / 9][cell % 9].isGiven || f_findNumber(board, cell))
    {
        int num_moves = 1;
        while (board[(cell + num_moves) / 9][(cell + num_moves) % 9].isGiven)
            num_moves++;
        f_solveSudoku(board, cell + num_moves);
    }
    else
    {
        int num_moves = 1;
        while (board[(cell - num_moves) / 9][(cell - num_moves) % 9].isGiven)
            num_moves++;
        f_solveSudoku(board, cell - num_moves);
    }

}


int f_findNumber(S_sudokuCell board[N][N], int cell)
{
    int n = board[cell / 9][cell % 9].number;
    for (size_t i = n + 1; i <= N; i++)
    {
        if (f_isCorrectRow(board, cell, i) && f_isCorrectCol(board, cell, i) && f_isCorrectBlock(board, cell, i))
        {
            board[cell / 9][cell % 9].number = i;
            return 1;
        }
    }
    board[cell / 9][cell % 9].number = 0;
    return 0;
}


int f_isCorrectBlock(S_sudokuCell board[N][N], int cell, int number)
{
    size_t i = (cell / 9) - (cell / 9) % 3;
    size_t j = (cell % 9) - (cell % 9) % 3;
    for (int x = 0; x < 3; x++)
    {
        for (int h = 0; h < 3; h++)
        {
            if (board[i + x][j + h].number == number)
                return 0;
        }

    }
    return 1;

}

0 个答案:

没有答案