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
1
u/julesjacobs Sep 08 '11
Yes, it is. It is exactly rejection sampling with early stopping.
It highly depends on which order you iterate over the cells, which you didn't specify in your code. I can easily find an order of iteration that gives you a substantial probability of taking a billion years to fill a 20x20 grid. I can also easily give you a map on which any iteration order will take a billion years with substantial probability.