views:

219

answers:

6

Here's a good one to reflect on:

http://en.wikipedia.org/wiki/Kakuro

I'm attempting to make a solver for this game. The paperwork is done (reading an initial file with a variable number of columns and rows. It's assumed the input file follows the rules of the game so the game is always solvable. Take your time to read the game rules.

I've taken care of the data structure which I think will suit best:

struct aSquare { int verticalSum; int horizontalSum; int value; }

And made an "array" of these dynamically to work on. I made it so that the black squares have value of -1 and white squares (the actual solution squares) initialize at 0. You can also get the position of each aSquare struct from the array easily, no need to make additional struct fields for it.

Now the algorithm ... How in the world will I conciliate all these sums and find a general way that will solve all types of grids. I been struggling with this all afternoon to no avail.

Help is appreciated, have fun!

*EDIT: I just realized the actual link I posted has some tips regarding solving techniques. I will still keep this up to see what people come up with.

A: 

This is straight-forward linear algebra, use vector/matrix manipulation techniques to solve.

[edit - answering the comments]

a + b + 0 + d + 0  = n1
0 + b + c + 0 + e  = n2
a + 0 + c + 0 + 0  = n3
a + b + c + 0 + e  = n4
a + 0 + c + d + 0  = n5

Above is converted to a matrix, and by adding and subtracting multiples of the rows, you end up with:

a 0 0 0 0 na
0 b 0 0 0 nb
0 0 c 0 0 nc
0 0 0 d 0 nd
0 0 0 0 e ne

No combinatorics, all remain integers.

slashmais
Using linear algebra you typically won't end up with an integer solution. Combinatorics needs to enter in somehow.
Philip Starhill
Linear algebra cannot represent the rule of no digit duplication.
liori
In your example, there is an elimination order where the solution is all integers, but finding such an order for a general 0-1 matrix is not easy. Try carrying out Gaussian Elimination in the order you present, you'll find that 1/2 creeps in. In this case, a good order is easy to find (one example is a, b, d, c, e), but in general you would need to be able to backtrack in the GE in case a later matrix element turns out to be fractional due to an earlier decision.
Philip Starhill
Remembered this technique, thought it could be of help. (Actually forgot it was Gauss - thanks)
slashmais
+2  A: 

If your interest is ultimately in making a software solver for these games, but not getting into the algorithmic details, I recommend using a Constraint Programming (CP) engine. CP is a declarative programming paradigm that is very well suited to these sorts of problems. Several commercial and open source CP engines are available.

http://en.wikipedia.org/wiki/Constraint_programming

Philip Starhill
It's actually just this game at the moment. I was actually trying to make something more complex for myself. Thanks for the tip though
Queops
+5  A: 

A simple brute-force solver for Sudoku takes miliseconds to run, so you don't need to bother implementing any special tactics. I think that in case of Kakuro this will be the same. A simple algorithm:

def solve(kakuro):
    if kakuro has no empty fields:
        print kakuro
        stop.
    else:
        position = pick a position
        values = [calculate possible legal values for that field]
        for value in values:
            kakuro[position] = value
            solve(kakuro)
        kakuro[position] = None # unset after trying all possibilities

This algorithm might work better if you find the best order of fields to fill. Try to choose fields which will be the most constrained (as in: there are not many values that are legal).

Anyway, this will be probably the simplest to implement, so try it and look for more sophisticated solvers only if this one will not work. (Actually this is one of the simplest constraint programming solvers; real CP solvers are much more complicated, of course).

liori
Note that there is a lot of room for cleverness in the "[calculate possible legal values for that field]" step.
Brian
@Brian: more for picking a position, as in many algorithms this might mean going from polynomial to exponential time. Kakoru is NP-complete, but still choosing the order wisely might make constants much smaller. And calculating possible legal values should be constant time with proper data structures, anyway.
liori
@liori: Fair enough. In a solver I designed (for a different puzzle), one trick I used was to pick **every** position and try it for one iteration, using proof by contradiction to reduce the number of possible legal values. I also filled in all spaces which had only one possible legal value. Thus, my puzzle had a de facto `[calculate possible legal values for every field]` function. My position picker for making guesses had no intelligence at all, and didn't really need it. To be fair, Nurikabe is a binary puzzle, so all positions are equally constrained.
Brian
+1  A: 

I would guess that Linear Programming can be easily used to solve this kind of game.. then this is an integer problem for which exact solutions does exist.. (branch and bound? cutting-plane?)

In any case using a table with the most certain combinations will be useful for sure (eg http://www.enigmoteka.com/Kakuro%20Cheatsheet.pdf)

Jack
+1 for the cheatsheet/table.
LarsH
A: 

I have found some nice bit-manipulation tricks that speed up Kakuro solving. You can pick up the source here.

zvrba
+4  A: 

Regarding Constraint Programming: Here are some different implementations of how to solve a Kakuro puzzle with Constraint Programming (all using the same basic principle). The problem instance is fixed in the program.

hakank
Useful links, thanks!
Queops