gpt4 book ai didi

c - 数独检查器 : Accessing 3x3 subgrid in C

转载 作者:行者123 更新时间:2023-11-30 17:11:23 25 4
gpt4 key购买 nike

我创建了一个程序,让用户输入 9x9 数独谜题的所有值,将这些值存储在数组中,并可以检查所有行和列中的值是否不同,但我我无法理解如何实现代码来专注于 3x3 的每个子网格。我想我必须有最后一个嵌套的 for 循环,该循环可能会除以 3,但我完全陷入了这一部分。

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;

}

最佳答案

如果您将解决方案分解为处理问题的离散部分的函数,将会有所帮助。然后您可以编写一个函数来处理每个子网格。使问题更容易管理。例如:

#include <stdio.h>

void process_sub_grid(int array[9][9], unsigned int start_row,
unsigned int start_col)
{
unsigned int r, c;

printf("Processing sub-array rows %d-%d, cols %d-%d\n",
start_row, start_row + 2, start_col, start_col + 2);

for (r = start_row; r < start_row + 3; r ++) {
for (c = start_col; c < start_col + 3; c++) {
printf("Accessing array[%d][%d]\n", r, c);
}
}
}

int main(int argc, char *argv[])
{
int array[9][9];
unsigned int start_row, start_col;

for (start_row = 0; start_row < 9; start_row += 3) {
for (start_col = 0; start_col < 9; start_col += 3) {
process_sub_grid(array, start_row, start_col);
}
}
}

运行该程序会给出:

Processing sub-array rows 0-2, cols 0-2
Accessing array[0][0]
Accessing array[0][1]
Accessing array[0][2]
Accessing array[1][0]
Accessing array[1][1]
Accessing array[1][2]
Accessing array[2][0]
Accessing array[2][1]
Accessing array[2][2]
Processing sub-array rows 0-2, cols 3-5
Accessing array[0][3]
Accessing array[0][4]
Accessing array[0][5]
Accessing array[1][3]
Accessing array[1][4]
Accessing array[1][5]
Accessing array[2][3]
Accessing array[2][4]
Accessing array[2][5]
Processing sub-array rows 0-2, cols 6-8
Accessing array[0][6]
Accessing array[0][7]
Accessing array[0][8]
Accessing array[1][6]
Accessing array[1][7]
Accessing array[1][8]
Accessing array[2][6]
Accessing array[2][7]
Accessing array[2][8]
Processing sub-array rows 3-5, cols 0-2
Accessing array[3][0]
Accessing array[3][1]
Accessing array[3][2]
Accessing array[4][0]
Accessing array[4][1]
Accessing array[4][2]
Accessing array[5][0]
Accessing array[5][1]
Accessing array[5][2]
Processing sub-array rows 3-5, cols 3-5
Accessing array[3][3]
Accessing array[3][4]
Accessing array[3][5]
Accessing array[4][3]
Accessing array[4][4]
Accessing array[4][5]
Accessing array[5][3]
Accessing array[5][4]
Accessing array[5][5]
Processing sub-array rows 3-5, cols 6-8
Accessing array[3][6]
Accessing array[3][7]
Accessing array[3][8]
Accessing array[4][6]
Accessing array[4][7]
Accessing array[4][8]
Accessing array[5][6]
Accessing array[5][7]
Accessing array[5][8]
Processing sub-array rows 6-8, cols 0-2
Accessing array[6][0]
Accessing array[6][1]
Accessing array[6][2]
Accessing array[7][0]
Accessing array[7][1]
Accessing array[7][2]
Accessing array[8][0]
Accessing array[8][1]
Accessing array[8][2]
Processing sub-array rows 6-8, cols 3-5
Accessing array[6][3]
Accessing array[6][4]
Accessing array[6][5]
Accessing array[7][3]
Accessing array[7][4]
Accessing array[7][5]
Accessing array[8][3]
Accessing array[8][4]
Accessing array[8][5]
Processing sub-array rows 6-8, cols 6-8
Accessing array[6][6]
Accessing array[6][7]
Accessing array[6][8]
Accessing array[7][6]
Accessing array[7][7]
Accessing array[7][8]
Accessing array[8][6]
Accessing array[8][7]
Accessing array[8][8]

关于c - 数独检查器 : Accessing 3x3 subgrid in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32343262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com