top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C/Java: How to program to check if input matrix is a valid sudoku solution?

0 votes
932 views

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.

posted Jul 2, 2015 by anonymous

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

1 Answer

0 votes

For sudoku you need three steps -
1. Horizontal no number should get repeated.
2. Vertical no number should get repeated.
3. At the grid no number should get repeated.

Following program is the 9*9 matrix -

int flag = 0;               /* 1 - Error, 0 - Success */

int main(int argc, char *argv[])
{
    FILE *data;
    char buffer[9];
    char matrix[9][9];
    int c;
    int i = 0, j = 0;

    data = fopen("myFile.dat", "r");

    /* Reading the Matrix from file */
    while ((c = getc(data)) != EOF)
        {
            if (c == ' ')
                continue;
            else if (c == '\n')
                {
                    i = 0;
                    j++;
                }
            else
                matrix[j][i++] = c;
        }


    /* Horizontal check */
    for (i = 0; i < 9; i++)
        {
            for (j = 0; j < 9; j++)
                buffer[j] = matrix[i][j];

            if (check(buffer) != 0)
                printNotValid(buffer, HORIZONTAL_CHECK, i);
        }

    /* Vertical check */
    for (j = 0; j < 9; j++)
        {
            for (i = 0; i < 9; i++)
                {
                    buffer[i] = matrix[i][j];
                }
            if (check(buffer) != 0)
                printNotValid(buffer, VERTICAL_CHECK, j);
        }

    /* Submatrix check */
    int count = 0;
    int icount = 0;
    int jcount = 0;
    int matcount = 0;

    for (i = 0; i <= 6; i+= 3)
        {
            for (j = 0; j <= 6; j += 3)
                {
                    for (icount = 0, count = 0; icount < 3; icount++)
                        {
                            for (jcount = 0; jcount < 3; jcount++)
                                {
                                    buffer[count++] = matrix[i+icount][j+jcount];
                                }

                        }

                    ++matcount;
                    if (check(buffer) != 0)
                        printNotValid(buffer, SUBMATIX_CHECK, matcount);
                }
        }

    if (flag == 0)
        printf("\n The Sudoku is correct :)");

    printf("\n");
    return 0;
}

int check(char *array)
{
    int i, count;
    for (i = 0; i < 9; i++)
        {
            count = i;
            while (count < 9)
                {
                    if (array[i] == array[++count])
                        return 1;                    
                }
        }
    return 0;
}

int printNotValid(char *array, int type, int errorLocation)
{
    int i;
    flag = 1;
    if (type == HORIZONTAL_CHECK)
        {
            printf("\n Found error at row %d: \n\t", ++errorLocation);
            for (i = 0; i < 9; i++)
                printf("%c ", array[i]);
        }
    else if (type == VERTICAL_CHECK)
        {
            printf("\n Found error at column %d: \n", ++errorLocation);
            for (i = 0; i < 9; i++)
                printf("\n\t %c ", array[i]);
        }
    else if (type == SUBMATIX_CHECK)
        {
            printf("\n Found error at Submatrix %d: \n", errorLocation);
            for (i = 0; i < 9; i++)
                {
                    printf("%c ", array[i]);
                    if ((i == 2) || (i == 5))
                        printf("\n");                        
                }

        }
}
answer Jul 3, 2015 by Salil Agrawal
...