sudoku problem with rows/columns repeated

Asked

Viewed 267 times

3

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

As you all know sudoku starts with some numbers so I put them using the srand+Rand()%x and some numbers end up repeating themselves...

Another problem I’m having is with the other space I’d like to know what I do to not appear anything... I put NULL and there as I am using integers appears the number 0.

#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;
}
  • Try to separate the text better, it is difficult to read and understand the problem. The title does not briefly describe the problem. Maybe because you have more than one problem with the question.

  • 1

    I edited your question, as @bigown said, separated the text and put a title that describes the problem. Next time you’ll know what to do. Feel free to edit your question if you feel pertinent.

1 answer

3


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

As to the NULL best is to use zero as empty square indicator and print an empty square.

Set N to 9 so you can change as many squares without having to move 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][ ][ ][ ][ ][ ][ ][ ]

Online example

  • guy I changed the model since it is the pc that will play I will mecher with file and file.txt and put the rules =) any doubt I see yours as reference =0 thank you very much

  • hi @Jorge B. would like to keep in touch with you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.