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 :)
20
Upvotes
View all comments
Show parent comment
3
u/awap Sep 09 '11 edited Sep 09 '11
I don't know about all of the other stuff he said, because frankly this exchange was too long and mean spirited to read through the whole thing. However, it really seemed like he was right about the importance of picking a good search order.
This really did not seem right to me. I mean, hell, 2 coloring a grid is like an undergrad algo-class example of why depth-first search can bite you.
And since I was bored and am waiting for a phone call, I implemented the algorithm you described using the checkerboard pattern search. I did it in C++ so there would be no complaints about python causing the slowness or anything. If you have a problem with the implementation you can bring that up. However, it seems pretty clear that, as implemented, this is an exponential time algorithm.
Results:
The 8x8 one ran for a few minutes, then I decided to kill it and try expanding just one dimension at a time:
6x10 also took too long. Apparently it is exponential with a really bad constant. I don't really feel like doing the math to figure out what it is, but it's bad.
The 4xC numbers: