is there anything wrong in sudoku logic ?

This is a discussion on is there anything wrong in sudoku logic ? within the C Programming forums, part of the General Programming Boards category; I'm using brute force algo. here PossValue() tells whether the number N can be place or not. It returns 1 ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    is there anything wrong in sudoku logic ?

    I'm using brute force algo.
    here PossValue() tells whether the number N can be place or not.
    It returns 1 if it can be placed.. it returns 0 if it can't

    - What I tried to do here is...
    go to an empty cell (I took 0 as i/p for empty ) and place a possible value N.
    then go to next empty cell and do it..
    if there exists a cell that can't take any value from 1-9 then go back to previous cell and choose another possible value ( this I did by incrementing from where I stopped..)..

    This is my code..
    when I compile and provided i/p its not doing nothing.. neither displaying o/p nor showing error not getting into infinite loop..

    Code:
    // initial value of K is 0
    
    for (i=0; i<9; i++) 
      {   printf("?");
          for (j=0; j<9; j++) 
          {
              if (sudoku[i][j] == 0)
              {   printf("(%d,%d)",i,j);
                  for (n=1+k; n<10; n++)
                  {   
                      flag1 = 0;
                      if ( PossValue(i,j,n) == 1 )
                      {
                          sudoku[i][j] = n;
                          break;
                      }
                      else if (PossValue(i,j,n) == 0)
                      {
                          flag1 = 1;
                      }
                  }
              }
              if (flag1 == 1)
              {
                 if(j >= 1)
                 {
                    j = j-1;
                    k = sudoku[i][j];
                 }
                 else if (j=0)
                 {
                    i = i-1;
                    j = 8;
                    k = sudoku[i][j];
                 }
              }       
          }
      }
    Last edited by suryak; 08-09-2011 at 12:22 AM. Reason: not included info

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > else if (j=0)
    Well this will mess up the logic.
    Try ==

    Here's a first test.
    Create a completed sudoku grid, remove just one number, and then see if your program can 'solve' it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    No, Salem..
    It the same..

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    but the rest of the code is fine ??
    If so, I'll look at other part.. but right now .. I am not able to find.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Did you try my test?
    Did it work?
    If not, then use the debugger to step through the code. It shouldn't take too long to step through the code to where it's trying to fill in the single missing cell.



    BTW, most of the effort is down to you.

    Most helpers don't have the time to construct a test program around a single function, then guess what input data might make the code fail in the exact way that you're seeing. If it's anything more complicated than copy/paste, don't expect anyone to bother with trying to RUN your code. At best, you're just going to get a few minutes of eyeball time over the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    The problem probably cannot be solved from only that limited code snippet.
    Fix the problem you've been told about and and make sure that you don't decrement i below zero.
    Oh and the way you are trying to backtrack will not work anyway. You aren't undoing all of the wrong moves made. Recursion would be a better option.
    Last edited by iMalc; 08-09-2011 at 01:22 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sudoku
    By ElemenT.usha in forum C++ Programming
    Replies: 22
    Last Post: 02-14-2011, 09:46 PM
  2. what is wrong with the logic in my loop?
    By tranman1 in forum C Programming
    Replies: 4
    Last Post: 03-10-2010, 07:48 PM
  3. Sudoku
    By mrusnak in forum C Programming
    Replies: 10
    Last Post: 03-20-2009, 06:31 AM
  4. Replies: 5
    Last Post: 01-31-2006, 12:54 AM
  5. Logic all wrong
    By nizbit in forum C Programming
    Replies: 4
    Last Post: 10-17-2004, 05:15 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21