

Solve the eight queens puzzle.



You can extend the problem to solve the puzzle with a board of size   NxN.

For the number of solutions for small values of   N,   see   OEIS: A000170.



Related tasks

 A* search algorithm
 Solve a Hidato puzzle
 Solve a Holy Knight's tour
 Knight's tour
 Peaceful chess queen armies
 Solve a Hopido puzzle
 Solve a Numbrix puzzle
 Solve the no connection puzzle


      Size% = 8
      Cell% = 32
      VDU 23,22,Size%*Cell%;Size%*Cell%;Cell%,Cell%,16,128+8,5
      *font Arial Unicode MS,16
      GCOL 3,11
      FOR i% = 0 TO Size%-1 STEP 2
        RECTANGLE FILL i%*Cell%*2,0,Cell%*2,Size%*Cell%*2
        RECTANGLE FILL 0,i%*Cell%*2,Size%*Cell%*2,Cell%*2
      NEXT
      num% = FNqueens(Size%, Cell%)
      SYS "SetWindowText", @hwnd%, "Total " + STR$(num%) + " solutions"
      REPEAT : WAIT 1 : UNTIL FALSE
      END
 
      DEF FNqueens(n%, s%)
      LOCAL i%, j%, m%, p%, q%, r%, a%(), b%(), c%()
      DIM a%(n%), b%(n%), c%(4*n%-2)
      FOR i% = 1 TO DIM(a%(),1) : a%(i%) = i% : NEXT
      m% = 0
      i% = 1
      j% = 0
      r% = 2*n%-1
      REPEAT
        i% -= 1
        j% += 1
        p% = 0
        q% = -r%
        REPEAT
          i% += 1
          c%(p%) = 1
          c%(q%+r%) = 1
          SWAP a%(i%),a%(j%)
          p% = i% - a%(i%) + n%
          q% = i% + a%(i%) - 1
          b%(i%) = j%
          j% = i% + 1
        UNTIL j% > n% OR c%(p%) OR c%(q%+r%)
        IF c%(p%)=0 IF c%(q%+r%)=0 THEN
          IF m% = 0 THEN
            FOR p% = 1 TO n%
              MOVE 2*s%*(a%(p%)-1)+6, 2*s%*p%+6
              PRINT "♛";
            NEXT
          ENDIF
          m% += 1
        ENDIF
        j% = b%(i%)
        WHILE j% >= n% AND i% <> 0
          REPEAT
            SWAP a%(i%), a%(j%)
            j% = j%-1
          UNTIL j% < i%
          i% -= 1
          p% = i% - a%(i%) + n%
          q% = i% + a%(i%) - 1
          j% = b%(i%)
          c%(p%) = 0
          c%(q%+r%) = 0
        ENDWHILE
      UNTIL i% = 0
      = m%