Unhandled exception in Sudoku code in C language

-3

this code try solve sudoku puzzle and I checked it with some sudokus and it worked fine but in other it throw me this exception:

Unhandled exception at 0x00E11D29 in Project2.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00802FD4). occurred

when I debug it I saw it began solve it OK but suddenly stop when I try to test this puzzle(shown in the bottom)with the number 4 in the 4th cell it work, what could be the problem?

the first call: 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;
    }

struct to represent sudoku cell

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;

}
c
asked on Stack Overflow Jul 18, 2019 by someone • edited Jul 18, 2019 by Tahel

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0