http://qs1969.pair.com?node_id=600439


in reply to decomposing binary matrices

This may be completely off target but the problem reminds me of the mental gymnastics used when solving Sudoku puzzles. I saw a Perl Sudoku solver once and the author had employed the Quantum::Superpositions module and it's any and all functions. Just a thought but perhaps something in there could help you.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: decomposing binary matrices
by Jenda (Abbot) on Feb 16, 2007 at 23:16 UTC

    I bet Sudoku is behind this thread. I wrote a bruteforce solver last week, takes about 10 seconds for the "hard" ones on my three years old PC. I'll post it once I get to the office. I do have some more ideas, a wee bit less bruteforcish, though not at all similar to this.

    Update: Here is the blindly bruteforcish implementation. Needs about 11s to solve the "hard" sudokus on my Pentium 4. Making something like 400000 "what numbers are allowed on this place so far" tests.

    Update 2: Here is the promised improved version. It's quite similar except that there is an additional step. Whenever I make a guess I scan the whole plan for fields that are clear according to the fields already set and continue scanning and setting fields until there are no more clear ones and I have to find the next unset field in which I have to guess. This has brought the time from 11s down to 0.3s.

      From Limbic~Region's reply, it looks like you might have lost your bet. I too have a brute force solver that I wrote a couple of years ago. It would be interesting to compare our approaches so I'll dig my version out and post it as well. I actually wrote it before I had got into solving the puzzles by hand so I ought to have a go at refining it now that I have more strategies to hand.

      Cheers,

      JohnGG

      Update: Here is my brute-force Sudoku solver. Although it has to resort to backtracking if it makes a wrong guess it is fairly efficient because it sorts the empty squares by the number of possible numbers for each square before making a guess. Thus, for a lot of the time, it will make the right choice and it also updates everything and re-sorts after each choice. It will also detect an unsolveable puzzle very quickly as there will be a square with no possible number.

      It uses Term::ANSIColor to prettify the output but I'm not sure if that works in Windows terminals. Here's the code

      It reads the puzzle to be solved from a file specified on the command line and here's an example using the same puzzle grid as Jenda used.

      5...7.682 ...596... ......... ....8..49 .36...... ......... ..8..7..1 ..3..4..7 64.3...2.
      It seems to be a bit quicker than Jenda's solver but, as I've said, the guessing is somewhat optimised and it wasn't anything like as fast when first written.

      Cheers,

      JohnGG

      Jenda,
      I will take that bet. My guess is that it is for a number crossword solver. Of course, I already asked hv in the CB so what do I win?

      Cheers - L~R