数独检查器:在C中访问3x3子网格

时间:2015-09-02 01:21:53

标签: c arrays grid sudoku

我创建了一个程序,允许用户输入数独游戏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;

}

1 个答案:

答案 0 :(得分: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]