Thread: Please help me with SUDOKU

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    1

    Please help me with SUDOKU

    Hi Fellas.
    I'm in trouble to make a way to solve my sudoku,and thats is my doubt.

    I need to solve this Sudoku doing in parts,like,first per section,than per row and last per column.I need to minimize the possibility of answers until i got the right answer for the index.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    //This is for read the initial puzzle.
    //The progam asks to someone digit the numbers of points that will be already revealed.
    void LeTabuleiroInicial()
    {
       int iNumDicas;
       int iIndex;
       int iElemento, iValue;
       int iIndexLinha,iIndexColuna;
              
              printf("\nEnter the number of points to be disclosed:");
              scanf("%d",&iNumDicas);
    
    // Preparing the array for begin
    for (iIndexLinha = 0; iIndexLinha < 81; iIndexLinha++)
    {
    // The possibility for each element is labeled as 9, this mean that the solution may be any digit [1-9]
    iPuzzle[iIndexLinha][0]=9;
    // There's no answer revealed
    iPuzzle[iIndexLinha][10]=0;
    // Declares column of each digit [1-9] as a candidate answer.
    for (iIndexColuna = 1; iIndexColuna < 10; iIndexColuna++)
    iPuzzle[iIndexLinha][iIndexColuna]=1;
    }
    //Reading of n elements revealed by the User and their sensitization  at the matrix iPuzzle
    //The reading consider index of the element varying in the range [1-81] and the value ranging from [1-9].
    for (iIndex = 0; iIndex < iNumDicas; iIndex++)
    {
    printf("Enter in sequence the index of the element and its value:");
    scanf("%d %d", &iElemento, &iValue);
    //Records 0 in column of all digits ,indicating that there is no answer candidate
    for (iIndexColuna = 1; iIndexColuna < 10; iIndexColuna++)
    iPuzzle[iElemento-1][iIndexColuna]=0;
    //Records that the element has only one possible solution
    iPuzzle[iElemento-1][0] = 1;
    //Records the revealed solution for the element
    iPuzzle[iElemento-1][10] = iValue;
    //Records 1 on the colunm of respective  revealed digit
    iPuzzle[iElemento-1][iValue] = 1;
    }
    }
    
    void printTabuleiro()
    {
      int index;
      int index2;
    
             printf("\n-------------------------\n|");
    
    for (index = 0; index<81; index++)
    {
    if (iPuzzle[index][0]==1)
    {
    for(index2 = 1; index2 <10; index2++)
    {
    if (iPuzzle[index][index2]==1)
    {
    printf(" %d", index2);
    break;
    }
    }
    }
    else printf(" %c", 64 + iPuzzle[index][0] );
    if ((index + 1) % 3 == 0)
    printf(" |");
    if (index + 1 == 81)
    printf("\n-------------------------\n");
    else if ((index + 1) % 27 == 0)
    printf("\n-------------------------\n|");
    else if ((index + 1) % 9 == 0)
    printf("\n|");
    }
    }
    int resolverSudoku()
    {
    int i = 1;
    do
    {
    reduzirPossibilidadesPorSecao();//Solving by section
    reduzirPossibilidadesPorLinha();//Solving by row
    reduzirPossibilidadesPorColuna();//Solving by column
    i++;
    }
    while (resolvido()!=1 && i<150);
    if (i>150)
    printf("Erro - Apos 150 iteracoes a solucao nao converge.");
    }
    
    int main (){
    
    int iPuzzle[81][11];
    
    LeTabuleiroInicial();
    printTabuleiro();
    resolverSudoku();
    printTabuleiro();
    
    return 0;
    }
    Sorry for my bad english.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by teuzz View Post
    Sorry for my bad english.
    You better be sorry for no indentation
    You have explained what you need to do - but forgot to explain what is your problem. Do you have compilation error/warnings? Does your program run but gives incorrect result?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Well, I doubt you'll find anyone willing to write a sudoku solver from scratch for you.

    Have a look here for strategies to solve sudoku puzzles.

    Generally speaking

    1) Keep track of what values are feasible for each cell.
    2) For those cells where values are given reduce the set of feasible values to the one specified.
    3) Repeatedly apply applicable strategies to reduce the number of feasible values in each cell, until no possible reductions occur. [These strategies are the subject of the link I gave above].
    4) If no reductions are possible and any cell has more than one possible value, the puzzle is not solvable using your set of strategies. Otherwise the problem is solved.

    The trick is representing the puzzle in a way so the strategies take into account groupings (i.e. rows include all numbers 1-9, as do 3x3 squares) of groups of groups (which is what some of the advanced strategies work with) - and that's where you need to spend time in designing your program. Although it's not strictly necessary, it is probably better to only apply advanced strategies if simpler strategies don't help, and only fall back on guesswork/back-tracking, etc if all other systematic strategies fail. This corresponds with most measures of puzzle difficulty and with how people often solve puzzles (a puzzle that can be solved with simple approaches is easier than a puzzle which can only be solved with the help of advanced strategies, and a puzzle where it is necessary to fall back on guesswork and backtracking are more difficult again [or deemed dirty by purists who insist that guesswork should not be needed]).
    Last edited by grumpy; 11-10-2013 at 12:09 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sudoku in c
    By Melamust in forum C Programming
    Replies: 10
    Last Post: 05-23-2013, 09:51 AM
  2. Sudoku
    By code_lover in forum C Programming
    Replies: 9
    Last Post: 09-16-2011, 11:19 AM
  3. Like Sudoku, but not.
    By quzah in forum Game Programming
    Replies: 4
    Last Post: 08-27-2011, 05:33 AM
  4. sudoku
    By ElemenT.usha in forum C++ Programming
    Replies: 22
    Last Post: 02-14-2011, 10:46 PM
  5. Sudoku
    By mrusnak in forum C Programming
    Replies: 10
    Last Post: 03-20-2009, 06:31 AM