Matrix comparison error

0

The question asks for a comparison of arrays, I tried to compare them, but the answer if they are the same appears several times and I do not know how to solve this problem, is what is causing the 10% error that the uri is reporting.

Here the link to the question

My code:

#include <stdio.h>

int i, j, teste, a;
int main()
{
  int matriz[9][9] = {{1, 3, 2, 5, 7, 9, 4, 6, 8},
                        {4, 9, 8, 2, 6, 1, 3, 7, 5},
                        {7, 5, 6, 3, 8, 4, 2, 1, 9},
                        {6, 4, 3, 1, 5, 8, 7, 9, 2},
                        {5, 2, 1, 7, 9, 3, 8, 4, 6},
                        {9, 8, 7, 4, 2, 6, 5, 3, 1},
                        {2, 1, 4, 9, 3, 5, 6, 8, 7},
                        {3, 6, 5, 8, 1, 7, 9, 2, 4},
                        {8, 7, 9, 6, 4, 2, 1, 5, 3}
                    }, sudoku[9][9];

    scanf("%d", &teste);
    for(a = 0; a < teste; a++)
    {
        for(i = 0; i < 9; i++)
        {
            for(j = 0; j < 9; j++)
            {
                scanf("%d", &sudoku[i][j]);
            }
        }

        for(i = 0; i < 9; i++)
        {
            for(j = 0; j < 9; j++)
            {
                if(matriz[i][j] != sudoku[i][j])
                {
                    printf("Instancia %d\n", a + 1);
                    printf("NAO\n\n");
                }
                else
                {
                    printf("Instancia %d\n", a + 1);
                    printf("SIM\n\n");
                }
            }
        }
    }

    return 0;
}
    
asked by anonymous 28.12.2017 / 21:17

1 answer

0

The answer is shown several times because it is inside the loop, you should change to save the result in a flag and ask outside the loop (second group of loops), like this:

//  Continua no laço enquanto encontrar valores iguais.
bool bIguais = true;
for(i = 0; i < 9 && bIguais; i++)
{
    for(j = 0; j < 9 && bIguais; j++)
    {
        bIguais = matriz[i][j] != sudoku[i][j];
    }
}

if(bIguais)
{
    printf("Instancia %d\n", a + 1);
    printf("NAO\n\n");
}
else
{
    printf("Instancia %d\n", a + 1);
    printf("SIM\n\n");
}
    
29.01.2018 / 17:03