简体   繁体   中英

checking validity of Sudoku Board. C program

I am currently creating a project that checks if a given 9x9 sudoku table follows the rules of sudoku. The table is stored in a multidimensional array (board[9][9])I am stuck on how to correctly check through each 3x3 sub-grid for duplicate numbers. I have already established checks for numbers outside of 0(blanks) and 9(max value) as well as checks for duplicates in each row and column. I have managed two create 4 nested for loops that successfully loop through each 3x3 box, but I am unsure how to check the values in the box for duplicates. My definition of a duplicate is two cells that contain the same number (other than zero which is reserved as blanks in my program). Below is my code so far. This code checks that each number in the first column of each 3x3 subgrid is valid but fails to check the other 2 columns of each box.

for(int x=0;x<9;x=x+3){                 //iterates through each 3x3 sub box going through each row. Temp B compares each value to all other values in box.
    for(y=0;y<9;y=y+3){
        for(j=x;j<x+3;j++){
            tempB = board[j][k];
                for(k=y;k<y+3;k++) {
                    for (z = x; z < x + 3; z++) {
                        if (z != j && tempB == board[z][k] && tempB != 0) {
                            return 0;
                        }
                    }
                }
        }
    }
}

Why nobody provides a MCVE ever?

As Yunnosch suggested you can just count occurrences of the numbers in an array. Remember to skip empty cells (0).

#include <stdio.h>

int board_noduplicates(int board[9][9]) 
{
    for (int x = 0; x < 9; x += 3) {
        for(int y = 0; y < 9; y += 3) {
            int count[10] = { 0 };
            for(int j = x; j < x + 3; ++j) {
                for(int k = y; k < y + 3; ++k) {
                    int cur = board[j][k];
                    if(cur > 0 && count[cur] > 0) {
                        return 0;
                    }
                    ++count[cur];
                }
            }
        }
    }
    return 1;
}

int main(void) 
{
    int board1[9][9] = {
        {1,2,3, 0,0,0, 4,5,6,},
        {0,0,0, 1,0,0, 0,0,0,},
        {0,0,0, 0,0,8, 0,0,0,},

        {4,5,6, 2,0,0, 7,8,9,},
        {0,0,0, 0,1,0, 0,0,0,},
        {0,0,0, 0,0,3, 0,0,0,},

        {7,8,9, 4,0,0, 1,2,3,},
        {0,0,0, 0,5,0, 0,0,0,},
        {0,0,0, 0,0,6, 0,0,0,},
    };
    int board2[9][9] = {
        {1,2,3, 0,0,0, 4,5,6,},
        {0,0,0, 1,0,0, 0,0,0,},
        {0,0,0, 0,0,8, 0,0,0,},

        {4,5,6, 2,0,0, 7,8,9,},
        {0,0,0, 0,1,0, 0,0,0,},
        {0,0,5, 0,0,3, 0,0,0,}, // <-- duplicate

        {7,8,9, 4,0,0, 1,2,3,},
        {0,0,0, 0,5,0, 0,0,0,},
        {0,0,0, 0,0,6, 0,0,0,},
    };  
    
    printf("board_noduplicates(board1) -> %d\n", board_noduplicates(board1));
    printf("board_noduplicates(board2) -> %d\n", board_noduplicates(board2));
    
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM