Sudoku c ++ how to check if there are repeated numbers in an array or a multidimensional array [closed]

-1

All I have to do is grab a solved sudoku and check if the solved sudoku is correct.

What I do not know, is whether to make an array of "int sudoku [9] [9]" or make an array for each row and column

I'm new to c ++

using namespace std;
int sudoku[9][9] =
{
{5,3,4,6,7,8,9,1,2},
{6,7,2,1,9,5,3,4,8},
{1,9,8,3,4,2,5,6,7},
{8,5,9,7,6,1,4,2,3},
{4,2,6,8,5,3,7,9,1},
{7,1,3,9,2,4,8,5,6},
{9,6,1,5,3,7,2,8,4},
{2,8,7,4,1,9,6,3,5},
{3,4,5,2,8,6,1,7,9}
};
}
    
asked by isaac garza 12.09.2017 в 22:16
source

1 answer

1

SUDOKU Rules:

  • Only numbers 1 to 9,
  • No repetitions in the same row,
  • No repetitions in the same column,
  • No repetitions in the same region (9 sub-matrices of 3 x 3).
  • :

    /* Leer sudoku, 
        como vamos a leer digitos (1-9), los leemos de caracter en caracter.
        Para convertirlos de Ascii a entero, simplemente restamos el Ascii del '0'
        Si nos dan algo que no sea dígito, simplemente no será contado abajo.
    */
    for(i=0;i<9;i++)
        for(j=0;j<9;j++)
        {
            scanf("%c",&c);
            a[i][j]=c-'0';
        }
    
    /* Valida filas
        Revisamos cara campo del arreglo y guardamos cuales digitos ya
        encontramos, activando el bit correspondiente en una bandera de bits.
        Solo usaremos 9 de los 32 bits, así que si están los 9 digitos, la
        bandera tendrá el valor 0x01ff. Usaremos este valor para comparar.
    */
    for(i=0;i<9;i++)
    {
        flag=0x0000;
        for(j=0;j<9;j++)
            flag|=1<<(a[i][j]-1);
        if(flag!=0x01FF)
            reporta("fila",i,j-1);
    }
    
    /* Valida columnas
        Hacemos lo mismo que en las filas, haciendo el recorrido ahora 
        de forma "vertical"
    */
    for(j=0;j<9;j++)
    {
        flag=0x0000;
        for(i=0;i<9;i++)
            flag|=1<<(a[i][j]-1);
        if(flag!=0x01FF)
        reporta("col",i-1,j);
    }
    
    /* Valida bloques (3x3)
        Hacemos lo mismo que en las filas, pero en bloques de 3x3.
    */
    for(si=0;si<3;si++)
    {
        for(sj=0;sj<3;sj++)
        {
            flag=0x0000;
            for(i=0;i<3;i++)
            {
                for(j=0;j<3;j++)
                    flag|=1<<(a[si*3+i][sj*3+j]-1);
    
            }
            if(flag!=0x01FF)
                reporta("bloque",si*3+i-1,sj*3+j-1);
        }
    }
    printf("\nEl sudoku es correcto");
    

    This solution is not your own, it was translated from English, you can see it here

        
    answered by 12.09.2017 / 23:37
    source