sudoku problem with repeated rows / columns

3

Well I'm doing a college job which is to make a sudoku in which the PC plays, first I'm creating the rules of the game which is no number can be repeated on the line nor in the row where it is .. .

As everyone knows sudoku starts with some numbers so I put them using srand + rand ()% x and some numbers end up repeating themselves ...

Another problem I am having is with the other space I would like to know what I do so nothing appears ... I put NULL and as I am using integers the number 0 appears.

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

int main()
{
    int su[9][9];
    int i, j;
    int cont;

    srand( (unsigned)time(NULL) );//evita que o rand seja gerado pelo tempo

    for(i=0; i<9; i++)
    {
        for(j=0; j<9; j++)
        {
            cont=rand()%9;

            if(i==cont || j==cont)
                su[i][j]=1+rand()%9;
            else
            {
                su[i][j]=NULL;
            }
            if( su[i][j]==su[i+1][j+1])
            {
                su[i][j]=NULL;
            }
        }
    }

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

    /*for(i=1 ; i <= 10 ; i++)
        printf("Numero %d: %d\n",i, 1+rand()%9);*/


    return 0;
}
    
asked by anonymous 05.11.2014 / 01:18

1 answer

3

Of course you have to check when inserting a value if it already exists on the same row and in the same column before inserting. For this I created the function isValueInLineOrColumn that verifies whether or not the value already exists in the row / column.

As far as NULL is even better, use zero as an empty square indicator and print an empty square.

I also set the N value to 9 so that you can change the maximum of squares without having to tinker with the code.

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

#define N 9

int isValueInLineOrColumn(int val, int arr[N][N], int line, int column)
{
    int i,j;

    for (i=0; i < N; i++)
    {
        if(arr[line][i] == val)
            return 1;

        if (arr[i][column] == val)
            return 1;
    }

    return 0;
}

int main()
{
    int su[N][N];
    int i, j;
    int cont;

    srand( (unsigned)time(NULL) );//evita que o rand seja gerado pelo tempo

    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
        {
            cont=rand()%N;

            if(i==cont || j==cont)
            {

                int val = 1+rand()%N;

                //verifica se já existe este valor na linha ou coluna atual.
                if(isValueInLineOrColumn(val, su, i, j))
                    su[i][j]=0;
                else
                    su[i][j]=val;

            }
            else
            {
                //usando 0 como identificador de vazio
                su[i][j]=0;
            }
        }
    }

    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
        {
            if(su[i][j]!=0)
                printf("[%i]", su[i][j]);
            else
                printf("[ ]");//se for igual a 0 coloca o quadrado vazio.
        }
        printf("\n");
    }

    /*for(i=1 ; i <= 10 ; i++)
        printf("Numero %d: %d\n",i, 1+rand()%9);*/


    return 0;
}

One of the results:

[5][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][3][ ]
[ ][7][ ][ ][9][ ][ ][1][ ]
[6][ ][4][ ][ ][ ][1][ ][ ]
[ ][ ][2][6][ ][ ][4][ ][ ]
[ ][ ][ ][ ][ ][ ][5][ ][ ]
[ ][3][ ][ ][ ][ ][ ][ ][ ]

Example online

    
05.11.2014 / 11:48