Sudoku Programmers Forum Index

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in          Games  Calendar

Log in to check your private messagesLog in to check your private messages   

search() in DLX loops forever (code inside)

 
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Solving sudoku
View previous topic :: View next topic  
Author Message
veroslav

Joined: 05 Jun 2006
Posts: 7
:

Items
PostPosted: Tue Jul 25, 2006 5:23 pm    Post subject: search() in DLX loops forever (code inside) Reply with quote

I have a weird problem that only occurs sometimes. I am using my DLX solver in order to generate puzzles and it works good, most of the time. However sometimes when checking if entered numbers in the board lead to a solution, search() - method never returns and just loops. I am stunned as it is very hard to find the error, as it works 90% of the time, with the solver being called several times and performing just fine. Here is my code:

Code:

private void search(int k) {
         if(masterHeader.getRight() == masterHeader) {
            ++nbrSolutions;
            if(showSolution)
               printSolution(k);
            if(nbrSolutions == 2) {
               multipleSolutions = true;
            }
            return;
         }
         X ok = null;
         X c = chooseColumnObject();
         coverColumn(c);
         for(X r = c.getLower(); r != c; r = r.getLower()) {
            ok = r;
            solution.add(k, ok);
            for(X j = r.getRight(); j != r; j = j.getRight()) {
               coverColumn(j.getListHeader());
            }
            if(!multipleSolutions)
               search(k+1);
            r = solution.get(k);
            c = r.getListHeader();
            for(X j = r.getLeft(); j != r; j = j.getLeft()) {
               uncoverColumn(j.getListHeader());
            }
         }
         uncoverColumn(c.getListHeader());
      }

private X chooseColumnObject() {
         X smallest = masterHeader.getRight();
         int s = smallest.getSize();
         int currentSize;
         
         for(X j = smallest; j != masterHeader; j = j.getRight()) {
            currentSize = j.getSize();
            if(currentSize == 1)
               return j;
            else if(currentSize < s) {
               smallest = j;
               s = currentSize;
            }
         }
         
         return smallest;
      }
      
      private void coverColumn(X c) {
         c.getRight().setLeft(c.getLeft());
         c.getLeft().setRight(c.getRight());
         
         for(X i = c.getLower(); i != c; i = i.getLower()) {
            for(X j = i.getRight(); j != i; j = j.getRight()) {
               j.getLower().setUpper(j.getUpper());
               j.getUpper().setLower(j.getLower());
               j.getListHeader().decreaseSize();
            }
         }
      }
      
      private void uncoverColumn(X c) {
         for(X i = c.getUpper(); i != c; i = i.getUpper()) {
            for(X j = i.getLeft(); j != i; j = j.getLeft()) {
               j.getListHeader().increaseSize();
               j.getLower().setUpper(j);
               j.getUpper().setLower(j);
            }
         }
         c.getRight().setLeft(c);
         c.getLeft().setRight(c);
      }


I am thankfull for any help, as I have been debugging for several days now and I am getting tired. Thanx in advance.

/Veroslav
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Solving sudoku All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Sudoku Programmers topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group

Igloo Theme Version 1.0 :: Created By: Andrew Charron