top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Determine whether the given sudoku board configuration is valid or not?

0 votes
465 views
C: Determine whether the given sudoku board configuration is valid or not?
posted Mar 8, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

A sudoku is valid and a sudoku is solvable are two different things. I am assuming that you are talking only if sudoku is valid or not.

A sudoku is valid if
digits seen in every row, every column and every block are unique ie appeared only once.
each digit is greater then or equal to 1 and less then or equal to 9.

Assume your sudoku is in the form of 9*9 matrix, with few filled characters and few not filled...Following is the sample code (not tested)

bool IsSudokuValid(char board[9][9]) 
{ 
        bool r[9][9], c[9][9], s[3][3][9];
        memset(r, false, sizeof(r));
        memset(c, false, sizeof(c));
        memset(s, false, sizeof(s));
        int number;

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] > '0' && board[i][j] <='9') { 
                    number = board[i][j] - '1';
                    if (r[i][number]) return false;
                    r[i][number] = true;

                    if (c[j][number]) return false;
                    c[j][number] = true;

                    if (s[i / 3][j / 3][number]) return false;
                    s[i / 3][j / 3][number] = true;
                }
            }
        }

        return true;
}
answer Mar 9, 2017 by Salil Agrawal
Similar Questions
0 votes

Please help me with a sample program to check whether given matrix is valid sudoku solution or not. Input would be matrix of n*n size and validate grids of sqrt(n)*sqrt(n) size along with row and columns.

–1 vote

Write a C program to check if the given string is repeated substring or not.
ex
1: abcabcabc - yes (as abc is repeated.)
2: abcdabababababab - no

...