r/programmingchallenges • • 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

38 comments sorted by

View all comments

Show parent comment

1

u/StoneCypher Sep 09 '11

Apparently it is exponential with a really bad constant.

May I see your implementation? I suspect a bug.

I should point out that the experiment you performed is not, in fact, his original claim, nor is it my restate of his original claim.

If you're not going to read the whole thing, consider also not taking a position.

2

u/awap Sep 09 '11 edited Sep 09 '11

The implementation is linked from my post.

I wasn't arguing for his original position, because he was making a silly pedantic remark: the obvious iteration order is to go either through them sequentially, either row-major or column major. Asserting that that's not what you meant just seems silly.

What I'm arguing against is the statement you made that iteration order doesn't matter. That statement stands on its own; I don't need to read the whole thread to see what you said. Also, I'm pretty sure it's wrong.

You should take his original suggestion and actually do the complexity analysis instead of just asserting that it must be fast on a gameboy or whatever.

Edit, Here's a hint: do the complexity analysis for a 2xN grid. Ignore the rows, and estimate how many backtracks are needed for a particular column (it's 2c). And every time we backtrack at a column, we have to redo all of the columns below it. It gets bad.

There are 2 obvious improvements to the algorithm. One is to use backjumping instead of ordinary backtracking to go directly to the cell that caused the conflict. The other is to use the current state of the cell to adjust our search order. Both of these might make it violate the uniformly random property that we are looking for. I'm not really sure, and it's time to get back to real work...

1

u/StoneCypher Sep 09 '11

The implementation is linked from my post.

Missed that. Sorry.

Asserting that that's not what you meant just seems silly.

Kay.

What I'm arguing against is the statement you made that iteration order doesn't matter. That statement stands on its own; I don't need to read the whole thread to see what you said. Also, I'm pretty sure it's wrong.

If you do read through, however, you get the context of that comment, and you come to understand its real meaning, instead of the incorrect meaning that is assigned to it by a superficial read out of context.

I said it didn't matter because the worst case was still so tractable.

You should take his original suggestion and actually do the complexity analysis

If you read the rest of the exchange, you'd learn that I did.

instead of just asserting

Oh right, I forgot, when you don't read a thread, it's reasonable for you to tell someone to do something that you imagine they might not have done.

Please stop criticizing your imagination of what I said. Read the rest.