This problem is known as n queens puzzle: How can we distribute n queens on a chess grid of n $\times$ n so that no queen can threaten another.

Many solutions are possible for n > 4. To answer this question, show a single solution for n from 4 to 10 as well as an algorithm for where to place the queens.

  • Here is a solution when n = 8

8 x 8 solution

  • $\begingroup$ You can simply specify that solutions are to be simple algorithms and not require brute force approaches (i.e. be possible to implement with pen paper) $\endgroup$ – March Ho Feb 15 '15 at 10:26
  • $\begingroup$ it goes without saying :) $\endgroup$ – Abr001am Feb 15 '15 at 10:31
  • 1
    $\begingroup$ I do think you should clarify whether you mean how many ways it can be done, or if you want an algorithm that can provide just one solution in each case. $\endgroup$ – HKOB Feb 15 '15 at 13:45
  • 1
    $\begingroup$ I made the post look nicer, I hope you don't mind :D $\endgroup$ – warspyking Feb 16 '15 at 0:55
  • 2
    $\begingroup$ This is a well known problem for $8 \times 8$, which has 12 different solutions if rotations and reflections are considered identical. The one you post has a nice pattern, but not all do. It is not clear what the question is. $\endgroup$ – Ross Millikan Feb 18 '15 at 3:43

Single solutions are shown below for values of n from 4 to 10. Placement of the queens is based on the algorithm described in the Wikipedia page for the eight queens puzzle which is:

"Let (i, j) be the square in column i and row j on the n × n chessboard, k an integer.

  1. If n is even and n ≠ 6k + 2, then place queens at (i, 2i) and (n/2 + i, 2i - 1) for i = 1,2,...,n/2.
  2. If n is even and n ≠ 6k, then place queens at (i, 1 + (2i + n/2 - 3 (mod n))) and (n + 1 - i, n - (2i + n/2 - 3 (mod n))) for i = 1,2,...,n/2.
  3. If n is odd, then use one of the patterns above for (n - 1) and add a queen at (n, n)."

Chess4to7 Chess8to10

| improve this answer | |
  • $\begingroup$ yes this s the most optimal solution with the least complexity O(1) . good job . $\endgroup$ – Abr001am Feb 19 '15 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.