Hacker News new | ask | show | jobs
by JoeAltmaier 3095 days ago
Reminds me of an old contest - the 8 Queens problem, posed by Byte Magazine in the '80s as a programming contest.

All the solutions presented started by declaring an 8X8 board, with a 1 or 0 in each cell to represent a queen. Then there was some two-dimensional iteration over the board, testing if any queen was on the same row, column or diagonal as any other.

I'd dismissed that solution as inefficient from the start. Since only one queen can be in each column, I represented the board as an 8-element array with each element containing the row number of the queen in that column.

My 2nd insight was, only one queen can be in each row, so the elements of the board array were simply the numbers 1..8. The search consisted of permuting the digits, then testing for diagonals e.g. test pairs to verify abs(difference) != abs(i1-i2). That is, their row numbers were not the same as the difference between their column indexes.

Finally, when permuting, you could test the elements as you went and trim the permutation tree drastically if you found a failure. No sense permuting the right-most elements of the array if there's already a diagonal conflict in the digits placed so far.

The whole thing ran in trivial time. The contest winners ran in hours or days (this was all in BASIC on <1Mhz processors back then). Made me wish I'd actually entered the contest. But I didn't bother, I guessed many people would have found the same solution.

2 comments

I entered a programming contest once, where the task was to take some input and construct an optimal solution to a geometric problem. I got irritated at the rules not being clear and well thought out, and I wasn't smart enough to work out a good implementation of the real-world algorithm they were hoping contestants would come up with. So I wrote code to provide pretty much the stupidest (most trivial) possible solution that fulfilled the specific constraints for a valid output that were stated. Because entrants were scored on a combination of speed, code size, and quality, my entry came in a close second, because it was by far the smallest and fastest. I kicked myself for not trying harder to optimize the quality even a little bit above "braindead".
dont think because its easy for you, it must be easy for everybody else. that thought held me back for quite a while until i recognized it was bogus.