Solving N-Queens Problem… How far can we go?

后端 未结 9 929
闹比i
闹比i 2020-12-09 03:23

The N-Queens Problem:

This problem states that given a chess board of size N by N, find the different permutations in which N queens can be placed on the board with

9条回答
  •  时光说笑
    2020-12-09 03:48

    Loren Pechtel said: "Now for some real insanity: 29 took 9 seconds. 30 took almost 6 minutes!"

    This fascinating lack of predictability in backtrack-complexity for different board sizes was the part of this puzzle that most interested me. For years I've been building a list of the 'counts' of algorithm steps needed to find the first solution for each board size - using the simple and well known depth-first algorithm, in a recursive C++ function.

    Here's a list of all those 'counts' for boards up to N=49 ... minus N=46 and N=48 which are still work-in-progress:

    http://queens.cspea.co.uk/csp-q-allplaced.html

    (I've got that listed in the Encyclopedia of Integer Sequences (OEIS) as A140450)

    That page includes a link to a list of the matching first solutions.

    (My list of First Solutions is OEIS Sequence number A141843)

    I don't primarily record how much processing time each solution demands, but rather I record how many failed queen-placements were needed prior to discovery of each board's algorithmically-first solution. Of course the rate of queen placements depends on CPU performance, but given a quick test-run on a particular CPU and a particular board size, it's an easy matter to calculate how long it took to solve one of these 'found' solutions.

    For example, on an Intel Pentium D 3.4GHz CPU, using a single CPU thread -

    • For N=35 my program 'placed' 24 million queens per second and took just 6 minutes to find the first solution.
    • For N=47 my program 'placed' 20.5 million queens per second and took 199 days.

    My current 2.8GHz i7-860 is thrashing through about 28.6 million queens per second, trying to find the first solution for N=48. So far it has taken over 550 days (theoretically, if it had never been uninterrupted) to UNsuccessfully place 1,369,331,731,000,000 (and rapidly climbing) queens.

    My web site doesn't (yet) show any C++ code, but I do give a link on that web page to my simple illustration of each one of the 15 algorithm steps needed to solve the N=5 board.

    It's a delicious puzzle indeed!

提交回复
热议问题