Question

I have currently been able to successfully make a sudoku code that randomly swaps columns randomly based on a random set of permutation, the permutation can only have the first 3 numbers within the first 3 numbers of the permutation in order to keep the board valid, for example {3,2,1,4,5,6,7,8,9} is valid but {4,2,3,1,5,6,7,8,9} is not, but . Now i must find a method to swap the stacks (every 3 columns is a stack) as well, this seemed a bit more difficult than swapping columns to me.

I have provided my column swapping code below, I have thought of the idea where instead of randomly permutating the columns 1-9 with a random permutation1 array as i did in this current code i should instead try to generate a random permutation array with random numbers between 1-9, however; instead i want to to only be able generate a random array such as {1,2,3,7,8,9,4,5,6} or {7,8,9,4,5,6,1,2,3}.

Can someone show me a way i could randomly generate 9 numbers but always have 1,2,3 in order, 4,5,6 in order, and 7,8,9 in order. If not could someone show me a more efficient way to do it then i have already proposed?

UPDATED CODE: The code i have updated does not seem to be able to read the permutation1 and permutation2 array that i define within each if/else-if statement.

#include <stdio.h>
#include <time.h>

int main(){
    int number, j, k;
    int p=0, q=0;
    int count=0;//initialize count
    int x, i;
    int m=0;
    int permutation1[9];
    int permutation2[3];

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

    srand(time(NULL));
    int randnum=rand()%6+1;

    if(randnum==1){
        permutation1[9]={1,2,3,4,5,6,7,8,9};
        permutation2[3]={1,2,3};
    }
    else if(randnum==2){
        permutation1[9]={1,2,3,7,8,9,4,5,6};
        permutation2[3]={1,3,2};
    }
    else if(randnum==3){
        permutation1[9]={4,5,6,1,2,3,7,8,9};
        permutation2[3]={2,1,3};
    }
    else if(randnum==4){
        permutation1[9]={4,5,6,7,8,9,1,2,3};
        permutation2[3]={2,3,1};
    }   
    else if(randnum==2){
        permutation1[9]={7,8,9,1,2,3,4,5,6};
        permutation2[3]={3,1,2};
    }
    else if(randnum==2){
        permutation1[9]={7,8,9,4,5,6,1,2,3};
        permutation2[3]={3,2,1};
    }

    for(k=1;k<10;k++){
        number=k;

        if(number == 1){
            for (j=0;j<9;j++)
                sudoku[(canonical[p]-1)][j] = number++;
            p++;
        }

        else {
            for (j=0;j<9;j++){
                sudoku[(canonical[p]-1)][j] = number++;
                if (number > 9)  {
                    number = number - 9;
                }
            }
            p++;
        }
    }

    for(i=0;i<9;i++){
        for (j=0;j<9;j++) {
            printf("%2d", sudoku[i][j]);
        }
        printf("\n");
    }

    printf("\nGiven the permutation: ");
    for(p=0;p<3;p++){
        printf("%2d", permutation2[p]);
    }
    printf("\n");

    for(q=0;q<9;q++){
        for(j=0;j<9;j++){
            printf("%2d",sudoku[q][permutation1[j]-1]);
        }
        printf("\n");
    }
    return 0;
}
Was it helpful?

Solution

Can someone show me a way i could randomly generate 9 numbers but always have 1,2,3 in order, 4,5,6 in order, and 7,8,9 in order?

There are only six permutations that meet your criteria. They are:

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

So choose a random number between 1 and 6, and use it to select the permutation you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top