Search code examples
calgorithmsudokufind-occurrences

How can I store the occurences of a number in a sudoku subgrid (3x3)?


So I've been trying to store all the occurences of numbers from 1 to 9 in an array. I've succeeded for rows and columns and tried to apply the same logic for subgrids of 3x3 but to no avail.

My sudoku grid is a 2D array 9x9 (purposefully put two 9 in the same subgrid to trigger the error message).

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

Here's the code of my function:

int no_conflict_3x3(int sudoku[N][N]){
    int occurrence[N] = {0};
        for (int l=0; l<3;l++){
            for(int k = 0; k<3; k++){
                for (int i=0+3*k; i<3*k; i++){
                    for(int j=0+3*l; j<3*l; j++){
                    printf("%d", sudoku[i][j]);
                        switch(sudoku[i][j]){
                case 1:
                    occurrence[0]++;
                    break;
                case 2:
                    occurrence[1]++;
                    break;
                case 3:
                    occurrence[2]++;
                    break;
                case 4:
                    occurrence[3]++;
                    break;
                case 5:
                    occurrence[4]++;
                    break;
                case 6:
                    occurrence[5]++;
                    break;
                case 7:
                    occurrence[6]++;
                    break;
                case 8:
                    occurrence[7]++;
                    break;
                case 9:
                    occurrence[8]++;
                    break;
                default:
                    break;
                }
            }
        }
        
        for (int o = 0; o<N; o++){
                printf("o = %d : occurence=%d | ", o+1, occurrence[o]); //DEBUG
            if (occurrence[o]>1){
                printf("Not a valid sudoku grid.\n");
                return 1;}
            else occurrence[o] = 0;
            }
            printf("\n");
         
     }
     }
        
        return 0;
}

and here's the result after one iteration (for one line):

o = 1 : occurence=0 | o = 2 : occurence=0 | o = 3 : occurence=0 | o = 4 : occurence=0 | o = 5 : occurence=0 | o = 6 : occurence=0 | o = 7 : occurence=0 | o = 8 : occurence=0 | o = 9 : occurence=0 |
 

I don't know why my variable occurrence is stuck at 0. Any help please?


Solution

  • You have 2 ignored loops since you loop from the start to the start. Your i and j loops are like:

    for (int i = 0; i < 0; ++i){...}
    

    It does nothing.

    Change

    for (int i=0+3*k; i<3*k; i++){
        for(int j=0+3*l; j<3*l; j++){
    

    to:

    for (int i=3*k; i<3*(k+1); i++){
        for(int j=3*l; j<3*(l+1); j++){
    

    Note the i < 3*k is changed to i < 3*(k + 1), similarly for j

    On the other hand, you count the 'golbal occurence', but you need them in every 3x3 subgrid...