Sudoku program

Asked

Viewed 29 times

-1

I have a sudoku program that checks everything is correct, if you have it shows "Sudoku Ok!" on the screen, if not it will show the row and column that is the error. But I have a question and I wanted to know how to check if there is repeated number in the same block of sudoku and how to go through and find the error in the columns because it only checks the lines

#include <stdio.h>

void leSudoku(int n, int sudoku[16][16])
{
    char s[2];
    int i = 0, j = 0, dim = n * n;    
    for (i = 0; i < dim; i++)     
    {
        for (j = 0; j < dim; j++) 
        {
             scanf("%s", &s);   

            if (s[0] >= '1' && s[0] <= '9')
            {
                sudoku[i][j] = s[0] - '0';
            }
            
            
                if (s[0] >= 'A' && s[0] <= 'G')
                {
                    sudoku[i][j] = s[0] - 'A' + 10;
                } 
        }
    }
}

void imprimeLinha(int n)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        printf("+");
        for (j = 0; j < n; j++)
            printf("---");
    }
    printf("+\n");
}

void imprimeSudoku(int n, int sudoku[16][16])
{
    int i, j, dim = n * n;
    imprimeLinha(n);
    for (i = 0; i < dim; i++)
    {
        printf("|");
        for (j = 0; j < dim; j++)
        {
            int num = sudoku[i][j];
            if (num <= 9)
            {
                printf("%2d ", num);
            }
            else
            {
                printf("%2c ", num - 10 + 'A');  
            }
            if ((j + 1) % n == 0)
            {
                printf("|");
            }
        }
        printf("\n");
        if ((i + 1) % n == 0)
        {
            imprimeLinha(n);
        }
    }
}

int sudokuOk(int n, int sudoku[16][16], int* lin, int* col)
{
    int j = 0, i = 0, w = 0;                
    for (i = 0; i < n; i++)           // percorre as linhas
    {
        for (j = 0; j < n; j++)       // percorre as colunas
        {
            for (w = j + 1; w < n; w++) // percorrer as colunas a partir de j ate o final da linha
            {
                if (sudoku[i][j] == sudoku[i][w])
                {
                    *lin = i;               
                    *col = w;              
                    return 0;               // retorna zero , pois tem dígito Repetido
                }
            }
        }
    }
    return 1;                               // retorna 1 pois não tem dígitos repetidos
}
int main(void)
{
    int n , nlin = 0, ncol = 0, ok;
    int sudoku[16][16];
    scanf("%d", & n);                                   
    leSudoku(n, sudoku);
    imprimeSudoku(n, sudoku);
     ok = sudokuOk(n*n, sudoku, & nlin, & ncol);
    if (ok)
    {
        puts("Sudoku Ok!");                         
    }
    else
    {
        printf("\n\nErro na linha %d coluna %d !", nlin, ncol); 
    }
    printf("\n\n\n");
    return 0;
}```
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.