r/programmingchallenges • u/babygame • Jun 01 '11
Coloring a grid
Make a program to color in the squares of an (n x m) grid (with some number of colors > 2) such that no two same colors touch.
There is a 'brute force' way, and (at least one) more 'elegant' solution as well.
Edit: I didn't specify-- try one that will output a different pattern each time. It's a nicer problem, I think :)
21
Upvotes
View all comments
Show parent comment
2
u/julesjacobs Sep 08 '11
Right well. I do mathematics, and the well known technique to solve this class of problems mathematicians call rejection sampling. It seems that you are from a different field, so sorry to have confused you with this terminology.
No need to resort to ad-hominems, it just makes one seem stupid. Since the order of iteration is the crucial element of your algorithm, you should not leave the critical part out of the description.
Okay, I'll show you. If you first iterate over all the even columns, and then all the odd columns, chances are good that your algorithm will take exponential time. If you don't believe me then try it. Or iterate over the grid randomly, nearly all iteration orders will result in exponential runtime of your algorithm.
Since this problem is NP-complete, take any reasonably sized graph coming from the reduction of a hard SAT instance to graph coloring.
If your algorithm really works as well as you think it does, go collect your Turing award and $1M Clay prize.