Algorithm for finding 1000 queens on a chessboard

Recently I was looking into my old developments / scripts and came across a script where the queens problem was solved. Actually, this served to write an article about how the stages of writing his algorithm went. Perhaps useful for novice programmers to solve similar problems (the code in the examples is written in java).





Introduction

4 years ago there was a stir about the problem of placing 1000 queens on a 1000x1000 board. The fact is that placing queens so that they are not



each other on the board is a problem with a large number of iterations and, as a result, a long execution time. Like many others, I wanted to check if it could be solved in an acceptable time.





Study the task

There are 8 queens in the picture that do not intersect along horizontal, vertical or diagonal lines.





It is necessary to write a script that will place queens on the board according to these rules.





Algorithm for finding queens

Recursion was chosen to search for shapes.





Description of the method that calls itself:





  • If the passed cell intersects with other figures, then we return false







  • If the transferred cell does not intersect with anyone:





    • true



      .





    • ( ) true



      .





    • .





      • false



        false



        ) false



        .





      • true



        .





8x8, 32x32, 50x50 . .





Cells on which a piece cannot be placed (they are under attack by other queens) are marked with red.
( ).





.

.





.

. .

.





400x400.





.

.

.







.

.

"row+1" "column+2" , .





1000x1000 ~4 ( : Intel Core i5-10400H CPU 2.60GHz).





1116x1116 ~6 .












All Articles