Я создал программу, которая позволяет пользователю вводить все значения головоломки sudoku, которая равна 9x9, сохраняет эти значения в массиве и может проверить, действительно ли значения во всех строки и столбцы разные, но мне трудно понять, как реализовать код для каждой подсеточной сетки 3х3. Я думаю, что я должен был бы иметь один последний вложенный цикл, который делится на 3, но я полностью застрял в этой части.Sudoku Checker: Доступ к 3g3-субграду в C
int main (void)
{
int n, i, j, x, array[9][9];
int check=0, sum;
//Enter how many puzzles user wants to solve.
scanf("%d", &n);
/*First for loop going from x to n is used so the user can enter
n amounts of puzzles and so the code doesn't try to
check n amount of puzzle inputs as one massive puzzle.*/
for(x=0;x<n;x++)
{
//Loop goes through all of the rows in the puzzle.
for(i=0;i<9;i++)
{
//Loop that goes through all of the columns in the puzzle.
for(j=0;j<9;j++)
{
scanf("%d", &array[i][j]);
}
}
//Checker for loops
for(i=0;i<9;i++){
/*Sum initalized to 0 through every iteration so that the
sum doesn't collect unneeded data from previous calculations
to determine if inputs are valid. */
sum=0;
/*Loop that goes through all columns individually and adds them up
totals of sum should equal 45 b/c (1+2+3+4...=45). If not check
is changed. */
for(j=0;j<9;j++)
sum+=array[i][j];
if(sum!=45){
check=1;
break;
}
}
/* Same loop but switches the i and j in the array[i][j] to check the rows */
for(i=0;i<9;i++){
sum=0;
for(j=0;j<9;j++)
sum+=array[j][i];
if(sum!=45){
check=1;
break;
}
}
for (row = (i/3) * 3; row < (i/3) * 3 + 3; row++){
sum=0;
for (col = (j/3) * 3; col < (j/3) * 3 + 3; col++)
sum+=array[i][j];
if(sum!=45){
check=1;
break;
}
}
//Check used to see if the inputs failed
if(check==0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
Я вижу, что вы говорите! спасибо, это очень помогло! – Perez