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 :)
22
Upvotes
View all comments
Show parent comment
1
u/StoneCypher Sep 08 '11
No, it isn't.
No, it won't be. This is a depth first search of an extremely sparse tree. In the grid example context of the original article, it will backtrack zero times per run. It takes a very complicated map to get even a trivial number of backtracks, and even with a relatively large number of backtracks, the work is log proportional.
This is going to be lightning fast on gigantic maps on trivial hardware. Stop pretending to see difficulties that aren't actually there and just try running it.
Why would I want to? You should be able to color a 20k cell hex map this way on a GameBoy Advance (16mHz ARM7, ~800 cycles/cell) in under a second.