N-queens problem: Difference between revisions

m
→‎Python: Backtracking on permutations: Restored the original look
m (→‎Python: Backtracking on permutations: Restored the original look)
(351 intermediate revisions by 87 users not shown)
Line 1:
{{taskTask}}
[[File:chess_queen.jpg|400px||right]]
 
[[File:N_queens_problem.png|400px||right]]
 
Solve the [[WP:Eight_queens_puzzle|eight queens puzzle]].
 
 
You can extend the problem to solve the puzzle with a board of side NxN.
You can extend the problem to solve the puzzle with a board of size &nbsp; <big>'''N'''x'''N'''</big>.
For the number of solutions for small values of &nbsp; '''N''', &nbsp; see &nbsp; [http://[oeis.org/:A000170|OEIS: oeis.orgA000170]].
 
 
;Cf.
;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]]
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">-V BoardSize = 8
 
F underAttack(col, queens)
I col C queens
R 1B
L(x) queens
I abs(col - x) == queens.len - L.index
R 1B
R 0B
 
F solve(n)
V result = [[Int]()]
[[Int]] newSolutions
L(row) 1 .. n
L(solution) result
L(i) 1 .. BoardSize
I !underAttack(i, solution)
newSolutions.append(solution [+] [i])
swap(&result, &newSolutions)
newSolutions.clear()
R result
 
print(‘Solutions for a chessboard of size ’String(BoardSize)‘x’String(BoardSize))
print()
 
L(answer) solve(BoardSize)
L(col) answer
V row = L.index
I row > 0
print(‘ ’, end' ‘’)
print(Char(code' ‘a’.code + row)‘’col, end' ‘’)
print(end' I L.index % 4 == 3 {"\n"} E ‘ ’)</syntaxhighlight>
 
{{out}}
<pre>
Solutions for a chessboard of size 8x8
 
a1 b5 c8 d6 e3 f7 g2 h4 a1 b6 c8 d3 e7 f4 g2 h5 a1 b7 c4 d6 e8 f2 g5 h3 a1 b7 c5 d8 e2 f4 g6 h3
a2 b4 c6 d8 e3 f1 g7 h5 a2 b5 c7 d1 e3 f8 g6 h4 a2 b5 c7 d4 e1 f8 g6 h3 a2 b6 c1 d7 e4 f8 g3 h5
a2 b6 c8 d3 e1 f4 g7 h5 a2 b7 c3 d6 e8 f5 g1 h4 a2 b7 c5 d8 e1 f4 g6 h3 a2 b8 c6 d1 e3 f5 g7 h4
a3 b1 c7 d5 e8 f2 g4 h6 a3 b5 c2 d8 e1 f7 g4 h6 a3 b5 c2 d8 e6 f4 g7 h1 a3 b5 c7 d1 e4 f2 g8 h6
a3 b5 c8 d4 e1 f7 g2 h6 a3 b6 c2 d5 e8 f1 g7 h4 a3 b6 c2 d7 e1 f4 g8 h5 a3 b6 c2 d7 e5 f1 g8 h4
a3 b6 c4 d1 e8 f5 g7 h2 a3 b6 c4 d2 e8 f5 g7 h1 a3 b6 c8 d1 e4 f7 g5 h2 a3 b6 c8 d1 e5 f7 g2 h4
a3 b6 c8 d2 e4 f1 g7 h5 a3 b7 c2 d8 e5 f1 g4 h6 a3 b7 c2 d8 e6 f4 g1 h5 a3 b8 c4 d7 e1 f6 g2 h5
a4 b1 c5 d8 e2 f7 g3 h6 a4 b1 c5 d8 e6 f3 g7 h2 a4 b2 c5 d8 e6 f1 g3 h7 a4 b2 c7 d3 e6 f8 g1 h5
a4 b2 c7 d3 e6 f8 g5 h1 a4 b2 c7 d5 e1 f8 g6 h3 a4 b2 c8 d5 e7 f1 g3 h6 a4 b2 c8 d6 e1 f3 g5 h7
a4 b6 c1 d5 e2 f8 g3 h7 a4 b6 c8 d2 e7 f1 g3 h5 a4 b6 c8 d3 e1 f7 g5 h2 a4 b7 c1 d8 e5 f2 g6 h3
a4 b7 c3 d8 e2 f5 g1 h6 a4 b7 c5 d2 e6 f1 g3 h8 a4 b7 c5 d3 e1 f6 g8 h2 a4 b8 c1 d3 e6 f2 g7 h5
a4 b8 c1 d5 e7 f2 g6 h3 a4 b8 c5 d3 e1 f7 g2 h6 a5 b1 c4 d6 e8 f2 g7 h3 a5 b1 c8 d4 e2 f7 g3 h6
a5 b1 c8 d6 e3 f7 g2 h4 a5 b2 c4 d6 e8 f3 g1 h7 a5 b2 c4 d7 e3 f8 g6 h1 a5 b2 c6 d1 e7 f4 g8 h3
a5 b2 c8 d1 e4 f7 g3 h6 a5 b3 c1 d6 e8 f2 g4 h7 a5 b3 c1 d7 e2 f8 g6 h4 a5 b3 c8 d4 e7 f1 g6 h2
a5 b7 c1 d3 e8 f6 g4 h2 a5 b7 c1 d4 e2 f8 g6 h3 a5 b7 c2 d4 e8 f1 g3 h6 a5 b7 c2 d6 e3 f1 g4 h8
a5 b7 c2 d6 e3 f1 g8 h4 a5 b7 c4 d1 e3 f8 g6 h2 a5 b8 c4 d1 e3 f6 g2 h7 a5 b8 c4 d1 e7 f2 g6 h3
a6 b1 c5 d2 e8 f3 g7 h4 a6 b2 c7 d1 e3 f5 g8 h4 a6 b2 c7 d1 e4 f8 g5 h3 a6 b3 c1 d7 e5 f8 g2 h4
a6 b3 c1 d8 e4 f2 g7 h5 a6 b3 c1 d8 e5 f2 g4 h7 a6 b3 c5 d7 e1 f4 g2 h8 a6 b3 c5 d8 e1 f4 g2 h7
a6 b3 c7 d2 e4 f8 g1 h5 a6 b3 c7 d2 e8 f5 g1 h4 a6 b3 c7 d4 e1 f8 g2 h5 a6 b4 c1 d5 e8 f2 g7 h3
a6 b4 c2 d8 e5 f7 g1 h3 a6 b4 c7 d1 e3 f5 g2 h8 a6 b4 c7 d1 e8 f2 g5 h3 a6 b8 c2 d4 e1 f7 g5 h3
a7 b1 c3 d8 e6 f4 g2 h5 a7 b2 c4 d1 e8 f5 g3 h6 a7 b2 c6 d3 e1 f4 g8 h5 a7 b3 c1 d6 e8 f5 g2 h4
a7 b3 c8 d2 e5 f1 g6 h4 a7 b4 c2 d5 e8 f1 g3 h6 a7 b4 c2 d8 e6 f1 g3 h5 a7 b5 c3 d1 e6 f8 g2 h4
a8 b2 c4 d1 e7 f5 g3 h6 a8 b2 c5 d3 e1 f7 g4 h6 a8 b3 c1 d6 e2 f5 g7 h4 a8 b4 c1 d3 e6 f2 g7 h5
</pre>
 
=={{header|360 Assembly}}==
{{trans|FORTRAN}}
Translated from the Fortran 77 solution.<br>
For maximum compatibility, this program uses only the basic instruction set (S/360).
<lang 360asm>* N-QUEENS PROBLEM 04/09/2015
<syntaxhighlight lang="360asm">* PRINTN-QUEENS NOGENPROBLEM 04/09/2015
MACRO
&LAB XDECO &REG,&TARGET
&LAB B I&SYSNDX branch around work area
*--------------------------------------------------------
P&SYSNDX DS 0D,PL8 packed
* CHANGE THE VALUE IN THE L VARIABLE FOR N
W&SYSNDX DS CL13 char
* THIS CODE WORKS PROPERLY WITH RECENT HLASM ASSEMBLERS
I&SYSNDX CVD &REG,P&SYSNDX convert to decimal
* IT ALSO ASSEMBLES WITH THE MVS 3.8J XF ASSEMBLER BUT
MVC W&SYSNDX,=X'40202020202020202020212060' nice mask
* IT WILL CC 04 FOR ALIGNMENT PROBLEMS
EDMK W&SYSNDX,P&SYSNDX+2 edit and mark
*--------------------------------------------------------
AIFBCTR (T'&REGR1,0 EQ 'O').NOREG locate the right place
AIFMVC 0(T'1,R1),W&TARGETSYSNDX+12 EQ 'O').NODEST move the sign
MVC &TARGET.(12),W&SYSNDX move to target
&LAB B I&SYSNDX BRANCH AROUND WORK AREA
SYSNDX DS XL8 CONVERSION WORK AREA
I&SYSNDX CVD &REG,W&SYSNDX CONVERT TO DECIMAL
MVC &TARGET,=XL12'402120202020202020202020'
ED &TARGET,W&SYSNDX+2 MAKE FIELD PRINTABLE
BC 2,*+12 BYPASS NEGATIVE
MVI &TARGET+12,C'-' INSERT NEGATIVE SIGN
B *+8 BYPASS POSITIVE
MVI &TARGET+12,C'+' INSERT POSITIVE SIGN
MEXIT
.NOREG MNOTE 8,'INPUT REGISTER OMITTED'
MEXIT
.NODEST MNOTE 8,'TARGET FIELD OMITTED'
MEXIT
MEND
*
PRINT NOGEN
NQUEENS CSECT
SAVE (14,12) save registers on SAVE REGISTERS ON ENTRYentry
BASRBALR R12,0 establish SET UP MYaddressability
USING *,R12 set base BASE REGISTERregister
ST R13,SAVEA+4 link ENSURE SAVE AREAmySA->prevSA
LA R13 R11,SAVEA CHAIN BUILT CORRECTLY. mySA
OPENEM OPEN (OUTDCB ST R11,OUTPUT8(R13) OPEN THE PRINTERlink FILEprevSA->mySA
NQUEENT LA R9,1 LR R13,R11 N=1 STARTset OFmySA LOOPpointer
LA R7,LL l
LA R6,1 i=1
LOOPI LR R1,R6 do i=1 to l
SLA R1,1 i*2
STH R6,A-2(R1) a(i)=i
LA R6,1(R6) i=i+1
BCT R7,LOOPI loop do i
OPENEM OPEN (OUTDCB,OUTPUT) open the printer file
LA R9,1 n=1 start of loop
LOOPN CH R9,L do n=1 to l
BH ELOOPN if n>l then exit loop
Line 140 ⟶ 213:
STH R0,U-2(R1) u(q+r)=0
B E60 goto e60
ZERO XDECO R9,PG+0 edit N FROM REG TO PRINT
XDECO R8,PG+12 edit M FROM REG TO PRINT
PUT OUTDCB,PG print buffer/REPLCD XPRNT
LA R9,1(R9) n=n+1
B LOOPN loop do n
ELOOPN CLOSE (OUTDCB) close CLOSE OUTPUToutput
L R13,SAVEA+4 PREVIOUSprevious SAVEsave AREAarea ADDRSaddrs
RETURN (14,12),RC=0 RETURNreturn TOto CALLERcaller WITH RCwith rc=0
LTORG
SAVEA DS 18F SAVEsave AREAarea FORfor CHAININGchaining
OUTDCB DCB DSORG=PS,MACRF=PM,DDNAME=OUTDD USEuse OUTDD INin JCLjcl
LLL EQU DC 14 H'13' input valuell<=16
AL DC H'01',H'02',H'03',H'04',H'05',H'06'AL2(LL) input value
A DS DC H'07',H'08',H'09',H'10',H'11',(LL)H'12'
US DCDS 46H'0'(LL)H
S DS 12H
Z DS H
Y DS H
PG DS CL24 buffer
U DC REGS (4*LL-2)H'0' MAKE SURE TO INCLD COPYBOOK JCL stack
REGS make sure to include copybook jcl
END NQUEENS</lang>
END NQUEENS</syntaxhighlight>
{{out}}
<pre>
Line 175 ⟶ 248:
11 2680
12 14200
13 47600
14 365596
</pre>
 
=={{header|6502 Assembly}}==
{{trans|Java}}
A few optimization techniques are used in this implementation. One goal was to get 8-queens to run in under 2 seconds on a 1 MHz computer.
 
Zero page values are stored where frequent use of the immediate addressing mode can be used as a speed up. This can be seen where a byte is referenced as variablename+1. INC and DEC instructions are used instead of ADC and SBC instructions for the comparison offsets.
 
The solution count is a 64-bit little endian value stored in memory starting at $0020, or $0D20 if the [[#Zero Page stub|Zero Page stub]] routine is used.
 
<syntaxhighlight lang="6502asm">n equ 8 ; queens
maximum equ 32 ; only limited by time
place equ $00
count equ maximum+place ; 64 bits (8 bytes)
length equ maximum+8
org $80
start
LDY #n ; n queens on an n x n board
STY greater+1
DEY
STY safe+1
LDX #length
LDA #$00
clear
STA place,X
DEX
BPL clear
next
INX
LDA #$FF
STA place,X
loop
INC place,X
LDA place,X
greater
CMP #n
BCS max
STX index+1
index
LDY #$00 ; index+1
BEQ safe
DEY
STA compare+1
STA add+1 ; compare
STA sub+1 ; compare
issafe
LDA place,Y
compare
CMP #$00 ; compare+1
BEQ loop ; unsafe
INC add+1
add
CMP #$00 ; add+1
BEQ loop ; unsafe
DEC sub+1
sub
CMP #$00 ; sub+1
BEQ loop ; unsafe
DEY
BPL issafe
safe
CPX #n-1
BNE next
INC count ; 64 bits (8 bytes)
BNE loop
INC count+1
BNE loop
INC count+2
BNE loop
INC count+3
BNE loop
INC count+4
BNE loop
INC count+5
BNE loop
INC count+6
BNE loop
INC count+7
BNE loop
BRK
max
DEX
BPL loop
; RTS</syntaxhighlight>
The code was assembled using Merlin32. The code length is 104 bytes not including the final 6 cycle RTS instruction.
<pre> n solutions cycles
1 1 443
2 0 710
3 0 1440
4 2 4359
5 10 17134
6 4 75848
7 40 337161
8 92 1616054
9 352 8044019
10 724 41556729
11 2680 230829955
12 14200 1378660940
13 73712 8684130248
14 365596 58185218171
15 2279184 412358679630
</pre>
==== Zero Page stub ====
The 6502 N-queens problem code resides within the zero page starting at $80 which can make running the program a bit tricky on some platforms. A stub is provided to facilitate running the zero page code. The stub routine turns off interrupts and swaps the zero page memory with an area residing at $D00 to $DFF, runs the zero page code, and swaps memory again. The cycle counts listed above do not include the time to run this stub. With the final RTS instruction included, the 105 byte N-queens zero page code must be in memory starting at $D80.
<syntaxhighlight lang="6502asm"> org $C00
PHP
SEI
JSR swap
JSR $0080
JSR swap
PLP
jmp end
swap
LDX #$00
loop
LDY $D00,X
LDA $00,X
STY $00,X
STA $D00,X
INX
BNE loop
RTS
end
; RTS</syntaxhighlight>
=={{header|ABAP}}==
<syntaxhighlight lang="abap">
<lang ABAP>
TYPES: BEGIN OF gty_matrix,
1 TYPE c,
Line 464 ⟶ 661:
SKIP 1.
ENDFORM. " SHOW_MATRIX
</syntaxhighlight>
</lang>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Queens is
Line 516 ⟶ 713:
end loop;
Put_Line (" A B C D E F G H");
end Queens;</langsyntaxhighlight>
{{out}}
<pre>
Line 535 ⟶ 732:
This one only counts solutions, though it's easy to do something else with each one (instead of the <code>M := M + 1;</code> line).
 
<langsyntaxhighlight lang="ada">with Ada.Text_IO;
use Ada.Text_IO;
 
Line 583 ⟶ 780:
Put_Line (Long_Integer'Image (Queens (N)));
end loop;
end CountQueens;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 593 ⟶ 790:
 
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8.8d.fc9.i386]}}
<langsyntaxhighlight Algol68lang="algol68">INT ofs = 1, # Algol68 normally uses array offset of 1 #
dim = 8; # dim X dim chess board #
[ofs:dim+ofs-1]INT b;
Line 643 ⟶ 840:
FI
OD
)</langsyntaxhighlight>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
More or less copied from the "DFS" lesson on tryapl.org .
<syntaxhighlight lang="apl">
⍝Solution
accm←{⍺,((⍴⍵)=⍴⊃⍺)↑⊂⍵}
atk←{∪∊(⊂⍵)+¯1 0 1×⊂⌽⍳⍴⍵}
dfs←{⊃∇⍨/⌽(⊂⍺ ⍺⍺ ⍵),⍺ ⍵⍵ ⍵}
qfmt←{⍵∘.=⍳⍴⍵}
subs←{(⊂⍵),¨(⍳⍴⊃⍺)~atk ⍵}
queens←{qfmt¨(↓0 ⍵⍴0)accm dfs subs ⍬}
printqueens←{i←1⋄{⎕←'answer'i⋄⎕←⍵⋄i+←1}¨queens ⍵}
 
⍝Example
printqueens 6
</syntaxhighlight>
{{out}}
<pre>
answer 1
0 1 0 0 0 0
0 0 0 1 0 0
0 0 0 0 0 1
1 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 1 0
answer 2
0 0 1 0 0 0
0 0 0 0 0 1
0 1 0 0 0 0
0 0 0 0 1 0
1 0 0 0 0 0
0 0 0 1 0 0
answer 3
0 0 0 1 0 0
1 0 0 0 0 0
0 0 0 0 1 0
0 1 0 0 0 0
0 0 0 0 0 1
0 0 1 0 0 0
answer 4
0 0 0 0 1 0
0 0 1 0 0 0
1 0 0 0 0 0
0 0 0 0 0 1
0 0 0 1 0 0
0 1 0 0 0 0
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">-- Finds all possible solutions and the unique patterns.
 
property Grid_Size : 8
 
property Patterns : {}
property Solutions : {}
property Test_Count : 0
 
property Rotated : {}
 
on run
local diff
local endTime
local msg
local rows
local startTime
set Patterns to {}
set Solutions to {}
set Rotated to {}
set Test_Count to 0
set rows to Make_Empty_List(Grid_Size)
set startTime to current date
Solve(1, rows)
set endTime to current date
set diff to endTime - startTime
set msg to ("Found " & (count Solutions) & " solutions with " & (count Patterns) & " patterns in " & diff & " seconds.") as text
display alert msg
return Solutions
end run
 
on Solve(row as integer, rows as list)
if row is greater than (count rows) then
Append_Solution(rows)
return
end if
repeat with column from 1 to Grid_Size
set Test_Count to Test_Count + 1
if Place_Queen(column, row, rows) then
Solve(row + 1, rows)
end if
end repeat
end Solve
 
on abs(n)
if n < 0 then
-n
else
n
end if
end abs
 
on Place_Queen(column as integer, row as integer, rows as list)
local colDiff
local previousRow
local rowDiff
local testColumn
repeat with previousRow from 1 to (row - 1)
set testColumn to item previousRow of rows
if testColumn is equal to column then
return false
end if
set colDiff to abs(testColumn - column) as integer
set rowDiff to row - previousRow
if colDiff is equal to rowDiff then
return false
end if
end repeat
set item row of rows to column
return true
end Place_Queen
 
on Append_Solution(rows as list)
local column
local rowsCopy
local testReflection
local testReflectionText
local testRotation
local testRotationText
local testRotations
copy rows to rowsCopy
set end of Solutions to rowsCopy
local rowsCopy
copy rows to testRotation
set testRotations to {}
repeat 3 times
set testRotation to Rotate(testRotation)
set testRotationText to testRotation as text
if Rotated contains testRotationText then
return
end if
set end of testRotations to testRotationText
set testReflection to Reflect(testRotation)
set testReflectionText to testReflection as text
if Rotated contains testReflectionText then
return
end if
set end of testRotations to testReflectionText
end repeat
repeat with testRotationText in testRotations
set end of Rotated to (contents of testRotationText)
end repeat
set end of Rotated to (rowsCopy as text)
set end of Rotated to (Reflect(rowsCopy) as text)
set end of Patterns to rowsCopy
end Append_Solution
 
on Make_Empty_List(depth as integer)
local i
local emptyList
set emptyList to {}
repeat with i from 1 to depth
set end of emptyList to missing value
end repeat
return emptyList
end Make_Empty_List
 
on Rotate(rows as list)
local column
local newColumn
local newRow
local newRows
local row
local rowCount
set rowCount to (count rows)
set newRows to Make_Empty_List(rowCount)
repeat with row from 1 to rowCount
set column to (contents of item row of rows)
set newRow to column
set newColumn to rowCount - row + 1
set item newRow of newRows to newColumn
end repeat
return newRows
end Rotate
 
on Reflect(rows as list)
local column
local newRows
set newRows to {}
repeat with column in rows
set end of newRows to (count rows) - column + 1
end repeat
return newRows
end Reflect</syntaxhighlight>
 
=={{header|Applesoft BASIC}}==
{{trans|Java}}
<syntaxhighlight lang="basic"> 1 READ N,T,M,R(0): FOR Y = 0 TO M STEP 0: FOR L = 0 TO T STEP 0:R(Y) = R(Y) + T:X = R(Y):C = NOT Y: IF NOT C THEN FOR I = T TO Y:A = R(Y - I): IF NOT (A = X OR A = X - I OR A = X + I) THEN NEXT I:C = T
2 L = R(Y) > N OR C: NEXT L:D = - (R(Y) > N): IF NOT D AND Y < N THEN R(Y + T) = M:D = D + T
3 S = S + NOT D:Y = Y + D: NEXT Y: PRINT "THERE " MID$ ("AREIS",4 ^ (S = 1),3)" "S" SOLUTION" MID$ ("S",1,S < > 1)" FOR "N + T" X "N + T: DATA7,1,-1,-1</syntaxhighlight>
{{out}}
<pre>THERE ARE 92 SOLUTIONS FOR 8 X 8
</pre>
 
=={{header|Arc}}==
This program prints out all possible solutions:
<syntaxhighlight lang="lisp">(def nqueens (n (o queens))
(if (< len.queens n)
(let row (if queens (+ 1 queens.0.0) 0)
(each col (range 0 (- n 1))
(let new-queens (cons (list row col) queens)
(if (no conflicts.new-queens)
(nqueens n new-queens)))))
(prn queens)))
 
; check if the first queen in 'queens' lies on the same column or diagonal as
; any of the others
(def conflicts (queens)
(let (curr . rest) queens
(or (let curr-column curr.1
(some curr-column (map [_ 1] rest))) ; columns
(some [diagonal-match curr _] rest))))
 
(def diagonal-match (curr other)
(is (abs (- curr.0 other.0))
(abs (- curr.1 other.1))))</syntaxhighlight>
{{out}}
The output is one solution per line, each solution in the form `((row col) (row col) (row col) ...)`:
<pre>arc> (nqueens 4)
((3 2) (2 0) (1 3) (0 1))
((3 1) (2 3) (1 0) (0 2))</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">result: new []
 
queens: function [n, i, a, b, c][
if? i < n [
loop 1..n 'j [
if all? @[
not? contains? a j
not? contains? b i+j
not? contains? c i-j
] ->
queens n, i+1, a ++ @[j], b ++ @[i+j], c ++ @[i-j]
]
]
else [
if n = size a ->
'result ++ @[a]
]
]
 
BoardSize: 6
 
queens BoardSize, 0, [], [], []
loop result 'solution [
loop solution 'col [
line: new repeat "-" BoardSize
line\[col-1]: `Q`
print line
]
print ""
]</syntaxhighlight>
 
{{out}}
 
<pre>-Q----
---Q--
-----Q
Q-----
--Q---
----Q-
 
--Q---
-----Q
-Q----
----Q-
Q-----
---Q--
 
---Q--
Q-----
----Q-
-Q----
-----Q
--Q---
 
----Q-
--Q---
Q-----
-----Q
---Q--
-Q----</pre>
 
=={{header|AWK}}==
Inspired by Raymond Hettinger's Python solution, but builds the vector incrementally.
<syntaxhighlight lang="awk">
#!/usr/bin/gawk -f
# Solve the Eight Queens Puzzle
# Inspired by Raymond Hettinger [https://code.activestate.com/recipes/576647/]
# Just the vector of row positions per column is kept,
# and filled with all possibilities from left to right recursively,
# then checked against the columns left from the current one:
# - is a queen in the same row
# - is a queen in the digonal
# - is a queen in the reverse diagonal
BEGIN {
dim = ARGC < 2 ? 8 : ARGV[1]
# make vec an array
vec[1] = 0
# scan for a solution
if (tryqueen(1, vec, dim))
result(vec, dim)
else
print "No solution with " dim " queens."
}
# try if a queen can be set in column (col)
function tryqueen(col, vec, dim, new) {
for (new = 1; new <= dim; ++new) {
# check all previous columns
if (noconflict(new, col, vec, dim)) {
vec[col] = new
if (col == dim)
return 1
# must try next column(s)
if (tryqueen(col+1, vec, dim))
return 1
}
}
# all tested, failed
return 0
}
 
# check if setting the queen (new) in column (col) is ok
# by checking the previous colums conflicts
function noconflict(new, col, vec, dim, j) {
for (j = 1; j < col; j++) {
if (vec[j] == new)
return 0 # same row
if (vec[j] == new - col + j)
return 0 # diagonal conflict
if (vec[j] == new + col - j)
return 0 # reverse diagonal conflict
}
# no test failed, no conflict
return 1
}
 
# print matrix
function result(vec, dim, row, col, sep, lne) {
# print the solution vector
for (row = 1; row <= dim; ++row)
printf " %d", vec[row]
print
# print a board matrix
for (row = 1; row <= dim; ++row) {
lne = "|"
for (col = 1; col <= dim; ++col) {
if (row == vec[col])
lne = lne "Q|"
else
lne = lne "_|"
}
print lne
}
}
 
 
</syntaxhighlight>
{{out}}
<pre>
1 5 8 6 3 7 2 4
|Q|_|_|_|_|_|_|_|
|_|_|_|_|_|_|Q|_|
|_|_|_|_|Q|_|_|_|
|_|_|_|_|_|_|_|Q|
|_|Q|_|_|_|_|_|_|
|_|_|_|Q|_|_|_|_|
|_|_|_|_|_|Q|_|_|
|_|_|Q|_|_|_|_|_|
</pre>
 
=={{header|ATS}}==
<syntaxhighlight lang="ats">
<lang ATS>
(* ****** ****** *)
//
Line 727 ⟶ 1,330:
 
(* end of [queens.dats] *)
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
=== Output to formatted Message box ===
{{trans|C}}
<syntaxhighlight lang="autohotkey">;
<lang AutoHotkey>;
; Post: http://www.autohotkey.com/forum/viewtopic.php?p=353059#353059
; Timestamp: 05/may/2010
Line 811 ⟶ 1,414:
Output .= "|`n" , yyy++
}
}</langsyntaxhighlight>
=== Includes a solution browser GUI ===
This implementation supports N = 4..12 queens, and will find ALL solutions
Line 817 ⟶ 1,420:
The screenshot shows the first solution of 10 possible solutions for N = 5 queens.
 
<langsyntaxhighlight AutoHotkeylang="autohotkey">N := 5
Number: ; main entrance for different # of queens
SI := 1
Line 906 ⟶ 1,509:
 
GuiClose:
ExitApp</langsyntaxhighlight>
[[image:N-Queens_SolutionBrowserGUI.png]]
 
Line 915 ⟶ 1,518:
[[Image:queens9_bbc.gif|right]]
[[Image:queens10_bbc.gif|right]]
<langsyntaxhighlight lang="bbcbasic"> Size% = 8
Cell% = 32
VDU 23,22,Size%*Cell%;Size%*Cell%;Cell%,Cell%,16,128+8,5
Line 975 ⟶ 1,578:
ENDWHILE
UNTIL i% = 0
= m%</langsyntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight BCPLlang="bcpl">// This can be run using Cintcode BCPL freely available from www.cl.cam.ac.uk/users/mr10.
 
GET "libhdr.h"
Line 1,008 ⟶ 1,611:
RESULTIS 0
}
</syntaxhighlight>
</lang>
The following is a re-implementation of the algorithm given above but
using the MC package that allows machine independent runtime generation
Line 1,014 ⟶ 1,617:
It runs about 25 times faster that the version given above.
 
<syntaxhighlight lang="bcpl">
<lang BCPL>
GET "libhdr.h"
GET "mc.h"
Line 1,149 ⟶ 1,752:
i, n, all)
}
</syntaxhighlight>
</lang>
 
=={{header|Befunge}}==
 
This algorithm works with any board size from 4 upwards.
 
<syntaxhighlight lang="befunge"><+--XX@_v#!:-1,+55,g\1$_:00g2%-0vv:,+55<&,,,,,,"Size: "
"| Q"$$$>:01p:2%!00g0>>^<<!:-1\<1>00p::2%-:40p2/50p2*1+
!77**48*+31p\:1\g,::2\g:,\3\g,,^g>0g++40g%40g\-\40g\`*-
2g05\**!!%6g04-g052!:`\g05::-1/2<^4*2%g05\+*+1*!!%6g04-</syntaxhighlight>
 
{{out}}
 
<pre>Size: 8
 
+---+---+---+---+---+---+---+---+
| | | | | Q | | | |
+---+---+---+---+---+---+---+---+
| | | Q | | | | | |
+---+---+---+---+---+---+---+---+
| Q | | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | | Q | |
+---+---+---+---+---+---+---+---+
| | Q | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | | | Q |
+---+---+---+---+---+---+---+---+
| | | | | | Q | | |
+---+---+---+---+---+---+---+---+
| | | | Q | | | | |
+---+---+---+---+---+---+---+---+</pre>
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( printBoard
= board M L x y S R row line
. :?board
Line 1,212 ⟶ 1,846:
| out$(found !solutions solutions)
)
);</langsyntaxhighlight>
{{out}} (tail):
<pre>
Line 1,257 ⟶ 1,891:
 
=={{header|C}}==
C99, compiled with <code>gcc -std=c99 -Wall</code>. Take one commandline argument: size of board, or default to 8. Shows the board layout for each solution.<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 1,287 ⟶ 1,921:
int hist[n];
solve(n, 0, hist);
}</langsyntaxhighlight>
 
Similiar to above, but using bits to save board configurations and quite a bit faster:<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
Line 1,326 ⟶ 1,960:
printf("\nSolutions: %d\n", count);
return 0;
}</langsyntaxhighlight>
Take that and unwrap the recursion, plus some heavy optimizations, and we have a very fast and very unreadable solution:
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 1,418 ⟶ 2,052:
printf("\nSolutions: %d\n", count);
return 0;
}</langsyntaxhighlight>
 
A slightly cleaned up version of the code above where some optimizations were redundant. This version is also further optimized, and runs about 15% faster than the one above on modern compilers:
 
<langsyntaxhighlight lang="c">#include <stdio.h>
#define MAXN 31
 
Line 1,491 ⟶ 2,125:
printf("Number of solution for %d is %d\n",n,nqueens(n));
}
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
=== Roger Hui (1981) Algorithm ===
From Hui, Roger, The N Queens Problem, APL Quote-Quad, Volume 11, Number 3, 1981-03:-
 
"In a solution, each possible row (column) index must appear exactly once: an index occurring more than once means that two queens are on the same row (column); and the absence of an index means that some other index must occur more than once. Hence, we can specify an arrangement as a permutation of ⍳n , which are the column indices, with the row indices understood to be ⍳n . With this, the number of possibilities is reduced from n!n×n to !n . It remains to eliminate arrangements having two queens on the same diagonal.
 
If two queens occupy the same diagonal, the line connecting them has slope 1 or ¯1 . Conversely, if the line connecting two queens has slope 1 or ¯1 , the two queens share a diagonal. Therefore, we seek to eliminate all permutations specifying a pair of queens where
((change in y) ÷ (change in x)) ∊ 1 ¯1 , or (|change in y) = (|change in x)"
{{trans|J}}
{{works with|C sharp|C#|7}}
<!-- By Martin Freedman, 13/02/2018 -->
<syntaxhighlight lang="csharp">using System.Collections.Generic;
using static System.Linq.Enumerable;
using static System.Console;
using static System.Math;
 
namespace N_Queens
{
static class Program
{
static void Main(string[] args)
{
var n = 8;
var cols = Range(0, n);
var combs = cols.Combinations(2).Select(pairs=> pairs.ToArray());
var solved = from v in cols.Permutations().Select(p => p.ToArray())
where combs.All(c => Abs(v[c[0]] - v[c[1]]) != Abs(c[0] - c[1]))
select v;
WriteLine($"{n}-queens has {solved.Count()} solutions");
WriteLine("Position is row, value is column:-");
var first = string.Join(" ", solved.First());
WriteLine($"First Solution: {first}");
Read();
}
 
//Helpers
public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> values)
{
if (values.Count() == 1)
return values.ToSingleton();
 
return values.SelectMany(v => Permutations(values.Except(v.ToSingleton())), (v, p) => p.Prepend(v));
}
 
public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> seq) =>
seq.Aggregate(Empty<T>().ToSingleton(), (a, b) => a.Concat(a.Select(x => x.Append(b))));
 
public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> seq, int numItems) =>
seq.Combinations().Where(s => s.Count() == numItems);
 
public static IEnumerable<T> ToSingleton<T>(this T item) { yield return item; }
}
}</syntaxhighlight>
Output
<pre>8-queens has 92 solutions
Position is row, value is column:-
First Solution: 0 4 7 5 2 6 1 3
</pre>
===Hettinger Algorithm===
Compare this to the Hettinger solution used in the first Python answer. The logic is similar but the diagonal calculation is different and more expensive computationally (Both suffer from being unable to eliminate permutation prefixes that are invalid e.g. 0 1 ...)
<syntaxhighlight lang="csharp">
using System.Collections.Generic;
using static System.Linq.Enumerable;
using static System.Console;
using static System.Math;
 
namespace N_Queens
{
static class Program
{
static void Main(string[] args)
{
var n = 8;
var cols = Range(0, n);
var solved = from v in cols.Permutations().Select(p => p.ToArray())
where n == (from i in cols select v[i]+i).Distinct().Count()
where n == (from i in cols select v[i]-i).Distinct().Count()
select v;
 
WriteLine($"{n}-queens has {solved.Count()} solutions");
WriteLine("Position is row, value is column:-");
var first = string.Join(" ", solved.First());
WriteLine($"First Solution: {first}");
Read();
}
 
//Helpers from https://gist.github.com/martinfreedman/139dd0ec7df4737651482241e48b062f
 
public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> values)
{
if (values.Count() == 1)
return values.ToSingleton();
 
return values.SelectMany(v => Permutations(values.Except(v.ToSingleton())), (v, p) => p.Prepend(v));
}
 
public static IEnumerable<T> ToSingleton<T>(this T item) { yield return item; }
}
}</syntaxhighlight>
=== Amb solution===
This uses the second version of the [https://rosettacode.org/wiki/Amb#C.23 Amb C# class] in the Amb challenge. Really that is not McCarthy's Amb (Ambiguous function) and here it is used just as a simple general interface by lambdas to a standalone backtrack algorithm. Due to the specification of the Amb challenge, this, ironically (given the notion of ambiguous functions), only produces one solution not 92. It is trivial to update Amb (might be better called a backtracker rather than Amb too) but here it is just used to show how easy it is to go from a generate and prune Linq solution to a backtrack solution. The Linq filters becoming "amb" requirements.
{{works with|C sharp|C#|7.1}}
<!-- By Martin Freedman, 9/02/2018 -->
<syntaxhighlight lang="csharp">using static System.Linq.Enumerable;
using static System.Console;
 
namespace N_Queens
{
static class Program
{
static void Main(string[] args)
{
var n = 8;
var domain = Range(0, n).ToArray();
 
var amb = new Amb.Amb();
var queens = domain.Select(_ => amb.Choose(domain)).ToArray();
amb.Require(() => n == queens.Select(q=> q.Value).Distinct().Count());
amb.Require(() => n == domain.Select(i=> i + queens[i].Value).Distinct().Count());
amb.Require(() => n == domain.Select(i=> i - queens[i].Value).Distinct().Count());
 
if (amb.Disambiguate())
{
WriteLine("Position is row, value is column:-");
WriteLine(string.Join(" ", queens.AsEnumerable()));
}
else
WriteLine("amb is angry");
Read();
}
}
}</syntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">// Much shorter than the version below;
// uses C++11 threads to parallelize the computation; also uses backtracking
// Outputs all solutions for any table size
Line 1,597 ⟶ 2,365:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}Output for N = 4:
<pre> a b c d
Line 1,611 ⟶ 2,379:
3 #
4 # </pre>
<langsyntaxhighlight lang="cpp">
// A straight-forward brute-force C++ version with formatted output,
// eschewing obfuscation and C-isms, producing ALL solutions, which
Line 1,869 ⟶ 2,637:
std::cout << queens( N ) << "\n";
}
</syntaxhighlight>
</lang>
{{out}} for N=4:
<pre>
Line 1,890 ⟶ 2,658:
=== Alternate version ===
Windows-only
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <iostream>
Line 1,991 ⟶ 2,759:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,020 ⟶ 2,788:
 
Version using Heuristics - explained here: [http://en.wikipedia.org/wiki/8_queens_puzzle#Solution_construction Solution_construction]
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <iostream>
Line 2,109 ⟶ 2,877:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NQueens
{
class Program
{
const int N = 8;
static bool Allowed(bool[,] board, int x, int y)
{
for (int i=0; i<=x; i++)
{
if (board[i,y] || (i <= y && board[x-i,y-i]) || (y+i < N && board[x-i,y+i]))
{
return false;
}
}
return true;
}
static bool FindSolution(bool[,] board, int x)
{
for (int y = 0; y < N; y++)
{
if (Allowed(board, x, y))
{
board[x, y] = true;
if (x == N-1 || FindSolution(board, x + 1))
{
return true;
}
board[x, y] = false;
}
}
return false;
}
static void Main(string[] args)
{
bool[,] board = new bool[N, N];
if (FindSolution(board, 0))
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
Console.Write(board[i, j] ? "|Q" : "| ");
}
Console.WriteLine("|");
}
}
else
{
Console.WriteLine("No solution found for n = " + N + ".");
}
Console.ReadKey(true);
}
}
}</lang>
 
=={{header|Clojure}}==
This produces all solutions by essentially a backtracking algorithm. The heart is the ''extends?'' function, which takes a partial solution for the first ''k<size'' columns and sees if the solution can be extended by adding a queen at row ''n'' of column ''k+1''. The ''extend'' function takes a list of all partial solutions for ''k'' columns and produces a list of all partial solutions for ''k+1'' columns. The final list ''solutions'' is calculated by starting with the list of 0-column solutions (obviously this is the list ''[ [] ]'', and iterates ''extend'' for ''size'' times.
<langsyntaxhighlight lang="clojure">(def size 8)
 
(defn extends? [v n]
Line 2,201 ⟶ 2,903:
(println s))
 
(println (count solutions) "solutions")</langsyntaxhighlight>
===Short Version===
<syntaxhighlight lang="clojure">(ns queens
(:require [clojure.math.combinatorics :as combo]
 
(defn queens [n]
(filter (fn [x] (every? #(apply distinct? (map-indexed % x)) [+ -]))
(combo/permutations (range 1 (inc n))))) </syntaxhighlight>
===Backtracking as Tree processing===
Each state of the board can be represented as a sequence of the row coordinate for a queen, the column being the index in the sequence (coordinates starting at 0). Each state can have 'children' states if it is legal (no conflict) and has less than n queens. A child state is the result of adding a new queen on the next column, there are as many children states as rows as we are trying all of them. A depth first traversal of this virtual tree of states gives us the solutions when we filter out the illegal states and the incomplete states. The sequence of states is lazy so we could read only one result and not have to compute the other states.
 
<syntaxhighlight lang="clojure">
(defn n-queens [n]
(let[children #(map (partial conj %) (range n))
no-conflict? (fn [x] (or (empty? x)
(every? #(apply distinct? (map-indexed % x))
[+ - (fn[_ v] v)])))]
(filter (every-pred no-conflict? #(= n (count %)))
(tree-seq (every-pred #(> n (count %))
no-conflict?)
children []))))
</syntaxhighlight>
 
=={{header|CLU}}==
{{trans|C}}
<syntaxhighlight lang="clu">n_queens = cluster is solve
rep = null
own hist: array[int] := array[int]$[]
own solutions: array[string] := array[string]$[]
attack = proc (i,j,col: int) returns (bool)
return(hist[j]=i | int$abs(hist[j]-i)=col-j)
end attack
cur_solution = proc ()
n: int := array[int]$size(hist)
ss: stream := stream$create_output()
for i: int in int$from_to(0,n-1) do
for j: int in int$from_to(0,n-1) do
if j=hist[i] then stream$putc(ss, 'Q')
elseif (i+j)//2 = 1 then stream$putc(ss, ' ')
else stream$putc(ss, '.')
end
end
stream$putc(ss, '\n')
end
array[string]$addh(solutions, stream$get_contents(ss))
end cur_solution
solve_rec = proc (col: int)
n: int := array[int]$size(hist)
if col=n then cur_solution() return end
for i: int in int$from_to(0,n-1) do
j: int := 0
while j<col cand ~attack(i,j,col) do j := j+1 end
if j<col then continue end
hist[col] := i
solve_rec(col+1)
end
end solve_rec
solve = proc (n: int) returns (sequence[string])
hist := array[int]$fill(0,n,0)
solutions := array[string]$[]
solve_rec(0)
return(sequence[string]$a2s(solutions))
end solve
end n_queens
 
start_up = proc()
N = 8
po: stream := stream$primary_output()
solutions: sequence[string] := n_queens$solve(N)
 
count: int := 0
for s: string in sequence[string]$elements(solutions) do
count := count + 1
stream$putl(po, "No. " || int$unparse(count) || "\n-------\n" || s)
end
end start_up</syntaxhighlight>
{{out}}
<pre style='height:50ex'>No. 1
-------
Q . . .
. .Q. .
. . . .Q
. . Q .
. Q . .
. . .Q.
.Q. . .
. Q . .
 
No. 2
-------
Q . . .
. . Q .
. . . .Q
.Q. . .
. . . Q
. Q . .
.Q. . .
. .Q. .
 
No. 3
-------
Q . . .
. . .Q.
. .Q. .
. . Q .
. . . .Q
Q . . .
. . Q .
.Q. . .
 
No. 4
-------
Q . . .
. . .Q.
. . Q .
. . . Q
.Q. . .
. Q . .
. . .Q.
.Q. . .
 
No. 5
-------
.Q. . .
. Q . .
. . .Q.
. . . Q
. Q . .
Q. . . .
. . . Q
. .Q. .
 
No. 6
-------
.Q. . .
. .Q. .
. . . Q
Q. . . .
. Q . .
. . . Q
. . .Q.
. Q . .
 
No. 7
-------
.Q. . .
. .Q. .
. . . Q
. Q . .
Q . . .
. . . Q
. . .Q.
.Q. . .
 
No. 8
-------
.Q. . .
. . Q .
Q . . .
. . .Q.
. .Q. .
. . . Q
. Q . .
. .Q. .
 
No. 9
-------
.Q. . .
. . Q .
. . . .Q
.Q. . .
Q . . .
. Q . .
. . . Q
. .Q. .
 
No. 10
-------
.Q. . .
. . .Q.
. Q . .
. . Q .
. . . .Q
. .Q. .
Q . . .
. Q . .
 
No. 11
-------
.Q. . .
. . .Q.
. . Q .
. . . Q
Q . . .
. Q . .
. . .Q.
.Q. . .
 
No. 12
-------
.Q. . .
. . . Q
. . .Q.
Q. . . .
. Q . .
. .Q. .
. . . Q
. Q . .
 
No. 13
-------
. Q . .
Q. . . .
. . . Q
. .Q. .
. . . .Q
Q . . .
. .Q. .
. . Q .
 
No. 14
-------
. Q . .
. .Q. .
.Q. . .
. . . Q
Q . . .
. . .Q.
. .Q. .
. . Q .
 
No. 15
-------
. Q . .
. .Q. .
.Q. . .
. . . Q
. . .Q.
. Q . .
. . . Q
Q. . . .
 
No. 16
-------
. Q . .
. .Q. .
. . . Q
Q. . . .
. .Q. .
Q . . .
. . . .Q
. . Q .
 
No. 17
-------
. Q . .
. .Q. .
. . . .Q
. Q . .
Q . . .
. . .Q.
.Q. . .
. . Q .
 
No. 18
-------
. Q . .
. . Q .
.Q. . .
. .Q. .
. . . .Q
Q. . . .
. . . Q
. Q . .
 
No. 19
-------
. Q . .
. . Q .
.Q. . .
. . .Q.
Q . . .
. Q . .
. . . .Q
. .Q. .
 
No. 20
-------
. Q . .
. . Q .
.Q. . .
. . .Q.
. . Q .
Q. . . .
. . . .Q
. Q . .
 
No. 21
-------
. Q . .
. . Q .
. .Q. .
Q. . . .
. . . .Q
. .Q. .
. . . Q
Q . . .
 
No. 22
-------
. Q . .
. . Q .
. .Q. .
Q . . .
. . . .Q
. .Q. .
. . . Q
Q. . . .
 
No. 23
-------
. Q . .
. . Q .
. . . .Q
Q. . . .
. .Q. .
. . .Q.
. . Q .
Q . . .
 
No. 24
-------
. Q . .
. . Q .
. . . .Q
Q. . . .
. . Q .
. . .Q.
.Q. . .
. Q . .
 
No. 25
-------
. Q . .
. . Q .
. . . .Q
Q . . .
. .Q. .
Q. . . .
. . . Q
. .Q. .
 
No. 26
-------
. Q . .
. . .Q.
.Q. . .
. . . Q
. . Q .
Q. . . .
. .Q. .
. . Q .
 
No. 27
-------
. Q . .
. . .Q.
.Q. . .
. . . Q
. . .Q.
. Q . .
Q . . .
. .Q. .
 
No. 28
-------
. Q . .
. . . Q
. .Q. .
. . .Q.
Q . . .
. . Q .
.Q. . .
. .Q. .
 
No. 29
-------
. .Q. .
Q. . . .
. . Q .
. . . Q
.Q. . .
. . .Q.
. Q . .
. . Q .
 
No. 30
-------
. .Q. .
Q. . . .
. . Q .
. . . Q
. . .Q.
.Q. . .
. . . Q
Q . . .
 
No. 31
-------
. .Q. .
Q . . .
. . Q .
. . . Q
. . .Q.
Q. . . .
. Q . .
. . .Q.
 
No. 32
-------
. .Q. .
Q . . .
. . . Q
.Q. . .
. . .Q.
. . . Q
Q . . .
. .Q. .
 
No. 33
-------
. .Q. .
Q . . .
. . . Q
.Q. . .
. . .Q.
. . . Q
. . Q .
Q. . . .
 
No. 34
-------
. .Q. .
Q . . .
. . . Q
. .Q. .
Q . . .
. . . Q
. . .Q.
.Q. . .
 
No. 35
-------
. .Q. .
Q . . .
. . . .Q
. .Q. .
. . . Q
Q. . . .
. Q . .
. . Q .
 
No. 36
-------
. .Q. .
Q . . .
. . . .Q
. . Q .
Q . . .
.Q. . .
. . Q .
. . .Q.
 
No. 37
-------
. .Q. .
. . Q .
Q . . .
. .Q. .
.Q. . .
. . . Q
. Q . .
. . .Q.
 
No. 38
-------
. .Q. .
. . Q .
. . . .Q
Q . . .
. . . Q
Q. . . .
. Q . .
. .Q. .
 
No. 39
-------
. .Q. .
. . Q .
. . . .Q
.Q. . .
Q . . .
. . .Q.
. . Q .
Q . . .
 
No. 40
-------
. .Q. .
. . .Q.
Q . . .
. . . Q
. . Q .
Q . . .
. . .Q.
.Q. . .
 
No. 41
-------
. .Q. .
. . .Q.
. Q . .
. . . Q
.Q. . .
. .Q. .
Q . . .
. . Q .
 
No. 42
-------
. .Q. .
. . .Q.
. . Q .
Q . . .
. . .Q.
Q. . . .
. Q . .
. . . Q
 
No. 43
-------
. .Q. .
. . .Q.
. . Q .
.Q. . .
Q . . .
. . Q .
. . . .Q
Q . . .
 
No. 44
-------
. .Q. .
. . . Q
Q . . .
.Q. . .
. . .Q.
Q . . .
. . . Q
. .Q. .
 
No. 45
-------
. .Q. .
. . . Q
Q . . .
. .Q. .
. . . Q
Q . . .
. . .Q.
.Q. . .
 
No. 46
-------
. .Q. .
. . . Q
. . Q .
.Q. . .
Q . . .
. . .Q.
.Q. . .
. . Q .
 
No. 47
-------
. . Q .
Q. . . .
. .Q. .
. . Q .
. . . .Q
Q . . .
. . . Q
.Q. . .
 
No. 48
-------
. . Q .
Q. . . .
. . . .Q
. Q . .
.Q. . .
. . .Q.
. Q . .
. . Q .
 
No. 49
-------
. . Q .
Q. . . .
. . . .Q
. . Q .
. Q . .
. . .Q.
.Q. . .
. Q . .
 
No. 50
-------
. . Q .
Q . . .
. .Q. .
. . Q .
. . . .Q
.Q. . .
Q . . .
. . .Q.
 
No. 51
-------
. . Q .
Q . . .
. .Q. .
. . .Q.
. Q . .
. . . Q
. . .Q.
Q. . . .
 
No. 52
-------
. . Q .
Q . . .
. . .Q.
Q. . . .
. . . Q
. Q . .
. . . .Q
.Q. . .
 
No. 53
-------
. . Q .
Q . . .
. . . .Q
Q. . . .
. .Q. .
. . .Q.
. Q . .
. . Q .
 
No. 54
-------
. . Q .
.Q. . .
Q . . .
. . Q .
. . . .Q
Q . . .
. .Q. .
. . .Q.
 
No. 55
-------
. . Q .
.Q. . .
Q . . .
. . .Q.
.Q. . .
. . . Q
. . .Q.
. Q . .
 
No. 56
-------
. . Q .
.Q. . .
. . . .Q
. Q . .
. . . Q
Q. . . .
. . .Q.
Q . . .
 
No. 57
-------
. . Q .
. . .Q.
Q . . .
.Q. . .
. . . .Q
. . Q .
. .Q. .
Q . . .
 
No. 58
-------
. . Q .
. . .Q.
Q . . .
. Q . .
.Q. . .
. . . Q
. . .Q.
.Q. . .
 
No. 59
-------
. . Q .
. . .Q.
.Q. . .
. Q . .
. . . .Q
Q. . . .
. Q . .
. . Q .
 
No. 60
-------
. . Q .
. . .Q.
.Q. . .
. . Q .
. Q . .
Q. . . .
. .Q. .
. . . Q
 
No. 61
-------
. . Q .
. . .Q.
.Q. . .
. . Q .
. Q . .
Q. . . .
. . . .Q
. Q . .
 
No. 62
-------
. . Q .
. . .Q.
. .Q. .
Q. . . .
. Q . .
. . . Q
. . .Q.
Q . . .
 
No. 63
-------
. . Q .
. . . Q
. .Q. .
Q. . . .
. Q . .
. . Q .
.Q. . .
. . .Q.
 
No. 64
-------
. . Q .
. . . Q
. .Q. .
Q. . . .
. . . Q
Q . . .
. . .Q.
.Q. . .
 
No. 65
-------
. . .Q.
Q. . . .
. . Q .
Q . . .
. . . .Q
.Q. . .
. . . Q
. Q . .
 
No. 66
-------
. . .Q.
Q . . .
. . . Q
Q. . . .
. Q . .
. .Q. .
. . . .Q
. Q . .
 
No. 67
-------
. . .Q.
Q . . .
. . . Q
Q. . . .
. .Q. .
. . . Q
. . Q .
.Q. . .
 
No. 68
-------
. . .Q.
.Q. . .
Q . . .
. . .Q.
. . Q .
. . . Q
.Q. . .
. Q . .
 
No. 69
-------
. . .Q.
.Q. . .
Q . . .
. . . Q
. .Q. .
Q . . .
. . . Q
. .Q. .
 
No. 70
-------
. . .Q.
.Q. . .
Q . . .
. . . Q
. . Q .
Q . . .
. .Q. .
. . .Q.
 
No. 71
-------
. . .Q.
.Q. . .
. . Q .
. . .Q.
Q . . .
. Q . .
.Q. . .
. . . Q
 
No. 72
-------
. . .Q.
.Q. . .
. . Q .
. . . Q
Q . . .
. Q . .
.Q. . .
. . .Q.
 
No. 73
-------
. . .Q.
.Q. . .
. . . Q
Q . . .
. .Q. .
. . . Q
Q . . .
. .Q. .
 
No. 74
-------
. . .Q.
.Q. . .
. . . Q
Q . . .
. . . .Q
. .Q. .
Q . . .
. Q . .
 
No. 75
-------
. . .Q.
.Q. . .
. . . Q
. Q . .
Q . . .
. . . Q
.Q. . .
. .Q. .
 
No. 76
-------
. . .Q.
. Q . .
Q . . .
. .Q. .
. . . .Q
Q . . .
. . . Q
.Q. . .
 
No. 77
-------
. . .Q.
. Q . .
.Q. . .
. . . Q
. . Q .
. . .Q.
Q . . .
.Q. . .
 
No. 78
-------
. . .Q.
. Q . .
. . . Q
Q. . . .
. Q . .
. .Q. .
.Q. . .
. . . Q
 
No. 79
-------
. . .Q.
. Q . .
. . . Q
Q. . . .
. . . .Q
Q . . .
. . Q .
.Q. . .
 
No. 80
-------
. . .Q.
. . . Q
.Q. . .
. Q . .
Q . . .
. . .Q.
. . Q .
.Q. . .
 
No. 81
-------
. . . Q
Q. . . .
. Q . .
. . . Q
. . .Q.
. Q . .
.Q. . .
. .Q. .
 
No. 82
-------
. . . Q
Q . . .
. .Q. .
Q. . . .
. . . .Q
. .Q. .
. Q . .
. . Q .
 
No. 83
-------
. . . Q
Q . . .
. . .Q.
.Q. . .
Q . . .
. Q . .
. . . .Q
. .Q. .
 
No. 84
-------
. . . Q
.Q. . .
Q . . .
. . Q .
. . . .Q
. .Q. .
.Q. . .
. Q . .
 
No. 85
-------
. . . Q
.Q. . .
. . . .Q
Q . . .
. . Q .
Q. . . .
. . .Q.
. Q . .
 
No. 86
-------
. . . Q
. Q . .
.Q. . .
. .Q. .
. . . .Q
Q. . . .
. Q . .
. . Q .
 
No. 87
-------
. . . Q
. Q . .
.Q. . .
. . . Q
. . .Q.
Q. . . .
. Q . .
. .Q. .
 
No. 88
-------
. . . Q
. .Q. .
. Q . .
Q. . . .
. . .Q.
. . . Q
.Q. . .
. Q . .
 
No. 89
-------
. . . .Q
Q . . .
. .Q. .
Q. . . .
. . . Q
. .Q. .
. Q . .
. . Q .
 
No. 90
-------
. . . .Q
Q . . .
. . Q .
.Q. . .
Q . . .
. . .Q.
. .Q. .
. . Q .
 
No. 91
-------
. . . .Q
.Q. . .
Q . . .
. . Q .
.Q. . .
. .Q. .
. . . Q
. Q . .
 
No. 92
-------
. . . .Q
. Q . .
Q . . .
.Q. . .
. . .Q.
Q . . .
. . . Q
. .Q. .</pre>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
# Unlike traditional N-Queens solutions that use recursion, this
# program attempts to more closely model the "human" algorithm.
Line 2,316 ⟶ 4,113:
 
nqueens(8)
</syntaxhighlight>
</lang>
 
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun queens (n &optional (m n))
(if (zerop n)
(list nil)
Line 2,338 ⟶ 4,134:
 
(defun print-queens (n)
(mapc #'print-solution (queens n)))</langsyntaxhighlight>
 
=== Alternate solution ===
Translation of Fortran 77
<langsyntaxhighlight lang="lisp">(defun queens1 (n)
(let ((a (make-array n))
(s (make-array n))
Line 2,381 ⟶ 4,177:
> (loop for n from 1 to 14 collect (cons n (queens1 n)))
((1 . 1) (2 . 0) (3 . 0) (4 . 2) (5 . 10) (6 . 4) (7 . 40) (8 . 92) (9 . 352)
(10 . 724) (11 . 2680) (12 . 14200) (13 . 73712) (14 . 365596))</langsyntaxhighlight>
 
As in Fortran, the iterative function above is equivalent to the recursive function below:
 
<langsyntaxhighlight lang="lisp">(defun queens2 (n)
(let ((a (make-array n))
(u (make-array (+ n n -1) :initial-element t))
Line 2,405 ⟶ 4,201:
(rotatef (aref a i) (aref a k))))))))
(sub 0))
m))</langsyntaxhighlight>
 
=={{header|Curry}}==
Three different ways of attacking the same problem. All copied from [http://web.cecs.pdx.edu/~antoy/flp/patterns/ A Catalog of Design Patterns in FLP]
<langsyntaxhighlight lang="curry">
-- 8-queens implementation with the Constrained Constructor pattern
-- Sergio Antoy
Line 2,468 ⟶ 4,264:
 
main = extend []
</syntaxhighlight>
</lang>
 
Another approach from the same source.
 
<langsyntaxhighlight lang="curry">
-- N-queens puzzle implemented with "Distinct Choices" pattern
-- Sergio Antoy
Line 2,507 ⟶ 4,303:
store = free
-- end
</syntaxhighlight>
</lang>
 
Yet another approach, also from the same source.
 
<langsyntaxhighlight lang="curry">
-- 8-queens implementation with both the Constrained Constructor
-- and the Fused Generate and Test patterns.
Line 2,571 ⟶ 4,367:
 
main = extend []
</syntaxhighlight>
</lang>
Mainly [http://www-ps.informatik.uni-kiel.de/~pakcs/webpakcs/main.cgi?queens webpakcs], uses constraint-solver.
<langsyntaxhighlight lang="curry">import CLPFD
import Findall
 
Line 2,591 ⟶ 4,387:
 
-- oneSolution = unpack $ queens 8
-- allSolutions = findall $ queens 8</langsyntaxhighlight>
 
=={{header|D}}==
===Short Version===
This high-level version uses the second solution of the Permutations task.
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm, std.range, permutations2;
 
Line 2,604 ⟶ 4,400:
n.iota.map!(i => p[i] - i).array.sort().uniq.count == n)
.count.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>92</pre>
Line 2,611 ⟶ 4,407:
This version shows all the solutions.
{{trans|C}}
<langsyntaxhighlight lang="d">enum side = 8;
__gshared int[side] board;
 
Line 2,654 ⟶ 4,450:
y--;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,691 ⟶ 4,487:
===Fast Version===
{{trans|C}}
<langsyntaxhighlight lang="d">ulong nQueens(in uint nn) pure nothrow @nogc @safe
in {
assert(nn > 0 && nn <= 27,
Line 2,780 ⟶ 4,576:
immutable uint side = (args.length >= 2) ? args[1].to!uint : 8;
writefln("N-queens(%d) = %d solutions.", side, side.nQueens);
}</langsyntaxhighlight>
{{out}}
<pre>N-queens(8) = 92 solutions.</pre>
Line 2,789 ⟶ 4,585:
 
=={{header|Dart}}==
<langsyntaxhighlight lang="dart">/**
Return true if queen placement q[n] does not conflict with
other queens q[0] through q[n-1]
Line 2,853 ⟶ 4,649:
void main() {
enumerate(4);
}</langsyntaxhighlight>
{{out}}
<pre>* Q * *
Line 2,865 ⟶ 4,661:
* Q * *
</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program N_queens_problem;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils;
 
var
i: Integer;
q: boolean;
a: array[0..8] of boolean;
b: array[0..16] of boolean;
c: array[0..14] of boolean;
x: array[0..8] of Integer;
 
procedure TryMove(i: Integer);
begin
var j := 1;
while True do
begin
q := false;
if a[j] and b[i + j] and c[i - j + 7] then
begin
x[i] := j;
a[j] := false;
b[i + j] := false;
c[i - j + 7] := false;
 
if i < 8 then
begin
TryMove(i + 1);
if not q then
begin
a[j] := true;
b[i + j] := true;
c[i - j + 7] := true;
end;
end
else
q := true;
end;
if q or (j = 8) then
Break;
inc(j);
end;
end;
 
begin
for i := 1 to 8 do
a[i] := true;
 
for i := 2 to 16 do
b[i] := true;
 
for i := 0 to 14 do
c[i] := true;
 
TryMove(1);
 
if q then
for i := 1 to 8 do
writeln(i, ' ', x[i]);
readln;
end.</syntaxhighlight>
 
=={{header|Draco}}==
{{trans|C}}
<syntaxhighlight lang="draco">byte SIZE = 8;
word count;
 
proc solve([*] int hist; int col) void:
int i, j, n;
n := dim(hist, 1);
if col = n then
count := count + 1;
writeln();
writeln("No. ", count);
writeln("-----");
for i from 0 upto n-1 do
for j from 0 upto n-1 do
write(if j=hist[i] then 'Q'
elif (i+j)&1 /= 0 then ' '
else '.' fi)
od;
writeln()
od
else
for i from 0 upto n-1 do
j := 0;
while j<col and not (hist[j]=i or |(hist[j]-i) = col-j) do
j := j + 1
od;
if j >= col then
hist[col] := i;
solve(hist, col+1)
fi
od
fi
corp
 
proc nonrec main() void:
[SIZE] int hist;
count := 0;
solve(hist, 0)
corp</syntaxhighlight>
{{out}}
<pre>No. 1
-----
Q . . .
. .Q. .
. . . .Q
. . Q .
. Q . .
. . .Q.
.Q. . .
. Q . .</pre>
...
<pre>No. 92
-----
. . . .Q
. Q . .
Q . . .
.Q. . .
. . .Q.
Q . . .
. . . Q
. .Q. .</pre>
 
=={{header|EasyLang}}==
 
<syntaxhighlight lang="easylang">
subr show_sol
print "Solution " & n_sol
print ""
for i = 1 to n
write " "
for j = 1 to n
if j = x[i]
write "Q "
else
write ". "
.
.
print ""
.
print ""
.
subr test
ok = 1
for i = 1 to y - 1
if x[y] = x[i] or abs (x[i] - x[y]) = abs (y - i)
ok = 0
.
.
.
n = 8
len x[] n
y = 1
x[1] = 1
while y >= 1
test
if ok = 1 and y + 1 <= n
y += 1
x[y] = 1
else
if ok = 1
n_sol += 1
if n_sol <= 1
show_sol
.
.
while y >= 1 and x[y] = n
y -= 1
.
if y >= 1
x[y] += 1
.
.
.
print n_sol & " solutions"
</syntaxhighlight>
{{out}}
<pre>Solution 1
 
Q . . . . . . .
. . . . Q . . .
. . . . . . . Q
. . . . . Q . .
. . Q . . . . .
. . . . . . Q .
. Q . . . . . .
. . . Q . . . .
 
92 solutions</pre>
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
;; square num is i + j*N
(define-syntax-rule (sq i j) (+ i (* j N)))
 
;; compute diag number for each square
(define (do-diag1 i0 j0 dnum into: dnum1 N) ;; ++i and ++j diags
(for [(i (in-range i0 N)) (j (in-range j0 N))]
;;(writeln i j 'diag1 dnum)
(vector-set! dnum1 (sq i j) dnum)))
(define (do-diag2 i0 j0 dnum into: dnum2 N) ;; --i and ++j diags
(for [(i (in-range i0 -1 -1)) (j (in-range j0 N))]
;; (writeln i j 'diag2 dnum)
(vector-set! dnum2 (sq i j) dnum)))
(define (init-diags dnum1 dnum2 N)
(define dnum 0)
(for ((j N)) (do-diag1 0 j dnum dnum1 N) (++ dnum))
(for ((i (in-range 1 N)))
(do-diag1 i 0 dnum dnum1 N) (++ dnum))
(set! dnum 0)
(for ((j N)) (do-diag2 (1- N) j dnum dnum2 N) (++ dnum))
(for ((i (1- N))) (do-diag2 i 0 dnum dnum2 N) (++ dnum)))
;; end boring diags part
(define (q-search i N col diag1 diag2 dnum1 dnum2 &hits (ns))
(cond
[(= i N) (set-box! &hits (1+ (unbox &hits))) ] ;; (writeln 'HIT col)
[else
(for ((j N))
(set! ns (sq i j))
#:continue (or [col j] [diag1 [dnum1 ns]] [diag2 [dnum2 ns]])
(vector-set! col j i) ;; move
(vector-set! diag1 [dnum1 ns] #t) ;; flag busy diagonal
(vector-set! diag2 [dnum2 ns] #t)
(q-search (1+ i) N col diag1 diag2 dnum1 dnum2 &hits)
(vector-set! col j #f) ;; unmove
(vector-set! diag1 [dnum1 ns] #f)
(vector-set! diag2 [dnum2 ns] #f))
]))
(define (q-count (N 8))
(define dnum1 (make-vector (* N N)))
(define dnum2 (make-vector (* N N )))
(init-diags dnum1 dnum2 N)
(define diag1 (make-vector (* 2 N) #f)) ; busy diag's
(define diag2 (make-vector (* 2 N) #f))
(define col (make-vector N #f))
(define &hits (box 0))
(q-search 0 N col diag1 diag2 dnum1 dnum2 &hits)
(unbox &hits))
(define (task up-to-n)
(for ((i up-to-n)) (writeln i ' ♕ (q-count i) 'solutions)))
</syntaxhighlight>
{{out}}
<pre>
(task 13)
 
0 ♕ 1 solutions
1 ♕ 1 solutions
2 ♕ 0 solutions
3 ♕ 0 solutions
4 ♕ 2 solutions
5 ♕ 10 solutions
6 ♕ 4 solutions
7 ♕ 40 solutions
8 ♕ 92 solutions
9 ♕ 352 solutions
10 ♕ 724 solutions
11 ♕ 2680 solutions
12 ♕ 14200 solutions
</pre>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="Ecstasy">/**
* A solver for the classic 8-queens problem.
*
* @see https://rosettacode.org/wiki/N-queens_problem
*/
module eightQueens {
void run() {
@Inject Console console;
Int count = new Board().solve(b -> console.print($"{b}\n"));
console.print($"{count} solutions found");
}
 
/**
* `Board` represents a chess board that holds only queens. The board
* is organized as columns 0 ("A") to 7 ("H"), and rows 0 (rank "1")
* to 7 (rank "8").
*/
const Board {
/**
* Construct an empty board.
*/
construct() {}
 
/**
* Internal: Construct a specifically-populated board.
*/
private construct(Int queens, Int claimed) {
this.queens = queens;
this.claimed = claimed;
}
 
/**
* Each bit of this 64-bit integer represents a queen.
*/
private Int queens;
/**
* Each bit of this 64-bit integer represents a queen or a threat.
*/
private Int claimed;
 
/**
* Translate a column and row to a bit-mask, used with the
* [queens] and [claimed] properties. Examples:
* * A1 is (0,0) => 0x0000000000000001
* * H8 is (7,7) => 0x8000000000000000
*/
private Int mask(Int col, Int row) = 1 << (row << 3) + col;
 
/**
* Determine if the specified square has a queen in it.
*/
Boolean occupied(Int col, Int row) {
return queens & mask(col, row) != 0;
}
 
/**
* Determine if the specified square is safe from the queens.
*/
Boolean safe(Int col, Int row) {
return claimed & mask(col, row) == 0;
}
 
/**
* Attempt to place a queen in a specified square.
*
* @return True iff a queen can be safely placed in the square
* @return (conditional) the new Board with the new queen on it
*/
conditional Board placeQueen(Int col, Int row) {
assert 0 <= col < 8 && 0 <= row < 8;
if (!safe(col, row)) {
return False;
}
 
Int newQueens = queens | mask(col, row);
Int newClaimed = claimed | queens;
// claim all threatened spaces
for (Int i : 0..7) {
newClaimed |= mask(i, row) | mask(col, i);
val diagDownRow = row + i - col;
if (0 <= diagDownRow < 8) {
newClaimed |= mask(i, diagDownRow);
}
val diagUpRow = row - i + col;
if (0 <= diagUpRow < 8) {
newClaimed |= mask(i, diagUpRow);
}
}
return True, new Board(newQueens, newClaimed);
}
 
/**
* Attempt to find all solutions to the n-queens problem.
*/
Int solve(function void(Board) yield) = solve(yield, 0);
 
/**
* Internal: Attempt to find all solutions to the n-queens problem,
* starting with the specified column and recursively solving by
* moving to the next column for each potential solution found in
* the specified column.
*/
private Int solve(function void(Board) yield, Int col) {
if (col == 8) {
// there is no column 8; we've found a solution
yield(this);
return 1;
}
 
Int count = 0;
for (Int rank : 8..1) {
val row = 8-rank;
if (Board afterPlacing := placeQueen(col, row)) {
count += afterPlacing.solve(yield, col + 1);
}
}
return count;
}
 
@Override String toString() {
val buf = new StringBuffer();
for (Int rank : 8..1) {
buf.append($"{rank} |");
val row = 8-rank;
for (Int col : 0..7) {
buf.add(occupied(col, row) ? 'q' : '_').add('|');
}
buf.add('\n');
}
return buf.append(" A B C D E F G H").toString();
}
}
}</syntaxhighlight>
<b>Output:</b>
<code><pre>
8 |q|_|_|_|_|_|_|_|
7 |_|_|_|_|_|_|q|_|
6 |_|_|_|_|q|_|_|_|
5 |_|_|_|_|_|_|_|q|
4 |_|q|_|_|_|_|_|_|
3 |_|_|_|q|_|_|_|_|
2 |_|_|_|_|_|q|_|_|
1 |_|_|q|_|_|_|_|_|
A B C D E F G H
 
8 |q|_|_|_|_|_|_|_|
7 |_|_|_|_|_|_|q|_|
6 |_|_|_|q|_|_|_|_|
5 |_|_|_|_|_|q|_|_|
4 |_|_|_|_|_|_|_|q|
3 |_|q|_|_|_|_|_|_|
2 |_|_|_|_|q|_|_|_|
1 |_|_|q|_|_|_|_|_|
A B C D E F G H
 
(...)
 
8 |_|_|q|_|_|_|_|_|
7 |_|_|_|_|_|q|_|_|
6 |_|_|_|q|_|_|_|_|
5 |_|q|_|_|_|_|_|_|
4 |_|_|_|_|_|_|_|q|
3 |_|_|_|_|q|_|_|_|
2 |_|_|_|_|_|_|q|_|
1 |q|_|_|_|_|_|_|_|
A B C D E F G H
 
92 solutions found
</pre></code>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
QUEENS
Line 2,936 ⟶ 5,180:
end
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,972 ⟶ 5,216:
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule RC do
def queen(n, display \\ true) do
solve(n, [], [], [], display)
add = Tuple.duplicate(true, 2*n-1)
sub = Tuple.duplicate(true, 2*n-1)
solve(n, [], add, sub, display)
end
defp solve(n, row, _, _, display) when n<==length(row) do
if display, do: print(n,row)
1
end
defp solve(n, row, addadd_list, subsub_list, display) do
Enum.map(Enum.to_list(0..n-1) -- row, fn x ->
iaddadd = x + (len = length(row)) # \ diagonal check
isubsub = ifx - length(yrow) = x-len) < 0, do: y + 2*n - 1, else: y # / diagonal check
if elem(add, iaddin add_list) andor elem(sub, isubin sub_list) do
solve(n, [x|row], put_elem(add,iadd,false), put_elem(sub,isub,false), display)
else
0
else
solve(n, [x|row], [add | add_list], [sub | sub_list], display)
end
end) |> Enum.sum # total of the solution
end
Line 3,011 ⟶ 5,253:
Enum.each(7..12, fn n ->
IO.puts " #{n} Queen : #{RC.queen(n, false)}" # no display
end)</langsyntaxhighlight>
 
{{out}}
Line 3,144 ⟶ 5,386:
11 Queen : 2680
12 Queen : 14200
</pre>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(let ((*result* '()))
(defun grid-cnt (n)
(* n n) )
(defun x-axis (n pos)
(/ pos n) )
(defun y-axis (n pos)
(% pos n) )
(defun chess-cnt (chess-map)
(seq-count (lambda (x) x) chess-map))
(defun check-conflict (n chess-map pos)
(let ((is-conflict nil))
(cl-loop for i from 0 to (1- (grid-cnt n)) while (not is-conflict) do
(when (aref chess-map i)
(when (or (= (x-axis n i) (x-axis n pos))
(= (y-axis n i) (y-axis n pos))
(= (abs (- (x-axis n i) (x-axis n pos)))
(abs (- (y-axis n i) (y-axis n pos))))
)
(setq is-conflict 't)
)
)
)
is-conflict )
)
(defun place-chess (n chess-map start-pos)
(if (< (chess-cnt chess-map) n)
(progn
(let ()
(cl-loop for i from start-pos to (1- (grid-cnt n)) do
(when (not (aref chess-map i)) ;; check if place is empty
;; check if place is on hold by other chess
(when (not (check-conflict n chess-map i))
(let ((map1 (copy-sequence chess-map)))
(aset map1 i 't)
(place-chess n map1 i)
)
)
)
)
)
)
(progn
(if *result* (nconc *result* (list chess-map)) (setq *result* (list chess-map)))
)
)
)
 
(defun show-result (n)
(let ()
(seq-map (lambda (map1)
(let ((map-txt ""))
(message ">>>>>>>>>>>>>>")
(seq-map-indexed (lambda (elm idx)
(if (= (% idx n) 0)
;;(setq map-text (concat map-txt "\n"))
(progn
(message map-txt)
(setq map-txt "") )
)
(setq map-txt
(concat map-txt (if elm "✓" "⓪")))
) map1)
(message "<<<<<<<<<<<<<<\n")
)
) *result*)
)
(message "%d solutions in total" (length *result*))
)
(defun start-calculate (n)
(let ((chess-map (make-vector (grid-cnt n) nil)))
(place-chess n chess-map 0)
)
(show-result n)
)
 
(start-calculate 8)
)
</syntaxhighlight>
 
{{out}}
<pre>
...
92 solutions in total
</pre>
 
=={{header|Erlang}}==
Instead of spawning a new process to search for each possible solution I backtrack.
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( n_queens ).
 
Line 3,232 ⟶ 5,564:
Board = solve( N ),
display( Board ).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,251 ⟶ 5,583:
. . Q . . . . .
</pre>
 
===Alternative Version===
<syntaxhighlight lang="erlang">
%%%For 8X8 chessboard with N queens.
-module(queens).
-export([queens/1]).
 
queens(0) -> [[]];
queens(N) ->
[[Row | Columns] || Columns <- queens(N-1),
Row <- [1,2,3,4,5,6,7,8] -- Columns,
safe(Row, Columns, 1)].
 
safe(_Row, [], _N) -> true;
safe(Row, [Column|Columns], N) ->
(Row /= Column + N) andalso (Row /= Column - N) andalso
safe(Row, Columns, (N+1)).
</syntaxhighlight>
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
!------------------------------------------------
! QUEENS.R : solve queens problem on a NxN board
Line 3,338 ⟶ 5,688:
END IF
END PROGRAM
</syntaxhighlight>
</lang>
Note: The program prints solutions one per line. This version works well for the PC and the C-64. For PC only you can omit the % integer-type specificator with a <code>!$INTEGER</code> pragma directive.
 
=={{header|F Sharp}}==
<syntaxhighlight lang="fsharp">
let rec iterate f value = seq {
yield value
yield! iterate f (f value) }
 
let up i = i + 1
let right i = i
let down i = i - 1
 
let noCollisionGivenDir solution number dir =
Seq.forall2 (<>) solution (Seq.skip 1 (iterate dir number))
 
let goodAddition solution number =
List.forall (noCollisionGivenDir solution number) [ up; right; down ]
 
let rec extendSolution n ps =
[0..n - 1]
|> List.filter (goodAddition ps)
|> List.map (fun num -> num :: ps)
 
let allSolutions n =
iterate (List.collect (extendSolution n)) [[]]
 
// Print one solution for the 8x8 case
let printOneSolution () =
allSolutions 8
|> Seq.item 8
|> Seq.head
|> List.iter (fun rowIndex ->
printf "|"
[0..8] |> List.iter (fun i -> printf (if i = rowIndex then "X|" else " |"))
printfn "")
 
// Print number of solution for the other cases
let printNumberOfSolutions () =
printfn "Size\tNr of solutions"
[1..11]
|> List.map ((fun i -> Seq.item i (allSolutions i)) >> List.length)
|> List.iteri (fun i cnt -> printfn "%d\t%d" (i+1) cnt)
 
printOneSolution()
 
printNumberOfSolutions()
</syntaxhighlight>
 
The output:
 
<pre>
| | | |X| | | | | |
| |X| | | | | | | |
| | | | | | |X| | |
| | |X| | | | | | |
| | | | | |X| | | |
| | | | | | | |X| |
| | | | |X| | | | |
|X| | | | | | | | |
 
Size Nr of solutions
1 1
2 0
3 0
4 2
5 10
6 4
7 40
8 92
9 352
10 724
11 2680
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.98}}
<lang factor>USING: kernel sequences math math.combinatorics formatting io locals ;
<syntaxhighlight lang="factor">USING: kernel sequences math math.combinatorics formatting io locals ;
IN: queens
 
Line 3,350 ⟶ 5,772:
:: safe? ( board q -- ? )
[let q board nth :> x
q <iota> [
x swap
[ board nth ] keep
Line 3,360 ⟶ 5,782:
 
: solution? ( board -- ? )
dup length <iota> [ dupd safe? ] all? nip ;
 
: queens ( n -- l )
<iota> all-permutations [ solution? ] filter ;
 
: .queens ( n -- )
Line 3,369 ⟶ 5,791:
[
[ 1 + "%d " printf ] each nl
] each ;</langsyntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">variable solutions
variable nodes
 
Line 3,400 ⟶ 5,822:
solutions @ . ." solutions, " nodes @ . ." nodes" ;
 
8 queens \ 92 solutions, 1965 nodes</langsyntaxhighlight>
 
=== Alternate solution adapted from FD-V02N1.pdf ===
<syntaxhighlight lang="forth">
\ http://www.forth.org/fd/FD-V02N1.pdf
VOCABULARY nqueens ALSO nqueens DEFINITIONS
 
8 constant queens
 
\ Nqueen solution from FD-V02N1.pdf
: 1array CREATE 0 DO 1 , LOOP DOES> SWAP CELLS + ;
queens 1array a \ a,b & c: workspaces for solutions
queens 2* 1array b
queens 2* 1array c
queens 1array x \ trial solutions
 
: safe ( c i -- n )
SWAP
2DUP - queens 1- + c @ >R
2DUP + b @ >R
DROP a @ R> R> * * ;
 
: mark ( c i -- )
SWAP
2DUP - queens 1- + c 0 swap !
2DUP + b 0 swap !
DROP a 0 swap ! ;
 
: unmark ( c i -- )
SWAP
2DUP - queens 1- + c 1 swap !
2DUP + b 1 swap !
DROP a 1 swap ! ;
 
VARIABLE tries
VARIABLE sols
 
: .cols queens 0 DO I x @ 1+ 5 .r loop ;
: .sol ." Found on try " tries @ 6 .R .cols cr ;
 
: try
queens 0
DO 1 tries +!
DUP I safe
IF DUP I mark
DUP I SWAP x !
DUP queens 1- < IF DUP 1+ RECURSE ELSE sols ++ .sol THEN
DUP I unmark
THEN
LOOP DROP ;
 
: go 0 tries ! CR 0 try CR sols @ . ." solutions Found, for n = " queens . ;
go
</syntaxhighlight>
 
=={{header|Fortran}}==
Line 3,406 ⟶ 5,881:
 
Using a back tracking method to find one solution
<langsyntaxhighlight lang="fortran">program Nqueens
implicit none
 
Line 3,506 ⟶ 5,981:
write(*, "(a)") line
end subroutine
end program</langsyntaxhighlight>
{{out}} for 8, 16 and 32 queens
<pre style="height:40ex;overflow:scroll">n = 8
Line 3,630 ⟶ 6,105:
 
===Alternate Fortran 77 solution===
<langsyntaxhighlight lang="fortran">C This one implements depth-first backtracking.
C See the 2nd program for Scheme on the "Permutations" page for the
C main idea.
Line 3,702 ⟶ 6,177:
C 17 95815104
C 18 666090624
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang="fortran">!The preceding program implements recursion using arrays, since Fortran 77 does not allow recursive
!functions. The same algorithm is much easier to follow in Fortran 90, using the RECURSIVE keyword.
!Like previously, the program only counts solutions. It's pretty straightforward to adapt it to print
Line 3,756 ⟶ 6,231:
print *, n, m
end do
end program</langsyntaxhighlight>
 
===Alternate Fortran 95 solution with OpenMP===
Line 3,765 ⟶ 6,240:
column three.
 
If using GCC, compile with ''gfortran -O2 -fopenmp queens.f90''. With Absoft Pro Fortran, ''af90 -O2 -openmp queens.f90'', and with Intel Fortran, ''ifort /fast /openmpQopenmp queens.f90''.
 
With some versions of GCC the function OMP_GET_WTIME is not known, which seems to be a bug. Then it's enough to comment out the two calls, and the program won't display timings.
 
<langsyntaxhighlight lang="fortran">program queens
use omp_lib
implicit none
integer, parameter :: long = selected_int_kind(17)
integer, parameter :: l = 18
integer, parameter :: nthreads = 16 ! Change to suit your processor
integer :: n, i, j, a(l*l, 2), k, p, q
integer(long) :: s, b(l*l)
real(kind(1d0)) :: t1, t2
! Edit : Added OPEN MP calls to set number of threads
 
CALL OMP_SET_DYNAMIC(.TRUE.)
CALL OMP_SET_NUM_THREADS(nthreads)
do n = 6, l
k = 0
Line 3,890 ⟶ 6,368:
go to 60
end function
end program</langsyntaxhighlight>
 
===Fortran 2008 in a Lisp-like fashion===
{{works with|Fortran|2008 and later}}
The following program solves, stores, and prints all solutions to the n-queens problem, for board sizes given on the command line. To compile it, you need my modules that employ Fortran 2008’s type polymorphism to support Lisp-like CONS-pairs. The modules (and this program) are available at [https://sourceforge.net/p/chemoelectric/fortran-modules https://sourceforge.net/p/chemoelectric/fortran-modules] along with a GNU makefile, all under a permissive free software license. The makefile is written for GNU Fortran; compiler version 11.2.1 works. The programming style is essentially functional programming, and solutions are stored as a linked list of linked lists. One might notice how circular lists are used within the code to overcome Fortran’s limited ability to do closures.
 
Part of the intent here is to show that Fortran can do quite a few things people would not think it could, if it is given adequate library support.
<syntaxhighlight lang="fortran">program example__n_queens
 
use, intrinsic :: iso_fortran_env, only: output_unit
 
use, non_intrinsic :: garbage_collector
use, non_intrinsic :: cons_pairs
 
implicit none
 
! .true. is good for testing that necessary values are rooted.
! .false. to collect garbage only when the heap reaches a limit.
logical :: aggressive_garbage_collection = .true.
 
integer :: arg_count
integer :: stat
character(80) :: arg
 
type(gcroot_t) :: board_sizes
 
arg_count = command_argument_count ()
if (arg_count < 1) then
call print_usage (output_unit)
else
board_sizes = nil
block
integer :: i
integer :: board_size
do i = 1, arg_count
call get_command_argument (i, arg)
read (arg, *, iostat = stat) board_size
if (stat /= 0 .or. board_size < 1) then
board_size = -1
end if
board_sizes = cons (board_size, board_sizes)
end do
board_sizes = reversex (board_sizes)
end block
 
if (is_member (int_eq, -1, board_sizes)) then
call print_usage (output_unit)
else
! Use pair_for_each as a way to distinguish the last
! BOARD_SIZE from the others. The last entry will be the final
! pair, and so its CDR will *not* be a pair.
call pair_for_each (find_and_print_all_solutions, &
& circular_list (output_unit), &
& board_sizes)
end if
end if
 
contains
 
subroutine print_usage (outp)
integer, intent(in) :: outp
 
write (outp, '("Usage: example__n_queens BOARD_SIZE [BOARD_SIZE...]")')
write (outp, '("Each BOARD_SIZE must be at least 1.")')
write (outp, '("For each BOARD_SIZE, all solutions are computed before any is printed.")')
end subroutine print_usage
 
subroutine find_and_print_all_solutions (outp_pair, board_sizes)
class(*), intent(in) :: outp_pair
class(*), intent(in) :: board_sizes
 
integer :: n_outp
type(gcroot_t) :: all_solutions
 
n_outp = int_cast (car (outp_pair))
 
all_solutions = find_all_solutions (car (board_sizes))
call check_garbage
call print_all_solutions (n_outp, car (board_sizes), all_solutions)
call check_garbage
if (is_pair (cdr (board_sizes))) then
! Space between one BOARD_SIZE and another.
write (n_outp, '()')
end if
end subroutine find_and_print_all_solutions
 
function find_all_solutions (board_size) result (all_solutions)
class(*), intent(in) :: board_size
type(cons_t) :: all_solutions
 
class(*), allocatable :: solutions
 
call find_solutions_from_ranks_so_far (board_size, nil, solutions)
all_solutions = solutions
end function find_all_solutions
 
recursive subroutine find_solutions_from_ranks_so_far (board_size, ranks_so_far, solutions)
class(*), intent(in) :: board_size
class(*), intent(in) :: ranks_so_far
class(*), allocatable, intent(out) :: solutions
 
type(cons_t) :: ranks
 
if (length (ranks_so_far) == int_cast (board_size)) then
solutions = list (ranks_so_far)
else
ranks = find_legal_ranks_for_file (int_cast (board_size), ranks_so_far)
solutions = concatenatex (map (find_solutions_from_ranks_so_far, &
& circular_list (board_size), &
& map (kons, ranks, circular_list (ranks_so_far))))
end if
end subroutine find_solutions_from_ranks_so_far
 
function find_legal_ranks_for_file (board_size, ranks_so_far) result (ranks)
!
! Return a list of all the ranks in the next file, under the
! constraint that a queen placed in the position not be under
! attack.
!
integer, intent(in) :: board_size
class(*), intent(in) :: ranks_so_far
type(cons_t) :: ranks
 
ranks = iota (board_size, 1) ! All the possible ranks.
ranks = remove_illegal_ranks (ranks, ranks_so_far)
end function find_legal_ranks_for_file
 
function remove_illegal_ranks (new_ranks, ranks_so_far) result (legal_ranks)
class(*), intent(in) :: new_ranks
class(*), intent(in) :: ranks_so_far
type(cons_t) :: legal_ranks
 
legal_ranks = filter_map (keep_legal_rank, new_ranks, &
& circular_list (ranks_so_far))
end function remove_illegal_ranks
 
subroutine keep_legal_rank (rank, ranks_so_far, retval)
class(*), intent(in) :: rank
class(*), intent(in) :: ranks_so_far
class(*), allocatable, intent(out) :: retval
 
if (rank_is_legal (rank, ranks_so_far)) then
retval = rank
else
retval = .false.
end if
end subroutine keep_legal_rank
 
function rank_is_legal (new_rank, ranks_so_far) result (bool)
class(*), intent(in) :: new_rank
class(*), intent(in) :: ranks_so_far
logical :: bool
 
integer :: new_file
type(cons_t) :: files_so_far
 
new_file = int (length (ranks_so_far)) + 1
files_so_far = iota (new_file - 1, new_file - 1, -1)
bool = every (these_two_queens_are_nonattacking, &
& circular_list (new_file), &
& circular_list (new_rank), &
& files_so_far, &
& ranks_so_far)
end function rank_is_legal
 
function these_two_queens_are_nonattacking (file1, rank1, file2, rank2) result (bool)
class(*), intent(in) :: file1, rank1
class(*), intent(in) :: file2, rank2
logical :: bool
 
integer :: f1, r1
integer :: f2, r2
 
! The rank and the two diagonals must not be the same. (The files
! are known to be different.)
 
f1 = int_cast (file1)
r1 = int_cast (rank1)
f2 = int_cast (file2)
r2 = int_cast (rank2)
 
bool = (r1 /= r2 .and. r1 + f1 /= r2 + f2 .and. r1 - f1 /= r2 - f2)
end function these_two_queens_are_nonattacking
 
subroutine print_all_solutions (outp, board_size, all_solutions)
class(*), intent(in) :: outp
class(*), intent(in) :: board_size
class(*), intent(in) :: all_solutions
 
integer(size_kind) :: n
 
n = length (all_solutions)
write (int_cast (outp), '("For a board ", I0, " by ", I0, ", ")', advance = 'no') &
& int_cast (board_size), int_cast (board_size)
if (n == 1) then
write (int_cast (outp), '("there is ", I0, " solution.")') n
else
write (int_cast (outp), '("there are ", I0, " solutions.")') n
end if
call for_each (print_spaced_solution, circular_list (outp), &
& circular_list (board_size), all_solutions)
end subroutine print_all_solutions
 
subroutine print_spaced_solution (outp, board_size, solution)
class(*), intent(in) :: outp
class(*), intent(in) :: board_size
class(*), intent(in) :: solution
 
write (int_cast (outp), '()', advance = 'yes')
call print_solution (outp, board_size, solution)
end subroutine print_spaced_solution
 
subroutine print_solution (outp, board_size, solution)
class(*), intent(in) :: outp
class(*), intent(in) :: board_size
class(*), intent(in) :: solution
 
integer :: n_outp
integer :: n_board_size
integer :: rank
integer :: file
integer :: file_of_queen
 
n_outp = int_cast (outp)
n_board_size = int_cast (board_size)
 
do rank = n_board_size, 1, -1
do file = 1, n_board_size
write (n_outp, '("----")', advance = 'no')
end do
write (n_outp, '("-")', advance = 'yes')
 
file_of_queen = n_board_size - int (list_index0 (int_eq, circular_list (rank), solution))
 
do file = 1, n_board_size
if (file == file_of_queen) then
write (n_outp, '("| Q ")', advance = 'no')
else
write (n_outp, '("| ")', advance = 'no')
end if
end do
write (n_outp, '("|")', advance = 'yes')
end do
 
do file = 1, n_board_size
write (n_outp, '("----")', advance = 'no')
end do
write (n_outp, '("-")', advance = 'yes')
end subroutine print_solution
 
subroutine kons (x, y, xy)
class(*), intent(in) :: x
class(*), intent(in) :: y
class(*), allocatable, intent(out) :: xy
 
xy = cons (x, y)
end subroutine kons
 
pure function int_cast (x) result (val)
class(*), intent(in) :: x
integer :: val
 
select type (x)
type is (integer)
val = x
class default
error stop
end select
end function int_cast
 
pure function int_eq (x, y) result (bool)
class(*), intent(in) :: x
class(*), intent(in) :: y
logical :: bool
 
bool = (int_cast (x) == int_cast (y))
end function int_eq
 
subroutine check_garbage
if (aggressive_garbage_collection) then
call collect_garbage_now
else
call check_heap_size
end if
end subroutine check_garbage
 
end program example__n_queens</syntaxhighlight>
{{out}}$ ./example__n_queens 1 2 3 4
<pre style="height:40ex;overflow:scroll">
For a board 1 by 1, there is 1 solution.
 
-----
| Q |
-----
 
For a board 2 by 2, there are 0 solutions.
 
For a board 3 by 3, there are 0 solutions.
 
For a board 4 by 4, there are 2 solutions.
 
-----------------
| | Q | | |
-----------------
| | | | Q |
-----------------
| Q | | | |
-----------------
| | | Q | |
-----------------
 
-----------------
| | | Q | |
-----------------
| Q | | | |
-----------------
| | | | Q |
-----------------
| | Q | | |
-----------------
</pre>
 
=={{header|FreeBASIC}}==
Get slower for N > 14
<syntaxhighlight lang="freebasic">' version 13-04-2017
' compile with: fbc -s console
Dim Shared As ULong count, c()
 
Sub n_queens(row As ULong, n As ULong, show As ULong = 0)
 
Dim As ULong x, y
 
For x = 1 To n
For y = 1 To row -1
If c(y) = x OrElse ((row - y) - Abs(x - c(y))) = 0 Then
Continue For, For
End If
Next
c(row) = x
If row < n Then
n_queens(row +1 , n, show)
Else
count += 1
If show <> 0 Then
For y = 1 To n
Print Using "###"; c(y);
Next
Print
End If
End If
Next
 
End Sub
 
' ------=< MAIN >=------
 
Dim As ULong n = 5
ReDim c(n)
' n_queens(1, n, show = 0 only show total | show <> 0 show every solution
n_queens(1, n, 1)
Print Using "## x ## board, ##### solutions"; n; n; count
Print
 
For n = 1 To 14
ReDim c(n)
count = 0
n_queens(1, n)
Print Using "A ## x ## board has ######## solutions"; n; n; count
Next
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre> 1 3 5 2 4
1 4 2 5 3
2 4 1 3 5
2 5 3 1 4
3 1 4 2 5
3 5 2 4 1
4 1 3 5 2
4 2 5 3 1
5 2 4 1 3
5 3 1 4 2
5 x 5 board, 10 solutions
 
A 1 x 1 board has 1 solutions
A 2 x 2 board has 0 solutions
A 3 x 3 board has 0 solutions
A 4 x 4 board has 2 solutions
A 5 x 5 board has 10 solutions
A 6 x 6 board has 4 solutions
A 7 x 7 board has 40 solutions
A 8 x 8 board has 92 solutions
A 9 x 9 board has 352 solutions
A 10 x 10 board has 724 solutions
A 11 x 11 board has 2680 solutions
A 12 x 12 board has 14200 solutions
A 13 x 13 board has 73712 solutions
A 14 x 14 board has 365596 solutions</pre>
 
=== Alternate version : recursive ===
 
<syntaxhighlight lang="freebasic">Sub aux(n As Integer, i As Integer, a() As Integer, _
u() As Integer, v() As Integer, ByRef m As LongInt)
 
Dim As Integer j, k, p, q
If i > n Then
m += 1
For k = 1 To n : Print a(k); : Next : Print
Else
For j = i To n
k = a(j)
p = i - k + n
q = i + k - 1
If u(p) And v(q) Then
u(p) = 0 : v(q) = 0
a(j) = a(i) : a(i) = k
aux(n, i + 1, a(), u(), v(), m)
u(p) = 1 : v(q) = 1
a(i) = a(j) : a(j) = k
End If
Next
End If
End Sub
 
Dim As Integer n, i
Dim m As LongInt = 1
If Command(1) <> "" Then
n = CInt(Command(1))
ReDim a(1 To n) As Integer
ReDim u(1 To 2 * n - 1) As Integer
ReDim v(1 To 2 * n - 1) As Integer
For i = 1 To n
a(i) = i
Next
For i = 1 To 2 * n - 1
u(i) = 1
v(i) = 1
Next
m = 0
aux(n, 1, a(), u(), v(), m)
Print m
End If</syntaxhighlight>
 
=== Alternate version : iterative ===
 
<syntaxhighlight lang="freebasic">Dim As Integer n, i, j, k, p, q
Dim m As LongInt = 0
 
If Command(1) <> "" Then
n = CInt(Command(1))
ReDim a(1 To n) As Integer
ReDim s(1 To n) As Integer
ReDim u(1 To 2 * n - 1) As Integer
ReDim v(1 To 2 * n - 1) As Integer
For i = 1 To n
a(i) = i
Next
For i = 1 To 2 * n - 1
u(i) = 1
v(i) = 1
Next
m = 0
i = 1
L1: If i > n Then
m += 1
For k = 1 To n : Print a(k); : Next : Print
Goto L4
End If
j = i
L2: k = a(j)
p = i - k + n
q = i + k - 1
If u(p) And v(q) Then
u(p) = 0 : v(q) = 0
a(j) = a(i) : a(i) = k
s(i) = j
i += 1
Goto L1
End If
L3: j += 1 : If j <= n Goto L2
L4: i -= 1 : If i = 0 Then Print m : End
j = s(i)
k = a(i) : a(i) = a(j) : a(j) = k
p = i - k + n
q = i + k - 1
u(p) = 1 : v(q) = 1
Goto L3
End If</syntaxhighlight>
 
=={{header|Frink}}==
This example uses Frink's built-in <CODE>array.permute[]</CODE> method to generate possible permutations of the board efficiently.
<syntaxhighlight lang="frink">solution[board] :=
{
for q = 0 to length[board] - 1
for c = q+1 to length[board] - 1
if board@q == board@c + (c - q) or board@q == board@c - (c - q)
return false
return true
}
 
for b = array[1 to 8].permute[]
if solution[b]
println[b]</syntaxhighlight>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/N-queens_problem}}
 
'''Solution'''
 
The following function:
 
* Is able to calculate solution for chessboards of any size (but it is slow for big chessboards)
* It does not detect rotated or reflected solutions
 
This is an example of backtracking:
 
[[File:Fōrmulæ - N-queens problem 01.png]]
 
[[File:Fōrmulæ - N-queens problem 02.png]]
 
[[File:Fōrmulæ - N-queens problem 03.png]]
 
'''Improvement.''' The following functions calls the previous one, but shows the solution on a more friendly way
 
[[File:Fōrmulæ - N-queens problem 04.png]]
 
[[File:Fōrmulæ - N-queens problem 05.png]]
 
[[File:Fōrmulæ - N-queens problem 06.png]]
 
=={{header|GAP}}==
Line 3,896 ⟶ 6,912:
Translation of Fortran 77. See also alternate Python implementation. One function to return the number of solutions, another to return the list of permutations.
 
<langsyntaxhighlight lang="gap">NrQueens := function(n)
local a, up, down, m, sub;
a := [1 .. n];
Line 3,973 ⟶ 6,989:
[ 0, 0, 0, 0, 0, 0, 1, 0 ],
[ 0, 1, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 1, 0, 0, 0, 0 ] ]</langsyntaxhighlight>
 
=={{header|Go}}==
===Niklaus Wirth algorithm (Wikipedia)===
<lang go>// A fairly literal translation of the example program on the referenced
<syntaxhighlight lang="go">// A fairly literal translation of the example program on the referenced
// WP page. Well, it happened to be the example program the day I completed
// the task. It seems from the WP history that there has been some churn
Line 3,983 ⟶ 7,000:
// Data Structures = Programs."
package main
 
import "fmt"
 
var (
i int
Line 3,994 ⟶ 7,011:
x [9]int
)
 
func try(i int) {
for j := 1; ; j++ {
Line 4,019 ⟶ 7,036:
}
}
 
func main() {
for i := 1; i <= 8; i++ {
Line 4,036 ⟶ 7,053:
}
}
}</langsyntaxhighlight>
 
{{out}}
<pre>
Line 4,047 ⟶ 7,065:
7 2
8 4
</pre>
 
=== Refactored Niklaus Wirth algorithm (clearer/Go friendly solution) ===
<syntaxhighlight lang="go">/*
* N-Queens Problem
*
* For an NxN chess board, 'safely' place a chess queen in every column and row such that none can attack another.
* This solution is based Wirth Pascal solution, although a tad cleaner, thus easier to understand as it uses Go/C
* style indexing and naming, and also prints the Queen using a Unicode 'rune' (which other languages do not handle natively).
*
* N rows by N columns are number left to right top to bottom 0 - 7
*
* There are 2N-1 diagonals (showing an 8x8)
* the upper-right to lower-left are numbered row + col that is:
* 0 1 2 3 4 5 6 7
* 1 2 3 4 5 6 7 8
* 2 3 4 5 6 7 8 9
* 3 4 5 6 7 8 9 10
* 4 5 6 7 8 9 10 11
* 5 6 7 8 9 10 11 12
* 6 7 8 9 10 11 12 13
* 7 8 9 10 11 12 13 14
*
* the upper-left to lower-right are numbered N-1 + row - col
* 7 6 5 4 3 2 1 0
* 8 7 6 5 4 3 2 1
* 9 8 7 6 5 4 3 2
* 10 9 8 7 6 5 4 3
* 11 10 9 8 7 6 5 4
* 12 11 10 9 8 7 6 5
* 13 12 11 10 9 8 7 6
* 14 13 12 11 10 9 8 7
*/
 
package main
 
import "fmt"
 
const N = 8
const HAS_QUEEN = false
const EMPTY = true
const UNASSIGNED = -1
const white_queen = '\u2655'
 
 
var row_num[N]int // results, indexed by row will be the column where the queen lives (UNASSIGNED) is empty
var right_2_left_diag[(2*N-1)]bool // T if no queen in diag[idx]: row i, column col is diag i+col
var left_2_right_diag[(2*N-1)]bool // T is no queen in diag[idx], row i, column col is N-1 + i-col
 
 
func printresults() {
for col := 0; col < N; col++ {
if col != 0 {
fmt.Printf(" ");
}
fmt.Printf("%d,%d", col, row_num[col])
}
fmt.Printf("\n");
for row := 0; row < N; row++ {
for col := 0; col < N; col++ {
if col == row_num[row] {
fmt.Printf(" %c ", white_queen)
} else {
fmt.Printf(" . ")
}
}
fmt.Printf("\n")
}
}
 
/*
* save a queen on the board by saving where we think it should go, and marking the diagonals as occupied
*/
func savequeen(row int, col int) {
row_num[row] = col // save queen column for this row
right_2_left_diag[row+col] = HAS_QUEEN // mark forward diags as occupied
left_2_right_diag[row-col+(N-1)] = HAS_QUEEN // mark backward diags as occupied
}
 
/*
* backout a previously saved queen by clearing where we put it, and marking the diagonals as empty
*/
 
func clearqueen(row int, col int) {
row_num[row] = UNASSIGNED
right_2_left_diag[row+col] = EMPTY
left_2_right_diag[row-col+(N-1)] = EMPTY
}
 
/*
* for each column try the solutions
*/
func trycol(col int) bool {
// check each row to look for the first empty row that does not have a diagonal in use too
for row := 0; row < N; row++ {
if row_num[row] == UNASSIGNED && // has the row been used yet?
right_2_left_diag[row+col] == EMPTY && // check for the forward diags
left_2_right_diag[row-col+(N-1)] == EMPTY { // check for the backwards diags
savequeen(row, col) // this is a possible solution
// Tricky part here: going forward thru the col up to but not including the rightmost one
// if this fails, we are done, no need to search any more
if col < N-1 && !trycol(col+1) {
// ok this did not work - we need to try a different row, so undo the guess
clearqueen(row, col)
} else {
// we have a solution on this row/col, start popping the stack.
return true
}
}
}
return false // not a solution for this col, pop the stack, undo the last guess, and try the next one
}
 
func main() {
for i := 0; i < N ; i++ {
row_num[i] = UNASSIGNED
}
for i := 0; i < 2*N-1 ; i++ {
right_2_left_diag[i] = EMPTY
}
for i := 0; i < 2*N-1 ; i++ {
left_2_right_diag[i] = EMPTY
}
trycol(0)
printresults()
}</syntaxhighlight>
{{out}}
<pre>
0,0 1,6 2,4 3,7 4,1 5,3 6,5 7,2
♕ . . . . . . .
. . . . . . ♕ .
. . . . ♕ . . .
. . . . . . . ♕
. ♕ . . . . . .
. . . ♕ . . . .
. . . . . ♕ . .
. . ♕ . . . . .
</pre>
 
===Dancing Links / Algorithm X===
Using Knuth's
[[WP:Dancing_Links|dancing links]] technique to implement his
[[WP:Algorithm_X|Knuth's Algorithm X]].
The Go code for this technique is in the
[[N-queens_problem/dlx_go|dlx packge]].
 
<syntaxhighlight lang="go">package main
 
import (
"flag"
"fmt"
"log"
"os"
"time"
 
"rosettacode.org/dlx" // or where ever you put the dlx package
)
 
func main() {
log.SetPrefix("N-queens: ")
log.SetFlags(0)
profile := flag.Bool("profile", false, "show DLX profile")
flag.Parse()
 
for N := 2; N <= 18; N++ {
err := nqueens(N, N == 8, *profile)
if err != nil {
log.Fatal(err)
}
}
}
 
func nqueens(N int, printFirst, profile bool) error {
// Build a new DLX matrix with 2N primary columns and 4N-6 secondary
// columns: R0..R(N-1), F0..F(N-1), A1..A(2N-3), B1..B(2N-3).
// We also know the number of cells and solution rows required.
m := dlx.NewWithHint(2*N, 4*N-6, N*N*4-4, 8)
 
s := solution{
N: N,
renumFwd: make([]int, 0, 2*N),
renumBack: make([]int, 2*N),
printFirst: printFirst,
}
 
// column indexes
iR0 := 0
iF0 := iR0 + N
iA1 := iF0 + N
iB1 := iA1 + 2*N - 3
 
// Use "organ-pipe" ordering. E.g. for N=8:
// R4 F4 R3 F3 R5 F5 R2 F2 R6 F6 R1 F1 R7 F7 R0 F0
// This can reduce the number of link updates required by
// almost half for large N; see Knuth's paper for details.
mid := N / 2
for off := 0; off <= N-mid; off++ {
i := mid - off
if i >= 0 {
s.renumBack[iR0+i] = len(s.renumFwd)
s.renumBack[iF0+i] = len(s.renumFwd) + 1
s.renumFwd = append(s.renumFwd, iR0+i, iF0+i)
}
if i = mid + off; off != 0 && i < N {
s.renumBack[iR0+i] = len(s.renumFwd)
s.renumBack[iF0+i] = len(s.renumFwd) + 1
s.renumFwd = append(s.renumFwd, iR0+i, iF0+i)
}
}
 
// Add constraint rows.
// TODO: pre-eliminate symetrical possibilities.
cols := make([]int, 4)
for i := 0; i < N; i++ {
for j := 0; j < N; j++ {
cols[0] = iR0 + i // Ri, rank i
cols[1] = iF0 + j // Fj, file j
a := (i + j) // A(i+j), diagonals
b := (N - 1 - i + j) // B(N-1-i+j), reverse diagonals
cols = cols[:2]
// Do organ-pipe reordering for R and F.
for i, c := range cols {
cols[i] = s.renumBack[c]
}
 
// Only add diagonals with more than one space; that
// is we omit the corners: A0, A(2N-2), B0, and B(2N-2)
if 0 < a && a < 2*N-2 {
cols = append(cols, iA1+a-1)
}
if 0 < b && b < 2*N-2 {
cols = append(cols, iB1+b-1)
}
 
m.AddRow(cols)
}
}
 
// Search for solutions.
start := time.Now()
err := m.Search(s.found)
if err != nil {
return err
}
elapsed := time.Since(start)
fmt.Printf("%d×%d queens has %2d solutions, found in %v\n", N, N, s.count, elapsed)
if profile {
m.ProfileWrite(os.Stderr)
}
return nil
}
 
type solution struct {
N int
count int
renumFwd []int // for "organ-pipe" column ordering
renumBack []int
printFirst bool
}
 
func (s *solution) found(m *dlx.Matrix) error {
s.count++
if s.printFirst && s.count == 1 {
fmt.Printf("First %d×%d queens solution:\n", s.N, s.N)
for _, cols := range m.SolutionIDs(nil) {
var r, f int
for _, c := range cols {
// Undo organ-pipe reodering
if c < len(s.renumFwd) {
c = s.renumFwd[c]
}
if c < s.N {
r = c + 1
} else if c < 2*s.N {
f = c - s.N + 1
}
}
fmt.Printf(" R%d F%d\n", r, f)
}
}
return nil
}</syntaxhighlight>
{{out}}
<pre>
2×2 queens has 0 solutions, found in 1.915µs
3×3 queens has 0 solutions, found in 1.22µs
4×4 queens has 2 solutions, found in 3.095µs
5×5 queens has 10 solutions, found in 7.15µs
6×6 queens has 4 solutions, found in 17.663µs
7×7 queens has 40 solutions, found in 54.08µs
First 8×8 queens solution:
R5 F1
R1 F4
R3 F5
R4 F3
R6 F6
R2 F7
R7 F8
R8 F2
8×8 queens has 92 solutions, found in 186.991µs
9×9 queens has 352 solutions, found in 580.225µs
10×10 queens has 724 solutions, found in 2.078235ms
11×11 queens has 2680 solutions, found in 8.186708ms
12×12 queens has 14200 solutions, found in 38.037841ms
13×13 queens has 73712 solutions, found in 183.846653ms
14×14 queens has 365596 solutions, found in 961.249859ms
15×15 queens has 2279184 solutions, found in 5.491853276s
16×16 queens has 14772512 solutions, found in 33.286561009s
17×17 queens has 95815104 solutions, found in 3m34.643824374s
18×18 queens has 666090624 solutions, found in 24m22.30241617s
</pre>
 
Line 4,052 ⟶ 7,381:
===Distinct Solutions===
This solver starts with the N! distinct solutions to the N-Rooks problem and then keeps only the candidates in which all Queens are mutually diagonal-safe.
<langsyntaxhighlight lang="groovy">def listOrder = { a, b ->
def k = [a.size(), b.size()].min()
def i = (0..<k).find { a[it] != b[it] }
Line 4,075 ⟶ 7,404:
// each permutation is an N-Rooks solution
orderedPermutations((0..<n)).findAll (diagonalSafe)
}</langsyntaxhighlight>
 
===Unique Solutions===
Unique solutions are equivalence classes of distinct solutions, factoring out all reflections and rotations of a given solution. See the [[WP:Eight_queens_puzzle|Wikipedia page]] for more details.
<langsyntaxhighlight lang="groovy">class Reflect {
public static final diag = { list ->
final n = list.size()
Line 4,129 ⟶ 7,458:
}
qus
}</langsyntaxhighlight>
 
===Test and Results===
This script tests both distinct and unique solution lists.
<langsyntaxhighlight lang="groovy">(1..9).each { n ->
def qds = queensDistinctSolutions(n)
def qus = queensUniqueSolutions(qds)
Line 4,140 ⟶ 7,469:
else { println "first:${qus[0]}"; println "last:${qus[-1]}" }
println()
}</langsyntaxhighlight>
 
Interpreting the Results:
Line 4,154 ⟶ 7,483:
 
In other words, this:
<pre>|///| Q |///| ---- ---- ---- ---- |
3 | |////| ♛ |////|
--- --- --- ---
---- ---- ---- ----
| |///| |/Q/|
2 |/♛/| |////| |
--- --- --- ---
---- ---- ---- ----
|/Q/| |///| |
1 | |////| |/♛/|
--- --- --- ---
---- ---- ---- ----
| |///| Q |///|</pre>
0 |////| ♛ |////| |
---- ---- ---- ----
0 1 2 3</pre>
 
Results:
Line 4,207 ⟶ 7,539:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad
import Data.List
 
Line 4,236 ⟶ 7,568:
 
-- prints all the solutions for 6 queens
main = mapM_ printSolution $ queens 6</langsyntaxhighlight>
 
If you just want one solution, simply take the <code>head</code> of the result of <code>queens n</code>; since Haskell is lazy, it will only do as much work as needed to find one solution and stop.
 
===Alternative version===
<langsyntaxhighlight lang="haskell">import Control.Monad (foldM)
import Data.List ((\\))
 
Line 4,251 ⟶ 7,583:
where
f qs _ = [q:qs | q <- [1..n] \\ qs, q `notDiag` qs]
q `notDiag` qs = and [abs (q - qi) /= i | (qi,i) <- qs `zip` [1..]]</langsyntaxhighlight>
 
===Using permutations===
This version uses permutations to generate unique horizontal and vertical position for each queen. Thus, we only need to check diagonals. However, it is less efficient than the previous version because it does not prune out prefixes that are found to be unsuitable.
<langsyntaxhighlight lang="haskell">import Data.List (nub, permutations)
 
-- checks if queens are on the same diagonal
Line 4,266 ⟶ 7,598:
 
-- 8 is for "8 queens"
main = print $ generate 8</langsyntaxhighlight>
 
===In terms of foldr===
A back-tracking variant using the Prelude's plain '''foldr''':
{{Trans|JavaScript}}
<syntaxhighlight lang="haskell">import Data.List (intercalate, transpose)
 
--------------------- N QUEENS PROBLEM -------------------
 
queenPuzzle :: Int -> Int -> [[Int]]
queenPuzzle nRows nCols
| nRows <= 0 = [[]]
| otherwise =
foldr
(\x y -> y <> foldr (go x) [] [1 .. nCols])
[]
$ queenPuzzle (pred nRows) nCols
where
go qs iCol b
| safe (nRows - 1) iCol qs = b <> [qs <> [iCol]]
| otherwise = b
 
safe :: Int -> Int -> [Int] -> Bool
safe iRow iCol qs =
(not . or) $
zipWith
( \sc sr ->
(iCol == sc) || (sc + sr == (iCol + iRow))
|| (sc - sr == (iCol - iRow))
)
qs
[0 .. iRow - 1]
 
--------------------------- TEST -------------------------
-- 10 columns of solutions for the 7*7 board:
showSolutions :: Int -> Int -> [String]
showSolutions nCols nSize =
unlines
. fmap (intercalate " ")
. transpose
. map boardLines
<$> chunksOf nCols (queenPuzzle nSize nSize)
where
go r x
| r == x = '♛'
| otherwise = '.'
boardLines rows =
[ go r <$> [1 .. (length rows)]
| r <- rows
]
 
chunksOf :: Int -> [a] -> [[a]]
chunksOf i = splits
where
splits [] = []
splits l = take i l : splits (drop i l)
 
main :: IO ()
main = (putStrLn . unlines) $ showSolutions 10 7</syntaxhighlight>
{{Out}}
<pre>......♛ ......♛ ......♛ ......♛ .....♛. .....♛. .....♛. .....♛. .....♛. .....♛.
.♛..... ..♛.... ...♛... ....♛.. ♛...... .♛..... ..♛.... ..♛.... ..♛.... ...♛...
...♛... .....♛. ♛...... ..♛.... ..♛.... ....♛.. ......♛ ....♛.. ♛...... ......♛
.....♛. .♛..... ....♛.. ♛...... ....♛.. ♛...... ...♛... ......♛ ...♛... ♛......
♛...... ....♛.. .♛..... .....♛. ......♛ ...♛... ♛...... ♛...... ......♛ ..♛....
..♛.... ♛...... .....♛. ...♛... .♛..... ......♛ ....♛.. ...♛... ....♛.. ....♛..
....♛.. ...♛... ..♛.... .♛..... ...♛... ..♛.... .♛..... .♛..... .♛..... .♛.....
 
.....♛. ....♛.. ....♛.. ....♛.. ....♛.. ....♛.. ....♛.. ...♛... ...♛... ...♛...
...♛... ♛...... ♛...... .♛..... ..♛.... ......♛ ......♛ ♛...... ♛...... .♛.....
.♛..... .....♛. ...♛... .....♛. ♛...... .♛..... .♛..... ....♛.. ..♛.... ......♛
......♛ ...♛... ......♛ ..♛.... .....♛. ...♛... .....♛. .♛..... .....♛. ....♛..
....♛.. .♛..... ..♛.... ......♛ ...♛... .....♛. ..♛.... .....♛. .♛..... ..♛....
..♛.... ......♛ .....♛. ...♛... .♛..... ♛...... ♛...... ..♛.... ......♛ ♛......
♛...... ..♛.... .♛..... ♛...... ......♛ ..♛.... ...♛... ......♛ ....♛.. .....♛.
 
...♛... ...♛... ...♛... ..♛.... ..♛.... ..♛.... ..♛.... ..♛.... ..♛.... .♛.....
.....♛. ......♛ ......♛ ♛...... ♛...... ....♛.. .....♛. ......♛ ......♛ ...♛...
♛...... ....♛.. ..♛.... .....♛. .....♛. ......♛ .♛..... ...♛... .♛..... .....♛.
..♛.... .♛..... .....♛. .♛..... ...♛... .♛..... ....♛.. ♛...... ...♛... ♛......
....♛.. .....♛. .♛..... ....♛.. .♛..... ...♛... ♛...... ....♛.. .....♛. ..♛....
......♛ ♛...... ....♛.. ......♛ ......♛ .....♛. ...♛... .♛..... ♛...... ....♛..
.♛..... ..♛.... ♛...... ...♛... ....♛.. ♛...... ......♛ .....♛. ....♛.. ......♛
 
.♛..... .♛..... .♛..... .♛..... .♛..... .♛..... ♛...... ♛...... ♛...... ♛......
...♛... ....♛.. ....♛.. ....♛.. .....♛. ......♛ ..♛.... ...♛... ....♛.. .....♛.
♛...... ......♛ ..♛.... ♛...... ..♛.... ....♛.. ....♛.. ......♛ .♛..... ...♛...
......♛ ...♛... ♛...... ...♛... ......♛ ..♛.... ......♛ ..♛.... .....♛. .♛.....
....♛.. ♛...... ......♛ ......♛ ...♛... ♛...... .♛..... .....♛. ..♛.... ......♛
..♛.... ..♛.... ...♛... ..♛.... ♛...... .....♛. ...♛... .♛..... ......♛ ....♛..
.....♛. .....♛. .....♛. .....♛. ....♛.. ...♛... .....♛. ....♛.. ...♛... ..♛....</pre>
 
===Breadth-first search and Depth-first search===
<syntaxhighlight lang="haskell">import Control.Monad
import System.Environment
 
-- | data types for the puzzle
type Row = Int
type State = [Row]
type Thread = [Row]
 
-- | utility functions
empty = null
 
-- | Check for infeasible states
infeasible :: Int -> (State, Thread) -> Bool
infeasible n ([], _) = False
infeasible n ((r:rs),t) = length rs >= n || attack r rs || infeasible n (rs, t)
 
feasible n st = not $ infeasible n st
 
-- | Check if a row is attacking another row of a state
attack :: Row -> [Row] -> Bool
attack r rs = r `elem` rs
|| r `elem` (upperDiag rs)
|| r `elem` (lowerDiag rs)
where
upperDiag xs = zipWith (-) xs [1..]
lowerDiag xs = zipWith (+) xs [1..]
 
-- | Check if it is a goal state
isGoal :: Int -> (State, Thread) -> Bool
isGoal n (rs,t) = (feasible n (rs,t)) && (length rs == n)
 
-- | Perform a move
move :: Int -> (State, Thread) -> (State, Thread)
move x (s,t) = (x:s, x:t)
 
choices n = [1..n]
moves n = pure move <*> choices n
 
emptySt = ([],[])
 
-- | Breadth-first search
bfs :: Int -> [(State, Thread)] -> (State, Thread)
bfs n [] = error "Could not find a feasible solution"
bfs n sts | (not.empty) goal = head goal
| otherwise = bfs n sts2
where
goal = filter (isGoal n) sts2
sts2 = filter (feasible n) $ (moves n) <*> sts
 
-- | Depth-first search
dfs :: Int -> (State, Thread) -> [(State, Thread)]
dfs n st | isGoal n st = [st]
| infeasible n st = [emptySt]
| otherwise = do x <- [1..n]
st2 <- dfs n $ move x st
guard $ st2 /= emptySt
return st2
 
main = do
[narg] <- getArgs
let n = read narg :: Int
print (bfs n [emptySt])
print (head $ dfs n emptySt)</syntaxhighlight>
 
{{Out}}
<pre>([1,5,8,6,3,7,2,4],[1,5,8,6,3,7,2,4])
([4,2,7,3,6,8,5,1],[4,2,7,3,6,8,5,1])</pre>
 
=={{header|Heron}}==
<langsyntaxhighlight lang="heron">module NQueens {
inherits {
Heron.Windows.Console;
Line 4,363 ⟶ 7,854:
}
}
}</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Here's a solution to the <tt>n = 8</tt> case:
<langsyntaxhighlight lang="icon">procedure main()
write(q(1), " ", q(2), " ", q(3), " ", q(4), " ", q(5), " ", q(6), " ", q(7), " ", q(8))
end
Line 4,382 ⟶ 7,873:
every 0 = row[r := 1 to 8] = ddiag[r + c - 1] = udiag[8 + r - c] do # test if free
suspend row[r] <- ddiag[r + c - 1] <- udiag[8 + r - c] <- r # place and yield
end</langsyntaxhighlight>
 
Notes:
Line 4,393 ⟶ 7,884:
* As the calls to q() are evaluated in main, each one will suspend a possible row, thereby allowing the next q(n) in main to be evaluated. If any of the q() fails to yield a row for the nth queen (or runs out of solutions) the previous, suspended calls to q() are backtracked progressively. If the final q(8) yields a row, the write() will be called with the row positions of each queen. Note that even the final q(8) will be suspended along with the other 7 calls to q(). Unless the write() is driven to produce more solutions (see next point) the suspended procedures will be closed at the "end of statement" ie after the write has "succeeded".
* If you want to derive all possible solutions, main() can be embellished with the '''every''' keyword:
<langsyntaxhighlight lang="icon">
procedure main()
every write(q(1), " ", q(2), " ", q(3), " ", q(4), " ", q(5), " ", q(6), " ", q(7), " ", q(8))
end
</syntaxhighlight>
</lang>
This drives the backtracking to find more solutions.
 
Line 4,405 ⟶ 7,896:
The comment explains how to modify the program to produce <i>all</i>
solutions for a given <tt>N</tt>.
<langsyntaxhighlight lang="icon">global n, rw, dd, ud
 
procedure main(args)
Line 4,439 ⟶ 7,930:
write()
return # Comment out to see all possible solutions
end</langsyntaxhighlight>
 
A sample run for <tt>N = 6</tt>:
Line 4,461 ⟶ 7,952:
{{libheader|Icon Programming Library}}
Two solutions are in the IPL [http://www.cs.arizona.edu/icon/library/progs/queens.htm queens] and [http://www.cs.arizona.edu/icon/library/progs/genqueen.htm genqueen].
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "NQueens.bas"
110 TEXT 80
120 DO
130 INPUT PROMPT "Size of board (2-12): ":N$
140 LET N=VAL(N$)
150 LOOP UNTIL N>1 AND N<13
160 NUMERIC A(1 TO N),X(1 TO N),B(2 TO 2*N),C(-N+1 TO N-1)
170 LET SOL=0
180 CALL INIT(A):CALL INIT(B):CALL INIT(C)
190 CALL TRY(1)
200 PRINT SOL;"solutions."
210 END
220 DEF WRITE
230 LET S$="":LET SOL=SOL+1
240 FOR K=1 TO N
250 LET S$=S$&CHR$(64+K)&STR$(X(K))&" "
260 NEXT
270 PRINT S$
280 END DEF
290 DEF TRY(I)
300 NUMERIC J
310 FOR J=1 TO N
320 IF A(J) AND B(I+J) AND C(I-J) THEN
330 LET X(I)=J:LET A(J),B(I+J),C(I-J)=0
340 IF I<N THEN
350 CALL TRY(I+1)
360 ELSE
370 CALL WRITE
380 END IF
390 LET A(J),B(I+J),C(I-J)=1
400 END IF
410 NEXT
420 END DEF
430 DEF INIT(REF T)
440 FOR I=LBOUND(T) TO UBOUND(T)
450 LET T(I)=1
460 NEXT
470 END DEF</syntaxhighlight>
 
=={{header|J}}==
Line 4,466 ⟶ 7,997:
This is one of several J solutions shown and explained on this [[J:Essays/N%20Queens%20Problem|J wiki page]]
 
<langsyntaxhighlight lang="j">perm =: ! A.&i. ] NB. all permutations of integers 0 to y
comb2 =: (, #: I.@,@(</)&i.)~ NB. all size 2 combinations of integers 0 to y
mask =: [ */@:~:&(|@-/) {
queenst=: comb2 (] #"1~ mask)&.|: perm</langsyntaxhighlight>
 
Note that the Roger Hui's approach (used here) matches the description attributed to Raymond Hettinger (in the Python implementation). (Both were posted years ago: 20081981 for Hui's version which was used here, and 2009 for Hettinger's.) However they do use different diagonal queen clash elimination approaches -see [http://rosettacode.org/wiki/N-queens_problem#Roger_Hui_.281981.29_Algorithm C# Roger Hui Algorithm] for a comparison of the two approaches.
 
Example use:
 
<langsyntaxhighlight lang="j"> $queenst 8
92 8</langsyntaxhighlight>
 
92 distinct solutions for an 8 by 8 board.
 
<langsyntaxhighlight lang="j"> {.queenst 8
0 4 7 5 2 6 1 3</langsyntaxhighlight>
 
One of the solutions. Position indicates row number, the integer indicates column number (0..7) for each queen -- though of course you could just as validly think of that the other way around.
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class NQueens {
 
private static int[] b = new int[8];
Line 4,533 ⟶ 8,064:
}
}
}</langsyntaxhighlight>
 
=={{header|Javascript}}==
===ES5===
Algorithm uses recursive Backtracking. Checks for correct position on subfields, whichs saves a lot position checks. Needs 15.720 position checks for a 8x8 field.
<syntaxhighlight lang="javascript">function queenPuzzle(rows, columns) {
<lang javascript>
function queenPuzzle(rows, columns) {
if (rows <= 0) {
return [[]];
Line 4,570 ⟶ 8,101:
}
 
console.log(queenPuzzle(8,8));</syntaxhighlight>
 
</lang>
===ES6===
Translating the ES5 version, and adding a function to display columns of solutions.
<syntaxhighlight lang="javascript">(() => {
"use strict";
 
// ---------------- N QUEENS PROBLEM -----------------
 
// queenPuzzle :: Int -> Int -> [[Int]]
const queenPuzzle = intCols => {
// All solutions for a given number
// of columns and rows.
const go = nRows =>
nRows <= 0 ? [
[]
] : go(nRows - 1).reduce(
(a, solution) => [
...a, ...(
enumFromTo(0)(intCols - 1)
.reduce((b, iCol) =>
safe(
nRows - 1, iCol, solution
) ? (
[...b, [...solution, iCol]]
) : b, [])
)
], []
);
 
 
return go;
};
 
// safe : Int -> Int -> [Int] -> Bool
const safe = (iRow, iCol, solution) =>
!zip(solution)(
enumFromTo(0)(iRow - 1)
)
.some(
([sc, sr]) => (iCol === sc) || (
sc + sr === iCol + iRow
) || (sc - sr === iCol - iRow)
);
 
// ---------------------- TEST -----------------------
// Ten columns of solutions to the 7*7 board
 
// main :: IO ()
const main = () =>
// eslint-disable-next-line no-console
console.log(
showSolutions(10)(7)
);
 
// --------------------- DISPLAY ---------------------
 
// showSolutions :: Int -> Int -> String
const showSolutions = nCols =>
// Display of solutions, in nCols columns
// for a board of size N * N.
n => chunksOf(nCols)(
queenPuzzle(n)(n)
)
.map(xs => transpose(
xs.map(
rows => rows.map(
r => enumFromTo(1)(rows.length)
.flatMap(
x => r === x ? (
"♛"
) : "."
)
.join("")
)
)
)
.map(cells => cells.join(" "))
)
.map(x => x.join("\n"))
.join("\n\n");
 
 
// ---------------- GENERIC FUNCTIONS ----------------
 
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = n => {
// xs split into sublists of length n.
// The last sublist will be short if n
// does not evenly divide the length of xs .
const go = xs => {
const chunk = xs.slice(0, n);
 
return Boolean(chunk.length) ? [
chunk, ...go(xs.slice(n))
] : [];
};
 
return go;
};
 
 
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m =>
n => Array.from({
length: 1 + n - m
}, (_, i) => m + i);
 
 
// transpose_ :: [[a]] -> [[a]]
const transpose = rows =>
// The columns of the input transposed
// into new rows.
// Simpler version of transpose, assuming input
// rows of even length.
Boolean(rows.length) ? rows[0].map(
(_, i) => rows.flatMap(
v => v[i]
)
) : [];
 
 
// zip :: [a] -> [b] -> [(a, b)]
const zip = xs =>
// The paired members of xs and ys, up to
// the length of the shorter of the two lists.
ys => Array.from({
length: Math.min(xs.length, ys.length)
}, (_, i) => [xs[i], ys[i]]);
 
// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
<pre>....... ....... ....... ....... ♛...... ♛...... ♛...... ♛...... ♛...... ♛......
.♛..... ..♛.... ...♛... ....♛.. ..♛.... ..♛.... ...♛... ...♛... ...♛... ....♛..
...♛... .....♛. ♛...... ..♛.... ....... ....♛.. ....... .♛..... .....♛. .♛.....
.....♛. .♛..... ....♛.. ♛...... .....♛. ....... ..♛.... ....... ..♛.... .....♛.
♛...... ....♛.. .♛..... .....♛. ...♛... .♛..... .....♛. .....♛. ....... ..♛....
..♛.... ♛...... .....♛. ...♛... .♛..... ...♛... .♛..... ..♛.... .♛..... .......
....♛.. ...♛... ..♛.... .♛..... ....♛.. .....♛. ....♛.. ....♛.. ....♛.. ...♛...
 
♛...... .♛..... .♛..... .♛..... .♛..... .♛..... .♛..... ..♛.... ..♛.... ..♛....
.....♛. ....... ....... ...♛... ....♛.. .....♛. .....♛. ....... ....... ♛......
...♛... ....♛.. ....♛.. .....♛. ♛...... ♛...... ..♛.... .♛..... ...♛... .....♛.
.♛..... ♛...... ..♛.... ♛...... ...♛... ..♛.... ....... ....♛.. ♛...... ...♛...
....... ...♛... ♛...... ..♛.... ....... ....♛.. ...♛... ♛...... ....♛.. .♛.....
....♛.. .....♛. .....♛. ....♛.. ..♛.... ....... ♛...... .....♛. .♛..... .......
..♛.... ..♛.... ...♛... ....... .....♛. ...♛... ....♛.. ...♛... .....♛. ....♛..
 
..♛.... ..♛.... ..♛.... ...♛... ...♛... ...♛... ...♛... ...♛... ...♛... ....♛..
....♛.. .....♛. .....♛. ....... ....... ♛...... .♛..... .....♛. .....♛. .......
....... .♛..... ...♛... ..♛.... ....♛.. ....♛.. ....... ♛...... ♛...... .♛.....
.♛..... ....♛.. ♛...... .....♛. ..♛.... .♛..... ....♛.. ..♛.... ....♛.. ...♛...
...♛... ♛...... ....♛.. .♛..... ♛...... .....♛. ..♛.... ....♛.. .♛..... .....♛.
.....♛. ...♛... ....... ....♛.. .....♛. ..♛.... ♛...... ....... ....... ♛......
♛...... ....... .♛..... ♛...... .♛..... ....... .....♛. .♛..... ..♛.... ..♛....
 
....♛.. ....♛.. ....♛.. ....♛.. ....♛.. ....♛.. .....♛. .....♛. .....♛. .....♛.
♛...... .♛..... .♛..... .♛..... ..♛.... ..♛.... ♛...... .♛..... ..♛.... ...♛...
...♛... ....... ...♛... .....♛. ♛...... .....♛. ..♛.... ....♛.. ....... .♛.....
....... ..♛.... .....♛. ..♛.... .....♛. ....... ....♛.. ♛...... ...♛... .......
..♛.... .....♛. ....... ....... ...♛... .♛..... ....... ...♛... ♛...... ....♛..
.....♛. ...♛... ..♛.... ...♛... .♛..... ...♛... .♛..... ....... ....♛.. ..♛....
.♛..... ♛...... ♛...... ♛...... ....... ♛...... ...♛... ..♛.... .♛..... ♛......</pre>
 
=={{header|jq}}==
====Single Solution====
Line 4,577 ⟶ 8,272:
This section presents a function for finding a single solution using
the formulae for explicit solutions at [[WP:Eight_queens_puzzle|Eight Queens Puzzle]].
<langsyntaxhighlight lang="jq">def single_solution_queens(n):
def q: "♛";
def init(k): reduce range(0;k) as $i ([]; . + ["."]);
Line 4,601 ⟶ 8,296:
(""; reduce $row[] as $x (.; . + $x) + "\n");
 
single_solution_queens(8) | pp</langsyntaxhighlight>
{{out}}
$ jq -M -n -r -f n-queens-single-solution.jq
<langsyntaxhighlight lang="sh">...♛....
.....♛..
.......♛
Line 4,611 ⟶ 8,306:
♛.......
..♛.....
....♛...</langsyntaxhighlight>
====Generate-and-test counter====
{{ works with|jq|1.4}}
'''Part 1: Generic functions'''
<langsyntaxhighlight lang="jq"># permutations of 0 .. (n-1)
def permutations(n):
# Given a single array, generate a stream by inserting n at different positions:
Line 4,627 ⟶ 8,322:
end;
 
def count(g): reduce g as $i (0; .+1);</langsyntaxhighlight>
'''Part 2: n-queens'''
<langsyntaxhighlight lang="jq">def queens(n):
def sums:
. as $board
Line 4,645 ⟶ 8,340:
 
count( permutations(n) | select(allowable) );
</syntaxhighlight>
</lang>
'''Example''':
<syntaxhighlight lang ="jq">queens(8)</langsyntaxhighlight>
{{out}}
92
Line 4,653 ⟶ 8,348:
=={{header|Julia}}==
 
<langsyntaxhighlight lang="ruby">"""
#!/usr/bin/env julia
 
__precompile__(true)
 
"""
# EightQueensPuzzle
 
Line 4,666 ⟶ 8,356:
module EightQueensPuzzle
 
export mainBoard, solve!
 
typemutable struct Board
cols::Int
nodes::Int
Line 4,680 ⟶ 8,370:
"Marks occupancy."
function mark!(b::Board, k::Int, j::Int)
b.cols $= (1 << j)
b.diag135 $= (1 << (j+k))
b.diag45 $= (1 << (32+j-k))
end
 
Line 4,713 ⟶ 8,403:
end
 
end # module
"C/C++-style `main` function."
function main()
for n = 1:17
gc()
b = Board()
@show n
print("elapsed:")
solutions = @time solve!(b, n-1, n)
@show solutions
println()
end
end
 
using .EightQueensPuzzle
 
for n = 1:17
b = Board()
@show n
print("elapsed:")
solutions = @time solve!(b, n-1, n)
@show solutions
println()
end
</syntaxhighlight> {{out}}
 
<pre>
using EightQueensPuzzle
 
main()
</lang>
 
<lang ruby>
juser@juliabox:~$ /opt/julia-0.5/bin/julia eight_queen_puzzle.jl
n = 1
elapsed: 0.000001 seconds
Line 4,752 ⟶ 8,434:
 
n = 5
elapsed: 0.000003000002 seconds
solutions = 10
 
n = 6
elapsed: 0.000008000006 seconds
solutions = 4
 
n = 7
elapsed: 0.000028000032 seconds
solutions = 40
 
n = 8
elapsed: 0.000108000168 seconds
solutions = 92
 
n = 9
elapsed: 0.000463000554 seconds
solutions = 352
 
n = 10
elapsed: 0.002146001804 seconds
solutions = 724
 
n = 11
elapsed: 0.010646008528 seconds
solutions = 2680
 
n = 12
elapsed: 0.057603049349 seconds
solutions = 14200
 
n = 13
elapsed: 0.334600292637 seconds
solutions = 73712
 
n = 14
elapsed: 21.055078734187 seconds
solutions = 365596
 
n = 15
elapsed: 1310.480449550665 seconds
solutions = 2279184
 
n = 16
elapsed: 9778.192552840067 seconds
solutions = 14772512
 
n = 17
elapsed:720550.314676816089 seconds
solutions = 95815104
</langpre>
 
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="scala">// version 1.1.3
 
var count = 0
var c = IntArray(0)
var f = ""
 
fun nQueens(row: Int, n: Int) {
outer@ for (x in 1..n) {
for (y in 1..row - 1) {
if (c[y] == x) continue@outer
if (row - y == Math.abs(x - c[y])) continue@outer
}
c[row] = x
if (row < n) nQueens(row + 1, n)
else if (++count == 1) f = c.drop(1).map { it - 1 }.toString()
}
}
 
fun main(args: Array<String>) {
for (n in 1..14) {
count = 0
c = IntArray(n + 1)
f = ""
nQueens(1, n)
println("For a $n x $n board:")
println(" Solutions = $count")
if (count > 0) println(" First is $f")
println()
}
}</syntaxhighlight>
 
{{out}}
<pre>
For a 1 x 1 board:
Solutions = 1
First is [0]
 
For a 2 x 2 board:
Solutions = 0
 
For a 3 x 3 board:
Solutions = 0
 
For a 4 x 4 board:
Solutions = 2
First is [1, 3, 0, 2]
 
For a 5 x 5 board:
Solutions = 10
First is [0, 2, 4, 1, 3]
 
For a 6 x 6 board:
Solutions = 4
First is [1, 3, 5, 0, 2, 4]
 
For a 7 x 7 board:
Solutions = 40
First is [0, 2, 4, 6, 1, 3, 5]
 
For a 8 x 8 board:
Solutions = 92
First is [0, 4, 7, 5, 2, 6, 1, 3]
 
For a 9 x 9 board:
Solutions = 352
First is [0, 2, 5, 7, 1, 3, 8, 6, 4]
 
For a 10 x 10 board:
Solutions = 724
First is [0, 2, 5, 7, 9, 4, 8, 1, 3, 6]
 
For a 11 x 11 board:
Solutions = 2680
First is [0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9]
 
For a 12 x 12 board:
Solutions = 14200
First is [0, 2, 4, 7, 9, 11, 5, 10, 1, 6, 8, 3]
 
For a 13 x 13 board:
Solutions = 73712
First is [0, 2, 4, 1, 8, 11, 9, 12, 3, 5, 7, 10, 6]
 
For a 14 x 14 board:
Solutions = 365596
First is [0, 2, 4, 6, 11, 9, 12, 3, 13, 8, 1, 5, 7, 10]
</pre>
 
=={{header|Liberty BASIC}}==
Program uses permutation generator (stores all permutations) and solves tasks 4x4 to 9x9. It prints all the solutions.
<syntaxhighlight lang="lb">
<lang lb>
'N queens
'>10 would not work due to way permutations used
Line 4,892 ⟶ 8,664:
End Function
 
</syntaxhighlight>
</lang>
 
=={{header|Locomotive Basic}}==
Line 4,898 ⟶ 8,670:
Uses the heuristic from the Wikipedia article to get one solution.
 
<langsyntaxhighlight lang="locobasic">10 mode 1:defint a-z
20 while n<4:input "How many queens (N>=4)";n:wend
30 dim q(n),e(n),o(n)
Line 4,949 ⟶ 8,721:
500 for i=1 to n
510 if o(i)=1 or o(i)=3 then o(i)=-1 else if o(i)=0 then o(i)=1:o(i+1)=3:return
520 next</langsyntaxhighlight>
 
[[File:Queens Puzzle, Locomotive Basic.png]]
Line 4,955 ⟶ 8,727:
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to try :files :diag1 :diag2 :tried
if :files = 0 [make "solutions :solutions+1 show :tried stop]
localmake "safe (bitand :files :diag1 :diag2)
Line 4,971 ⟶ 8,743:
end
 
print queens 8 ; 92</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">N = 8
 
-- We'll use nil to indicate no queen is present.
Line 5,008 ⟶ 8,780:
else
print(string.format("No solution for %d queens.\n", N))
end</langsyntaxhighlight>
 
=={{header|MathematicaM2000 Interpreter}}==
{{trans|VBA}}
<syntaxhighlight lang="m2000 interpreter">
Module N_queens {
Const l = 15 'number of queens
Const b = False 'print option
Dim a(0 to l), s(0 to l), u(0 to 4 * l - 2)
Def long n, m, i, j, p, q, r, k, t
For i = 1 To l: a(i) = i: Next i
For n = 1 To l
m = 0
i = 1
j = 0
r = 2 * n - 1
Do {
i--
j++
p = 0
q = -r
Do {
i++
u(p) = 1
u(q + r) = 1
Swap a(i), a(j)
p = i - a(i) + n
q = i + a(i) - 1
s(i) = j
j = i + 1
} Until j > n Or u(p) Or u(q + r)
If u(p) = 0 Then {
If u(q + r) = 0 Then {
m++ 'm: number of solutions
If b Then {
Print "n="; n; "m="; m
For k = 1 To n {
For t = 1 To n {
Print If$(a(n - k + 1) = t-> "Q", ".");
}
Print
}
}
}
}
j = s(i)
While j >= n And i <> 0 {
Do {
Swap a(i), a(j)
j--
} Until j < i
i--
p = i - a(i) + n
q = i + a(i) - 1
j = s(i)
u(p) = 0
u(q + r) = 0
}
} Until i = 0
Print n, m 'number of queens, number of solutions
Next n
}
N_queens
</syntaxhighlight>
 
=={{header|m4}}==
The following program should work with any POSIX-compliant m4.
It finds one solution of the Eight Queens problem.
 
<syntaxhighlight lang="m4">divert(-1)
 
The following macro find one solution to the eight-queens problem:
 
define(`solve_eight_queens',`_$0(1)')
define(`_solve_eight_queens',
`ifelse(none_of_the_queens_attacks_the_new_one($1),1,
`ifelse(len($1),8,`display_solution($1)',`$0($1`'1)')',
`ifelse(last_is_eight($1),1,`$0(incr_last(strip_eights($1)))',
`$0(incr_last($1))')')')
 
It works by backtracking.
 
Partial solutions are represented by strings. For example, queens at
a7,b3,c6 would be represented by the string "736". The first position
is the "a" file, the second is the "b" file, etc. The digit in a given
position represents the queen's rank.
 
When a new queen is appended to the string, it must satisfy the
following constraint:
 
define(`none_of_the_queens_attacks_the_new_one',
`_$0($1,decr(len($1)))')
define(`_none_of_the_queens_attacks_the_new_one',
`ifelse($2,0,1,
`ifelse(two_queens_attack($1,$2,len($1)),1,0,
`$0($1,decr($2))')')')
 
The `two_queens_attack' macro, used above, reduces to `1' if the
ith and jth queens attack each other; otherwise it reduces to `0':
 
define(`two_queens_attack',
`pushdef(`file1',eval($2))`'dnl
pushdef(`file2',eval($3))`'dnl
pushdef(`rank1',`substr($1,decr(file1),1)')`'dnl
pushdef(`rank2',`substr($1,decr(file2),1)')`'dnl
eval((rank1) == (rank2) ||
((rank1) + (file1)) == ((rank2) + (file2)) ||
((rank1) - (file1)) == ((rank2) - (file2)))`'dnl
popdef(`file1',`file2',`rank1',`rank2')')
 
Here is the macro that converts the solution string to a nice display:
 
define(`display_solution',
`pushdef(`rule',`+----+----+----+----+----+----+----+----+')`'dnl
rule
_$0($1,8)
rule
_$0($1,7)
rule
_$0($1,6)
rule
_$0($1,5)
rule
_$0($1,4)
rule
_$0($1,3)
rule
_$0($1,2)
rule
_$0($1,1)
rule`'dnl
popdef(`rule')')
define(`_display_solution',
`ifelse(index($1,$2),0,`| Q ',`| ')`'dnl
ifelse(index($1,$2),1,`| Q ',`| ')`'dnl
ifelse(index($1,$2),2,`| Q ',`| ')`'dnl
ifelse(index($1,$2),3,`| Q ',`| ')`'dnl
ifelse(index($1,$2),4,`| Q ',`| ')`'dnl
ifelse(index($1,$2),5,`| Q ',`| ')`'dnl
ifelse(index($1,$2),6,`| Q ',`| ')`'dnl
ifelse(index($1,$2),7,`| Q ',`| ')|')
 
Here are some simple macros used above:
 
define(`last',`substr($1,decr(len($1)))') Get the last char.
define(`drop_last',`substr($1,0,decr(len($1)))') Remove the last char.
define(`last_is_eight',`eval((last($1)) == 8)') Is the last char "8"?
define(`strip_eights',
`ifelse(last_is_eight($1),1,`$0(drop_last($1))',
`$1')') Backtrack by removing all final "8" chars.
define(`incr_last',
`drop_last($1)`'incr(last($1))') Increment the final char.
 
The macros here have been presented top-down. I believe the program
might be easier to understand were the macros presented bottom-up;
then there would be no "black boxes" (unexplained macros) as one reads
from top to bottom.
 
I leave such rewriting as an exercise for the reader. :)
 
divert`'dnl
dnl
solve_eight_queens</syntaxhighlight>
 
{{out}}
<pre>+----+----+----+----+----+----+----+----+
| | | Q | | | | | |
+----+----+----+----+----+----+----+----+
| | | | | | Q | | |
+----+----+----+----+----+----+----+----+
| | | | Q | | | | |
+----+----+----+----+----+----+----+----+
| | Q | | | | | | |
+----+----+----+----+----+----+----+----+
| | | | | | | | Q |
+----+----+----+----+----+----+----+----+
| | | | | Q | | | |
+----+----+----+----+----+----+----+----+
| | | | | | | Q | |
+----+----+----+----+----+----+----+----+
| Q | | | | | | | |
+----+----+----+----+----+----+----+----+</pre>
 
=={{header|Maple}}==
 
{{trans|Python}}
 
<syntaxhighlight lang="maple">queens:=proc(n)
local a,u,v,m,aux;
a:=[$1..n];
u:=[true$2*n-1];
v:=[true$2*n-1];
m:=[];
aux:=proc(i)
local j,k,p,q;
if i>n then
m:=[op(m),copy(a)];
else
for j from i to n do
k:=a[j];
p:=i-k+n;
q:=i+k-1;
if u[p] and v[q] then
u[p]:=false;
v[q]:=false;
a[j]:=a[i];
a[i]:=k;
aux(i+1);
u[p]:=true;
v[q]:=true;
a[i]:=a[j];
a[j]:=k;
fi;
od;
fi;
end;
aux(1);
m
end:
 
for a in queens(8) do printf("%a\n",a) od;</syntaxhighlight>
 
{{out}}
 
<pre>[1, 5, 8, 6, 3, 7, 2, 4]
[1, 6, 8, 3, 7, 4, 2, 5]
[1, 7, 4, 6, 8, 2, 5, 3]
...
[8, 2, 5, 3, 1, 7, 4, 6]
[8, 3, 1, 6, 2, 5, 7, 4]
[8, 4, 1, 3, 6, 2, 7, 5]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
This code recurses through the possibilities, using the "safe" method to check if the current set is allowed. The recursive method has the advantage that finding all possibilities is about as hard (code-wise, not computation-wise) as finding just one.
<langsyntaxhighlight Mathematicalang="mathematica">safe[q_List, n_] :=
With[{l = Length@q},
Length@Union@q == Length@Union[q + Range@l] ==
Line 5,020 ⟶ 9,022:
If[Length[q] == n, {q},
Cases[nQueen[Append[q, #], n] & /@ Range[n],
Except[{Null} | {}], {2}]], Null]</langsyntaxhighlight>
 
This returns a list of valid permutations by giving the queen's column number for each row. It can be displayed in a list of chess-board tables like this:
<langsyntaxhighlight Mathematicalang="mathematica">matrixView[n_] :=
Grid[Normal@
SparseArray[MapIndexed[{#, First@#2} -> "Q" &, #], {n, n}, "."],
Frame -> All] & /@ nQueen[n]
matrixView[6] // OutputForm</langsyntaxhighlight>
{{out}}
<pre>{. . . Q . ., . . . . Q ., . Q . . . ., . . Q . . .}
Line 5,044 ⟶ 9,046:
This solution uses Permutations and subsets, also prints out a board representation.
 
<langsyntaxhighlight Mathematicalang="mathematica">n=8;cnt=1;per=Permutations[Range[n],{n}];(* All Permutations of length n *)
Do[per[[q]]=Partition[Riffle[Reverse[Range[n]],per[[q]]],2],{q,1,Length[per]}];(* Riffled in the reverse of [range n] partitioned into pairs*)
Do[w=Subsets[per[[t]],{2}];(* This is a full subset of the previous set of pairs taken 2 at a time *)
Line 5,051 ⟶ 9,053:
If[tot==0,g=Grid[Table[" ",{n},{n}],Alignment->Center,Frame->All,Spacings->{1.2,1}];(* If no clashing diagonals setup an array and print the permutation and the grid*)
Do[g[[1,per[[t,w,1]],per[[t,w,2]]]]="Q",{w,1,n}];
Print[cnt," ",per[[t]]," ",g];cnt++],{t,1,Length[per]}]</langsyntaxhighlight>
 
Alternative Solution using Linear Programming:
 
<syntaxhighlight lang="mathematica">
<lang Mathematica>
dispSol[sol_] := sol /. {1 -> "Q" , 0 -> "-"} // Grid
 
Line 5,100 ⟶ 9,102:
 
solveNqueens[8] // dispSol
</syntaxhighlight>
</lang>
<pre>- - - - Q - - -
- Q - - - - - -
Line 5,109 ⟶ 9,111:
- - - - - - - Q
- - Q - - - - -</pre>
 
=={{header|MATLAB}}==
This solution is inspired by Raymond Hettinger's permutations based solution which was made in Python: https://code.activestate.com/recipes/576647/
<syntaxhighlight lang="matlab">n=8;
solutions=[[]];
v = 1:n;
P = perms(v);
for i=1:length(P)
for j=1:n
sub(j)=P(i,j)-j;
add(j)=P(i,j)+j;
end
if n==length(unique(sub)) && n==length(unique(add))
solutions(end+1,:)=P(i,:);
end
end
 
fprintf('Number of solutions with %i queens: %i', n, length(solutions));
 
if ~isempty(solutions)
%Print first possible solution
board=solutions(1,:);
s = repmat('-',n);
for k=1:length(board)
s(k,board(k)) = 'Q';
end
s
end</syntaxhighlight>
{{out}}
<pre>
Number of solutions with 8 queens: 92
 
'-------Q'
'---Q----'
'Q-------'
'--Q-----'
'-----Q--'
'-Q------'
'------Q-'
'----Q---'
</pre>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">/* translation of Fortran 77, return solutions as permutations */
 
queens(n) := block([a, i, j, m, p, q, r, s, u, v, w, y, z],
Line 5,128 ⟶ 9,171:
[1, 6, 8, 3, 7, 4, 2, 5],
...]] */
length(%); /* 92 */</langsyntaxhighlight>
 
<syntaxhighlight lang="maxima">
/* Inspired by code from Python */
Queens(N):=block([K,C,P,V,L:[]],
C: makelist(K,K,1,N),
P: permutations(C),
for V in P do (
if is(N=length(unique(makelist(V[K]+K, K, C)))) then (
if is(N=length(unique(makelist(V[K]-K, K, C)))) then (
L: endcons(V, L)
)
)
), L
)$
 
Queens(8);length(%);</syntaxhighlight>
 
=={{header|MiniScript}}==
This GUI implementation is for use with [http://miniscript.org/MiniMicro Mini Micro]. It displays a chess board with animation of the possibilities. At the end, after all of the solutions have been calculated, you can scroll through them with the left/right cursor keys.
<syntaxhighlight lang="miniscript">
clear
N = 8
SOLUTIONCOUNT = 0
 
getTileDisplay = function
gfx.clear
queen = file.loadImage("/sys/pics/gamePieces/blackQueen.png")
gfx.color = color.white
gfx.fillRect 0, 0, 80, 80
gfx.fillRect 160, 0, 80, 80
gfx.color = color.brown
gfx.fillRect 80, 0, 80, 80
gfx.fillRect 240, 0, 80, 80
gfx.drawImage queen, 172, 14
gfx.drawImage queen, 252, 14
tiles = gfx.getImage(0,0, 320, 80)
gfx.clear
display(4).mode = displayMode.tile
td = display(4)
td.cellSize = 640 / N
td.extent = [N, N]
td.overlap = 0
td.tileSet = tiles
td.tileSetTileSize = 80
td.scrollX = -160
td.clear
return td
end function
 
updateBoard = function(td, arr)
for y in range(0, N - 1)
ix = y % 2
for x in range(0, N - 1)
td.setCell x, y, ix
ix += 1
ix %= 2
end for
end for
y = 0
for x in arr
td.setCell x, y, td.cell(x, y) + 2
y += 1
end for
yield
end function
 
list.has = function(n)
return self.indexOf(n) != null
end function
 
queens = function(n, i, a, b, c, td)
solutions = []
updateBoard(td, a)
if i < n then
for j in range(0, n - 1)
if not a.has(j) and not b.has(i + j) and not c.has(i - j) then
solution = queens(n, i + 1, a + [j], b + [i + j], c + [i - j], td)
if solution != null then solutions += solution
end if
end for
else
globals.SOLUTIONCOUNT += 1
text.row = 25
text.print "SOLUTIONS"
text.print globals.SOLUTIONCOUNT
solutions.push(a)
end if
return solutions
end function
 
td = getTileDisplay
solutions = queens(N, 0, [], [], [], td)
ix = 0
while true
text.row = 25
text.print "SOLUTION # "
text.print (ix + 1) + (" " * 10)
text.print
text.print char(17) + "/" + char(18) + " keys"
updateBoard(td, solutions[ix])
k = key.get
kcode = code(k)
if kcode == 27 then break
ix = ix - (kcode == 17) + (kcode == 18) + solutions.len
ix %= solutions.len
end while
</syntaxhighlight>
 
=={{header|MiniZinc}}==
<syntaxhighlight lang="minizinc">int: n;
array [1..n] of var 1..n: q; % queen in column i is in row q[i]
 
include "alldifferent.mzn";
 
constraint alldifferent(q); % distinct rows
constraint alldifferent([ q[i] + i | i in 1..n]); % distinct diagonals
constraint alldifferent([ q[i] - i | i in 1..n]); % upwards+downwards
 
% search
solve :: int_search(q, first_fail, indomain_min)
satisfy;
output [ if fix(q[j]) == i then "Q" else "." endif ++
if j == n then "\n" else "" endif | i,j in 1..n]</syntaxhighlight>
 
This solution appears in the official MiniZinc tutorial documentation, and is generalized.
 
=={{header|Modula-2}}==
{{trans|C}}
<syntaxhighlight lang="modula2">MODULE NQueens;
FROM InOut IMPORT Write, WriteCard, WriteString, WriteLn;
 
CONST N = 8;
VAR hist: ARRAY [0..N-1] OF CARDINAL;
count: CARDINAL;
PROCEDURE Solve(n, col: CARDINAL);
VAR i, j: CARDINAL;
PROCEDURE Attack(i, j: CARDINAL): BOOLEAN;
VAR diff: CARDINAL;
BEGIN
IF hist[j] = i THEN RETURN TRUE;
ELSE
IF hist[j] < i THEN diff := i - hist[j];
ELSE diff := hist[j] - i;
END;
RETURN diff = col-j;
END;
END Attack;
BEGIN
IF col = n THEN
INC(count);
WriteLn;
WriteString("No. ");
WriteCard(count, 0);
WriteLn;
WriteString("---------------");
WriteLn;
FOR i := 0 TO n-1 DO
FOR j := 0 TO n-1 DO
IF j = hist[i] THEN Write('Q');
ELSIF (i + j) MOD 2 = 1 THEN Write(' ');
ELSE Write('.');
END;
END;
WriteLn;
END;
ELSE
FOR i := 0 TO n-1 DO
j := 0;
WHILE (j < col) AND (NOT Attack(i,j)) DO INC(j); END;
IF j >= col THEN
hist[col] := i;
Solve(n, col+1);
END;
END;
END;
END Solve;
 
BEGIN
count := 0;
Solve(N, 0);
END NQueens.</syntaxhighlight>
 
{{out}}
<pre style='height:50ex;'>No. 1
---------------
Q . . .
. .Q. .
. . . .Q
. . Q .
. Q . .
. . .Q.
.Q. . .
. Q . .
 
No. 2
---------------
Q . . .
. . Q .
. . . .Q
.Q. . .
. . . Q
. Q . .
.Q. . .
. .Q. .
 
No. 3
---------------
Q . . .
. . .Q.
. .Q. .
. . Q .
. . . .Q
Q . . .
. . Q .
.Q. . .
 
No. 4
---------------
Q . . .
. . .Q.
. . Q .
. . . Q
.Q. . .
. Q . .
. . .Q.
.Q. . .
 
No. 5
---------------
.Q. . .
. Q . .
. . .Q.
. . . Q
. Q . .
Q. . . .
. . . Q
. .Q. .
 
No. 6
---------------
.Q. . .
. .Q. .
. . . Q
Q. . . .
. Q . .
. . . Q
. . .Q.
. Q . .
 
No. 7
---------------
.Q. . .
. .Q. .
. . . Q
. Q . .
Q . . .
. . . Q
. . .Q.
.Q. . .
 
No. 8
---------------
.Q. . .
. . Q .
Q . . .
. . .Q.
. .Q. .
. . . Q
. Q . .
. .Q. .
 
No. 9
---------------
.Q. . .
. . Q .
. . . .Q
.Q. . .
Q . . .
. Q . .
. . . Q
. .Q. .
 
No. 10
---------------
.Q. . .
. . .Q.
. Q . .
. . Q .
. . . .Q
. .Q. .
Q . . .
. Q . .
 
No. 11
---------------
.Q. . .
. . .Q.
. . Q .
. . . Q
Q . . .
. Q . .
. . .Q.
.Q. . .
 
No. 12
---------------
.Q. . .
. . . Q
. . .Q.
Q. . . .
. Q . .
. .Q. .
. . . Q
. Q . .
 
No. 13
---------------
. Q . .
Q. . . .
. . . Q
. .Q. .
. . . .Q
Q . . .
. .Q. .
. . Q .
 
No. 14
---------------
. Q . .
. .Q. .
.Q. . .
. . . Q
Q . . .
. . .Q.
. .Q. .
. . Q .
 
No. 15
---------------
. Q . .
. .Q. .
.Q. . .
. . . Q
. . .Q.
. Q . .
. . . Q
Q. . . .
 
No. 16
---------------
. Q . .
. .Q. .
. . . Q
Q. . . .
. .Q. .
Q . . .
. . . .Q
. . Q .
 
No. 17
---------------
. Q . .
. .Q. .
. . . .Q
. Q . .
Q . . .
. . .Q.
.Q. . .
. . Q .
 
No. 18
---------------
. Q . .
. . Q .
.Q. . .
. .Q. .
. . . .Q
Q. . . .
. . . Q
. Q . .
 
No. 19
---------------
. Q . .
. . Q .
.Q. . .
. . .Q.
Q . . .
. Q . .
. . . .Q
. .Q. .
 
No. 20
---------------
. Q . .
. . Q .
.Q. . .
. . .Q.
. . Q .
Q. . . .
. . . .Q
. Q . .
 
No. 21
---------------
. Q . .
. . Q .
. .Q. .
Q. . . .
. . . .Q
. .Q. .
. . . Q
Q . . .
 
No. 22
---------------
. Q . .
. . Q .
. .Q. .
Q . . .
. . . .Q
. .Q. .
. . . Q
Q. . . .
 
No. 23
---------------
. Q . .
. . Q .
. . . .Q
Q. . . .
. .Q. .
. . .Q.
. . Q .
Q . . .
 
No. 24
---------------
. Q . .
. . Q .
. . . .Q
Q. . . .
. . Q .
. . .Q.
.Q. . .
. Q . .
 
No. 25
---------------
. Q . .
. . Q .
. . . .Q
Q . . .
. .Q. .
Q. . . .
. . . Q
. .Q. .
 
No. 26
---------------
. Q . .
. . .Q.
.Q. . .
. . . Q
. . Q .
Q. . . .
. .Q. .
. . Q .
 
No. 27
---------------
. Q . .
. . .Q.
.Q. . .
. . . Q
. . .Q.
. Q . .
Q . . .
. .Q. .
 
No. 28
---------------
. Q . .
. . . Q
. .Q. .
. . .Q.
Q . . .
. . Q .
.Q. . .
. .Q. .
 
No. 29
---------------
. .Q. .
Q. . . .
. . Q .
. . . Q
.Q. . .
. . .Q.
. Q . .
. . Q .
 
No. 30
---------------
. .Q. .
Q. . . .
. . Q .
. . . Q
. . .Q.
.Q. . .
. . . Q
Q . . .
 
No. 31
---------------
. .Q. .
Q . . .
. . Q .
. . . Q
. . .Q.
Q. . . .
. Q . .
. . .Q.
 
No. 32
---------------
. .Q. .
Q . . .
. . . Q
.Q. . .
. . .Q.
. . . Q
Q . . .
. .Q. .
 
No. 33
---------------
. .Q. .
Q . . .
. . . Q
.Q. . .
. . .Q.
. . . Q
. . Q .
Q. . . .
 
No. 34
---------------
. .Q. .
Q . . .
. . . Q
. .Q. .
Q . . .
. . . Q
. . .Q.
.Q. . .
 
No. 35
---------------
. .Q. .
Q . . .
. . . .Q
. .Q. .
. . . Q
Q. . . .
. Q . .
. . Q .
 
No. 36
---------------
. .Q. .
Q . . .
. . . .Q
. . Q .
Q . . .
.Q. . .
. . Q .
. . .Q.
 
No. 37
---------------
. .Q. .
. . Q .
Q . . .
. .Q. .
.Q. . .
. . . Q
. Q . .
. . .Q.
 
No. 38
---------------
. .Q. .
. . Q .
. . . .Q
Q . . .
. . . Q
Q. . . .
. Q . .
. .Q. .
 
No. 39
---------------
. .Q. .
. . Q .
. . . .Q
.Q. . .
Q . . .
. . .Q.
. . Q .
Q . . .
 
No. 40
---------------
. .Q. .
. . .Q.
Q . . .
. . . Q
. . Q .
Q . . .
. . .Q.
.Q. . .
 
No. 41
---------------
. .Q. .
. . .Q.
. Q . .
. . . Q
.Q. . .
. .Q. .
Q . . .
. . Q .
 
No. 42
---------------
. .Q. .
. . .Q.
. . Q .
Q . . .
. . .Q.
Q. . . .
. Q . .
. . . Q
 
No. 43
---------------
. .Q. .
. . .Q.
. . Q .
.Q. . .
Q . . .
. . Q .
. . . .Q
Q . . .
 
No. 44
---------------
. .Q. .
. . . Q
Q . . .
.Q. . .
. . .Q.
Q . . .
. . . Q
. .Q. .
 
No. 45
---------------
. .Q. .
. . . Q
Q . . .
. .Q. .
. . . Q
Q . . .
. . .Q.
.Q. . .
 
No. 46
---------------
. .Q. .
. . . Q
. . Q .
.Q. . .
Q . . .
. . .Q.
.Q. . .
. . Q .
 
No. 47
---------------
. . Q .
Q. . . .
. .Q. .
. . Q .
. . . .Q
Q . . .
. . . Q
.Q. . .
 
No. 48
---------------
. . Q .
Q. . . .
. . . .Q
. Q . .
.Q. . .
. . .Q.
. Q . .
. . Q .
 
No. 49
---------------
. . Q .
Q. . . .
. . . .Q
. . Q .
. Q . .
. . .Q.
.Q. . .
. Q . .
 
No. 50
---------------
. . Q .
Q . . .
. .Q. .
. . Q .
. . . .Q
.Q. . .
Q . . .
. . .Q.
 
No. 51
---------------
. . Q .
Q . . .
. .Q. .
. . .Q.
. Q . .
. . . Q
. . .Q.
Q. . . .
 
No. 52
---------------
. . Q .
Q . . .
. . .Q.
Q. . . .
. . . Q
. Q . .
. . . .Q
.Q. . .
 
No. 53
---------------
. . Q .
Q . . .
. . . .Q
Q. . . .
. .Q. .
. . .Q.
. Q . .
. . Q .
 
No. 54
---------------
. . Q .
.Q. . .
Q . . .
. . Q .
. . . .Q
Q . . .
. .Q. .
. . .Q.
 
No. 55
---------------
. . Q .
.Q. . .
Q . . .
. . .Q.
.Q. . .
. . . Q
. . .Q.
. Q . .
 
No. 56
---------------
. . Q .
.Q. . .
. . . .Q
. Q . .
. . . Q
Q. . . .
. . .Q.
Q . . .
 
No. 57
---------------
. . Q .
. . .Q.
Q . . .
.Q. . .
. . . .Q
. . Q .
. .Q. .
Q . . .
 
No. 58
---------------
. . Q .
. . .Q.
Q . . .
. Q . .
.Q. . .
. . . Q
. . .Q.
.Q. . .
 
No. 59
---------------
. . Q .
. . .Q.
.Q. . .
. Q . .
. . . .Q
Q. . . .
. Q . .
. . Q .
 
No. 60
---------------
. . Q .
. . .Q.
.Q. . .
. . Q .
. Q . .
Q. . . .
. .Q. .
. . . Q
 
No. 61
---------------
. . Q .
. . .Q.
.Q. . .
. . Q .
. Q . .
Q. . . .
. . . .Q
. Q . .
 
No. 62
---------------
. . Q .
. . .Q.
. .Q. .
Q. . . .
. Q . .
. . . Q
. . .Q.
Q . . .
 
No. 63
---------------
. . Q .
. . . Q
. .Q. .
Q. . . .
. Q . .
. . Q .
.Q. . .
. . .Q.
 
No. 64
---------------
. . Q .
. . . Q
. .Q. .
Q. . . .
. . . Q
Q . . .
. . .Q.
.Q. . .
 
No. 65
---------------
. . .Q.
Q. . . .
. . Q .
Q . . .
. . . .Q
.Q. . .
. . . Q
. Q . .
 
No. 66
---------------
. . .Q.
Q . . .
. . . Q
Q. . . .
. Q . .
. .Q. .
. . . .Q
. Q . .
 
No. 67
---------------
. . .Q.
Q . . .
. . . Q
Q. . . .
. .Q. .
. . . Q
. . Q .
.Q. . .
 
No. 68
---------------
. . .Q.
.Q. . .
Q . . .
. . .Q.
. . Q .
. . . Q
.Q. . .
. Q . .
 
No. 69
---------------
. . .Q.
.Q. . .
Q . . .
. . . Q
. .Q. .
Q . . .
. . . Q
. .Q. .
 
No. 70
---------------
. . .Q.
.Q. . .
Q . . .
. . . Q
. . Q .
Q . . .
. .Q. .
. . .Q.
 
No. 71
---------------
. . .Q.
.Q. . .
. . Q .
. . .Q.
Q . . .
. Q . .
.Q. . .
. . . Q
 
No. 72
---------------
. . .Q.
.Q. . .
. . Q .
. . . Q
Q . . .
. Q . .
.Q. . .
. . .Q.
 
No. 73
---------------
. . .Q.
.Q. . .
. . . Q
Q . . .
. .Q. .
. . . Q
Q . . .
. .Q. .
 
No. 74
---------------
. . .Q.
.Q. . .
. . . Q
Q . . .
. . . .Q
. .Q. .
Q . . .
. Q . .
 
No. 75
---------------
. . .Q.
.Q. . .
. . . Q
. Q . .
Q . . .
. . . Q
.Q. . .
. .Q. .
 
No. 76
---------------
. . .Q.
. Q . .
Q . . .
. .Q. .
. . . .Q
Q . . .
. . . Q
.Q. . .
 
No. 77
---------------
. . .Q.
. Q . .
.Q. . .
. . . Q
. . Q .
. . .Q.
Q . . .
.Q. . .
 
No. 78
---------------
. . .Q.
. Q . .
. . . Q
Q. . . .
. Q . .
. .Q. .
.Q. . .
. . . Q
 
No. 79
---------------
. . .Q.
. Q . .
. . . Q
Q. . . .
. . . .Q
Q . . .
. . Q .
.Q. . .
 
No. 80
---------------
. . .Q.
. . . Q
.Q. . .
. Q . .
Q . . .
. . .Q.
. . Q .
.Q. . .
 
No. 81
---------------
. . . Q
Q. . . .
. Q . .
. . . Q
. . .Q.
. Q . .
.Q. . .
. .Q. .
 
No. 82
---------------
. . . Q
Q . . .
. .Q. .
Q. . . .
. . . .Q
. .Q. .
. Q . .
. . Q .
 
No. 83
---------------
. . . Q
Q . . .
. . .Q.
.Q. . .
Q . . .
. Q . .
. . . .Q
. .Q. .
 
No. 84
---------------
. . . Q
.Q. . .
Q . . .
. . Q .
. . . .Q
. .Q. .
.Q. . .
. Q . .
 
No. 85
---------------
. . . Q
.Q. . .
. . . .Q
Q . . .
. . Q .
Q. . . .
. . .Q.
. Q . .
 
No. 86
---------------
. . . Q
. Q . .
.Q. . .
. .Q. .
. . . .Q
Q. . . .
. Q . .
. . Q .
 
No. 87
---------------
. . . Q
. Q . .
.Q. . .
. . . Q
. . .Q.
Q. . . .
. Q . .
. .Q. .
 
No. 88
---------------
. . . Q
. .Q. .
. Q . .
Q. . . .
. . .Q.
. . . Q
.Q. . .
. Q . .
 
No. 89
---------------
. . . .Q
Q . . .
. .Q. .
Q. . . .
. . . Q
. .Q. .
. Q . .
. . Q .
 
No. 90
---------------
. . . .Q
Q . . .
. . Q .
.Q. . .
Q . . .
. . .Q.
. .Q. .
. . Q .
 
No. 91
---------------
. . . .Q
.Q. . .
Q . . .
. . Q .
.Q. . .
. .Q. .
. . . Q
. Q . .
 
No. 92
---------------
. . . .Q
. Q . .
Q . . .
.Q. . .
. . .Q.
Q . . .
. . . Q
. .Q. .</pre>
 
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="mumps">Queens New count,flip,row,sol
Set sol=0
For row(1)=1:1:4 Do try(2) ; Not 8, the other 4 are symmetric...
Line 5,190 ⟶ 10,433:
Quit
Do Queens
</syntaxhighlight>
</lang>
<div style="overflow:scroll; height:400px;">
<syntaxhighlight lang="mumps">
<lang MUMPS>
+--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+
8 | |##| Q|##| |##| |##| | |##| Q|##| |##| |##| | |##| | Q| |##| |##|
Line 5,400 ⟶ 10,643:
1 |##| |##| | Q| |##| |
+--+--+--+--+--+--+--+--+
A B C D E F G H</langsyntaxhighlight></div>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">const boardSizeBoardSize = 8
 
proc underAttack(col,: int; queens: seq[int]): bool =
if col in queens: return true
for i, x in queens:
Line 5,412 ⟶ 10,655:
return false
 
proc solve(n: int): autoseq[seq[int]] =
result = newSeq[seq[int]]()
result.add(@[])
Line 5,418 ⟶ 10,661:
for row in 1..n:
for solution in result:
for i in 1..boardSizeBoardSize:
if not underAttack(i, solution):
newSolutions.add(solution & i)
Line 5,424 ⟶ 10,667:
newSolutions.setLen(0)
 
echo "Solutions for a chessboard of size ", BoardSize, 'x', BoardSize
for answer in solve(boardSize):
echo ""
for i, x in answer:
 
if i > 0: stdout.write ", "
for i, answer in solve(BoardSize):
stdout.write "(",i,", ",x,")"</lang>
for row, col in answer:
if row > 0: stdout.write ' '
stdout.write chr(ord('a') + row), col
stdout.write if i mod 4 == 3: "\n" else: " "</syntaxhighlight>
 
{{out}}
<pre>Solutions for a chessboard of size 8x8
 
a1 b5 c8 d6 e3 f7 g2 h4 a1 b6 c8 d3 e7 f4 g2 h5 a1 b7 c4 d6 e8 f2 g5 h3 a1 b7 c5 d8 e2 f4 g6 h3
a2 b4 c6 d8 e3 f1 g7 h5 a2 b5 c7 d1 e3 f8 g6 h4 a2 b5 c7 d4 e1 f8 g6 h3 a2 b6 c1 d7 e4 f8 g3 h5
a2 b6 c8 d3 e1 f4 g7 h5 a2 b7 c3 d6 e8 f5 g1 h4 a2 b7 c5 d8 e1 f4 g6 h3 a2 b8 c6 d1 e3 f5 g7 h4
a3 b1 c7 d5 e8 f2 g4 h6 a3 b5 c2 d8 e1 f7 g4 h6 a3 b5 c2 d8 e6 f4 g7 h1 a3 b5 c7 d1 e4 f2 g8 h6
a3 b5 c8 d4 e1 f7 g2 h6 a3 b6 c2 d5 e8 f1 g7 h4 a3 b6 c2 d7 e1 f4 g8 h5 a3 b6 c2 d7 e5 f1 g8 h4
a3 b6 c4 d1 e8 f5 g7 h2 a3 b6 c4 d2 e8 f5 g7 h1 a3 b6 c8 d1 e4 f7 g5 h2 a3 b6 c8 d1 e5 f7 g2 h4
a3 b6 c8 d2 e4 f1 g7 h5 a3 b7 c2 d8 e5 f1 g4 h6 a3 b7 c2 d8 e6 f4 g1 h5 a3 b8 c4 d7 e1 f6 g2 h5
a4 b1 c5 d8 e2 f7 g3 h6 a4 b1 c5 d8 e6 f3 g7 h2 a4 b2 c5 d8 e6 f1 g3 h7 a4 b2 c7 d3 e6 f8 g1 h5
a4 b2 c7 d3 e6 f8 g5 h1 a4 b2 c7 d5 e1 f8 g6 h3 a4 b2 c8 d5 e7 f1 g3 h6 a4 b2 c8 d6 e1 f3 g5 h7
a4 b6 c1 d5 e2 f8 g3 h7 a4 b6 c8 d2 e7 f1 g3 h5 a4 b6 c8 d3 e1 f7 g5 h2 a4 b7 c1 d8 e5 f2 g6 h3
a4 b7 c3 d8 e2 f5 g1 h6 a4 b7 c5 d2 e6 f1 g3 h8 a4 b7 c5 d3 e1 f6 g8 h2 a4 b8 c1 d3 e6 f2 g7 h5
a4 b8 c1 d5 e7 f2 g6 h3 a4 b8 c5 d3 e1 f7 g2 h6 a5 b1 c4 d6 e8 f2 g7 h3 a5 b1 c8 d4 e2 f7 g3 h6
a5 b1 c8 d6 e3 f7 g2 h4 a5 b2 c4 d6 e8 f3 g1 h7 a5 b2 c4 d7 e3 f8 g6 h1 a5 b2 c6 d1 e7 f4 g8 h3
a5 b2 c8 d1 e4 f7 g3 h6 a5 b3 c1 d6 e8 f2 g4 h7 a5 b3 c1 d7 e2 f8 g6 h4 a5 b3 c8 d4 e7 f1 g6 h2
a5 b7 c1 d3 e8 f6 g4 h2 a5 b7 c1 d4 e2 f8 g6 h3 a5 b7 c2 d4 e8 f1 g3 h6 a5 b7 c2 d6 e3 f1 g4 h8
a5 b7 c2 d6 e3 f1 g8 h4 a5 b7 c4 d1 e3 f8 g6 h2 a5 b8 c4 d1 e3 f6 g2 h7 a5 b8 c4 d1 e7 f2 g6 h3
a6 b1 c5 d2 e8 f3 g7 h4 a6 b2 c7 d1 e3 f5 g8 h4 a6 b2 c7 d1 e4 f8 g5 h3 a6 b3 c1 d7 e5 f8 g2 h4
a6 b3 c1 d8 e4 f2 g7 h5 a6 b3 c1 d8 e5 f2 g4 h7 a6 b3 c5 d7 e1 f4 g2 h8 a6 b3 c5 d8 e1 f4 g2 h7
a6 b3 c7 d2 e4 f8 g1 h5 a6 b3 c7 d2 e8 f5 g1 h4 a6 b3 c7 d4 e1 f8 g2 h5 a6 b4 c1 d5 e8 f2 g7 h3
a6 b4 c2 d8 e5 f7 g1 h3 a6 b4 c7 d1 e3 f5 g2 h8 a6 b4 c7 d1 e8 f2 g5 h3 a6 b8 c2 d4 e1 f7 g5 h3
a7 b1 c3 d8 e6 f4 g2 h5 a7 b2 c4 d1 e8 f5 g3 h6 a7 b2 c6 d3 e1 f4 g8 h5 a7 b3 c1 d6 e8 f5 g2 h4
a7 b3 c8 d2 e5 f1 g6 h4 a7 b4 c2 d5 e8 f1 g3 h6 a7 b4 c2 d8 e6 f1 g3 h5 a7 b5 c3 d1 e6 f8 g2 h4
a8 b2 c4 d1 e7 f5 g3 h6 a8 b2 c5 d3 e1 f7 g4 h6 a8 b3 c1 d6 e2 f5 g7 h4 a8 b4 c1 d3 e6 f2 g7 h5</pre>
 
=={{header|Objeck}}==
{{trans|Java}}
 
<langsyntaxhighlight lang="objeck">bundle Default {
class NQueens {
b : static : Int[];
Line 5,489 ⟶ 10,763:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
{{libheader|FaCiLe}}
 
<langsyntaxhighlight lang="ocaml">(* Authors: Nicolas Barnier, Pascal Brisset
Copyright 2004 CENA. All rights reserved.
This code is distributed under the terms of the GNU LGPL *)
Line 5,552 ⟶ 10,826:
then raise (Failure "Usage: queens <nb of queens>");
Gc.set ({(Gc.get ()) with Gc.space_overhead = 500}); (* May help except with an underRAMed system *)
queens (int_of_string Sys.argv.(1));;</langsyntaxhighlight>
===A stand-alone OCaml solution===
<langsyntaxhighlight lang="ocaml">let solutions n =
 
let show board =
Line 5,585 ⟶ 10,859:
else 8 in
 
solutions n</langsyntaxhighlight>
{{out}}
<pre>$ ocaml queens.ml 6
Line 5,619 ⟶ 10,893:
=={{header|Oz}}==
A pretty naive solution, using constraint programming:
<langsyntaxhighlight lang="oz">declare
fun {Queens N}
proc {$ Board}
Line 5,686 ⟶ 10,960:
in
{Length Solutions} = 92 %% assert
{Inspect {List.take Solutions 3}}</langsyntaxhighlight>
 
There is a more concise and much more efficient [http://www.mozart-oz.org/documentation/fdt/node25.html#section.scripts.queens solution] in the Mozart documentation.
 
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program queens;
 
const l=16;
Line 5,772 ⟶ 11,045:
14 365596
15 2279184
16 14772512 }</langsyntaxhighlight>
 
===Alternative===
Line 5,790 ⟶ 11,063:
Solution found</pre>
<langsyntaxhighlight lang="pascal">program NQueens;
{$IFDEF FPC}
{$MODE DELPHI}
Line 5,901 ⟶ 11,174:
end;
WriteLn('Fertig');
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 5,923 ⟶ 11,196:
17 95815104 03:00.950 secs=180.98 secs
Fertig}</pre>
 
=={{header|PDP-11 Assembly}}==
<syntaxhighlight lang="pdp-11 assembly">
; "eight queens problem" benchmark test
 
.radix 16
 
.loc 0
 
nop ;
mov #scr,@#E800
mov #88C6,@#E802
; clear the display RAM
mov #scr,r0
mov #1E0,r1
cls: clr (r0)+
sob r1,cls
; display the initial counter value
clr r3
mov #scr,r0
jsr pc,number
; perform the test
jsr pc,queens
; display the counter
mov #scr,r0
jsr pc,number
finish: br finish
 
; display the character R1 at the screen address R0,
; advance the pointer R0 to the next column
putc: mov r2,-(sp)
; R1 <- 6 * R1
asl r1 ;* 2
mov r1,-(sp)
asl r1 ;* 4
add (sp)+,r1 ;* 6
add #chars,r1
mov #6,r2
putc1: movb (r1)+,(r0)
add #1E,r0
sob r2,putc1
sub #B2,r0 ;6 * 1E - 2 = B2
mov (sp)+,r2
rts pc
 
print1: jsr pc,putc
; print a string pointed to by R2 at the screen address R0,
; advance the pointer R0 to the next column,
; the string should be terminated by a negative byte
print: movb (r2)+,r1
bpl print1
rts pc
 
; display the word R3 decimal at the screen address R0
number: mov sp,r1
mov #A0A,-(sp)
mov (sp),-(sp)
mov (sp),-(sp)
movb #80,-(r1)
numb1: clr r2
div #A,r2
movb r3,-(r1)
mov r2,r3
bne numb1
mov sp,r2
jsr pc,print
add #6,sp
rts pc
 
queens: mov #64,r5 ;100
l06: clr r3
clr r0
l00: cmp #8,r0
beq l05
inc r0
movb #8,ary(r0)
l01: inc r3
mov r0,r1
l02: dec r1
beq l00
movb ary(r0),r2
movb ary(r1),r4
sub r2,r4
beq l04
bcc l03
neg r4
l03: add r1,r4
sub r0,r4
bne l02
l04: decb ary(r0)
bne l01
sob r0,l04
l05: sob r5,l06
mov r3,cnt
rts pc
 
; characters, width = 8 pixels, height = 6 pixels
chars: .byte 3C, 46, 4A, 52, 62, 3C ;digit '0'
.byte 18, 28, 8, 8, 8, 3E ;digit '1'
.byte 3C, 42, 2, 3C, 40, 7E ;digit '2'
.byte 3C, 42, C, 2, 42, 3C ;digit '3'
.byte 8, 18, 28, 48, 7E, 8 ;digit '4'
.byte 7E, 40, 7C, 2, 42, 3C ;digit '5'
.byte 3C, 40, 7C, 42, 42, 3C ;digit '6'
.byte 7E, 2, 4, 8, 10, 10 ;digit '7'
.byte 3C, 42, 3C, 42, 42, 3C ;digit '8'
.byte 3C, 42, 42, 3E, 2, 3C ;digit '9'
.byte 0, 0, 0, 0, 0, 0 ;space
 
.even
 
cnt: .blkw 1
ary: .blkb 9
 
.loc 200
 
scr: ;display RAM
</syntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">my ($board_size, @occupied, @past, @solutions);
 
sub try_column {
Line 5,960 ⟶ 11,351:
}
 
$board_size = 12; # takes a minute or so, 14,200 solutions
try_column(0);
 
#print for @solutions; # un-comment to see all solutions
local $" = "\n";
print "total " . @solutions . " solutions\n";</syntaxhighlight>
{{out}}
print "total ", scalar(@solutions), " solutions\n";</lang>
<pre>total 14200 solutions</pre>
 
=={{header|Perl 6Phix}}==
<!--(phixonline)-->
{{works with|rakudo|2015-11-29}}
<syntaxhighlight lang="phix">
Neither pretty nor efficient, a simple backtracking solution
with javascript_semantics
 
--
<lang perl6>sub MAIN(\N = 8) {
-- demo\rosetta\n_queens.exw
sub collision(@field, $row) {
-- =========================
for ^$row -> $i {
--
my $distance = @field[$i] - @field[$row];
sequence co, -- columns occupied
return True if $distance == any(0, $row - $i, $i - $row);
} -- (ro is implicit)
False; fd, -- forward diagonals
bd, -- backward diagonals
}
board
sub search(@field, $row) {
return @field if $row == N;
atom count
for ^N -> $i {
@field[$row] = $i;
procedure solve(integer row, integer N, integer show)
return search(@field, $row + 1) || next
for col=1 to N do
unless collision(@field, $row);
}if not co[col] then
() integer fdi = col+row-1,
bdi = row-col+N
}
for 0 .. N / 2 { if not fd[fdi]
if search [$_], 1 ->and @fnot bd[bdi] {then
say @f; board[row][col] = 'Q'
last; co[col] = true
} fd[fdi] = true
bd[bdi] = true
}
if row=N then
}</lang>
if show then
puts(1,join(board,"\n")&"\n")
puts(1,repeat('=',N)&"\n")
end if
count += 1
else
solve(row+1,N,show)
end if
board[row][col] = '.'
co[col] = false
fd[fdi] = false
bd[bdi] = false
end if
end if
end for
end procedure
procedure n_queens(integer N=8, integer show=1)
co = repeat(false,N)
fd = repeat(false,N*2-1)
bd = repeat(false,N*2-1)
board = repeat(repeat('.',N),N)
count = 0
solve(1,N,show)
printf(1,"%d queens: %d solutions\n",{N,count})
end procedure
for N=1 to iff(platform()=JS?12:14) do
n_queens(N,N<5)
end for
</syntaxhighlight>
{{out}}
<pre>
<pre>[0 4 7 5 2 6 1 3]</pre>
Q
=
1 queens: 1 solutions
2 queens: 0 solutions
3 queens: 0 solutions
.Q..
...Q
Q...
..Q.
====
..Q.
Q...
...Q
.Q..
====
4 queens: 2 solutions
5 queens: 10 solutions
6 queens: 4 solutions
7 queens: 40 solutions
8 queens: 92 solutions
9 queens: 352 solutions
10 queens: 724 solutions
11 queens: 2680 solutions
12 queens: 14200 solutions
13 queens: 73712 solutions
14 queens: 365596 solutions
</pre>
N=14 takes about 10s on the desktop but 45s under p2js, so the limit of 12 for that allows it to finish in under 2s
 
=={{header|PHP}}==
<syntaxhighlight lang="php">
 
Probably not a great solution given this is one of my first forays into PHP.
First solves the n rooks problem and then finds solutions for n-queens,
disregarding any rotations/reflections. Checked up to n=10.
 
<lang PHP>
<html>
<head>
Line 6,014 ⟶ 11,460:
<?php
echo "<h1>n x n Queen solving program</h1>";
 
//Get the size of the board
$boardX = $_POST['boardX'];
$boardY = $_POST['boardX'];
 
// Function to rotate a board 90 degrees
function rotateBoard($p, $boardX) {
$a=0;
while ($a < count($p)) {
$b = strlen(decbin($p[$a]))-1;
$tmp[$b] = 1 << ($boardX - $a - 1);
++$a;
}
}
ksort($tmp);
return $tmp;
}
 
// This function will find rotations of a solution
function findRotation($p, $boardX,$solutions){
$tmp = rotateBoard($p,$boardX);
// Rotated 90
if (in_array($tmp,$solutions)) {}
else {$solutions[] = $tmp;}
 
$tmp = rotateBoard($tmp,$boardX);
// Rotated 180
if (in_array($tmp,$solutions)){}
else {$solutions[] = $tmp;}
 
$tmp = rotateBoard($tmp,$boardX);
// Rotated 270
if (in_array($tmp,$solutions)){}
else {$solutions[] = $tmp;}
 
// Reflected
$tmp = array_reverse($p);
if (in_array($tmp,$solutions)){}
else {$solutions[] = $tmp;}
 
$tmp = rotateBoard($tmp,$boardX);
// Reflected and Rotated 90
if (in_array($tmp,$solutions)){}
else {$solutions[] = $tmp;}
 
$tmp = rotateBoard($tmp,$boardX);
// Reflected and Rotated 180
if (in_array($tmp,$solutions)){}
else {$solutions[] = $tmp;}
 
$tmp = rotateBoard($tmp,$boardX);
// Reflected and Rotated 270
if (in_array($tmp,$solutions)){}
else {$solutions[] = $tmp;}
return $solutions;
}
 
// This is a function which will render the board
function renderBoard($p,$boardX) {
Line 6,079 ⟶ 11,525:
if (($x+$y) & 1) { $cellCol = '#9C661F';}
else {$cellCol = '#FCE6C9';}
if ($p[$y] == 1 << $x) { echo "<td bgcolor=".$cellCol."><img width=30 height=30 src='".$img."'></td>";}
else { echo "<td bgcolor=".$cellCol."> </td>";}
Line 6,086 ⟶ 11,532:
}
echo '<tr></tr></table>&nbsp';
 
}
 
//This function allows me to generate the next order of rows.
function pc_next_permutation($p) {
$size = count($p) - 1;
// slide down the array looking for where we're smaller than the next guy
 
for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { }
 
// if this doesn't occur, we've finished our permutations
// the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
if ($i == -1) { return false; }
 
// slide down the array looking for a bigger number than what we found before
for ($j = $size; $p[$j] <= $p[$i]; --$j) { }
// swap them
$tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
// now reverse the elements in between by swapping the ends
for (++$i, $j = $size; $i < $j; ++$i, --$j)
{ $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp; }
return $p;
}
 
//This function needs to check the current state to see if there are any
function checkBoard($p,$boardX) {
Line 6,116 ⟶ 11,562:
$b = 1;
while ($b < ($boardX - $a)){
$x = $p[$a+$b] << $b;
$y = $p[$a+$b] >> $b;
if ($p[$a] == $x | $p[$a] == $y) { return false;}
return false;
++$b;
}
++$b;
}
++$a;
Line 6,125 ⟶ 11,573:
return true;
}
 
 
if (isset($_POST['process']) && isset($_POST['boardX']))
{
//Within here is the code that needs to be run if process is clicked.
 
 
//First I need to create the different possible rows
for ($x = 0; $x < $boardX; ++$x){
$row[$x] = 1 << $x;
}
 
//Now I need to create all the possible orders of rows, will be equal to [boardY]!
$solcount = 0;
Line 6,148 ⟶ 11,596:
++$solcount;
}
}
$row = pc_next_permutation($row);
}
}
echo "<br><br>&nbsp&nbsp&nbsp&nbspRows/Columns: ".$boardX."<br>&nbsp&nbsp&nbsp&nbspUnique Solutions: ".$solcount."<br>&nbsp&nbsp&nbsp&nbspTotal Solutions: ".count($solutions)." - Note: This includes symmetrical solutions<br>";
//print_r($solutions);
}
 
//This code collects the starting parameters
echo <<<_END
<form name="input" action="queensindex.php" method="post">
&nbsp&nbsp&nbsp&nbspNumber of columns/rows <select name="boardX" />
<option value="1">One</option>
Line 6,175 ⟶ 11,622:
&nbsp<input type="submit" value="Process" />
</form>
 
_END;
?>
</body>
</html>
</syntaxhighlight>
 
<h2>Solution with recursion</h2>
 
<syntaxhighlight lang="php">
<html>
<body>
<pre>
<?php
 
/*************************************************************************
*
* This algorithm solves the 8 queens problem using backtracking.
* Please try with N<=25 * * * *************************************************************************/
class Queens {
var $size;
var $arr;
var $sol;
 
function Queens($n = 8) {
$this->size = $n;
$this->arr = array();
$this->sol = 0;
// Inicialiate array;
for($i=0; $i<$n; $i++) {
$this->arr[$i] = 0;
}
}
 
function isSolution($n) {
for ($i = 0; $i < $n; $i++) {
if ($this->arr[$i] == $this->arr[$n] ||
($this->arr[$i] - $this->arr[$n]) == ($n - $i) ||
($this->arr[$n] - $this->arr[$i]) == ($n - $i))
{
return false;
}
}
return true;
}
 
function PrintQueens() {
echo("solution #".(++$this->sol)."\n");
// echo("solution #".($this->size)."\n");
for ($i = 0; $i < $this->size; $i++) {
for ($j = 0; $j < $this->size; $j++) {
if ($this->arr[$i] == $j) echo("& ");
else echo(". ");
}
echo("\n");
}
echo("\n");
}
 
 
// backtracking Algorithm
function run($n = 0) {
if ($n == $this->size){
$this->PrintQueens();
}
else {
for ($i = 0; $i < $this->size; $i++) {
$this->arr[$n] = $i;
if($this->isSolution($n)){
$this->run($n+1);
}
}
}
}
}
 
$myprogram = new Queens(8);
$myprogram->run();
 
?>
</pre>
</body>
</html></lang>
</syntaxhighlight>
 
=={{header|Picat}}==
===0/1 encoding a N x N matrix===
Using constraint modelling using an 0/1 encoding of an N x N matrix. It is the probably the fastest approach when using SAT and MIP solvers.
<syntaxhighlight lang="picat">import sat.
% import mip.
 
queens_sat(N,Q) =>
Q = new_array(N,N),
Q :: 0..1,
foreach (K in 1-N..N-1)
sum([Q[I,J] : I in 1..N, J in 1..N, I-J==K]) #=< 1
end,
 
foreach (K in 2..2*N)
sum([Q[I,J] : I in 1..N, J in 1..N, I+J==K]) #=< 1
end,
 
foreach (I in 1..N)
sum([Q[I,J] : J in 1..N]) #= 1
end,
 
foreach (J in 1..N)
sum([Q[I,J] : I in 1..N]) #= 1
end,
solve([inout],Q).</syntaxhighlight>
 
===Constraint programming===
This is the "standard" model using constraint programming (in contract to the SAT 0/1 approach). Instead of an NxN matrix, this encoding uses a single list representing the columns. The three <code>all_different/1</code> then ensures that the rows, and the two diagonals are distinct.
<syntaxhighlight lang="picat">import cp.
 
queens(N, Q) =>
Q = new_list(N),
Q :: 1..N,
all_different(Q),
all_different([$Q[I]-I : I in 1..N]),
all_different([$Q[I]+I : I in 1..N]),
solve([ff],Q).</syntaxhighlight>
 
==="Naive" approach===
This approach might be called "naive" (in the constraint programming context) since it doesn't use the (general) faster <code>all_different/1</code> constraint.
<syntaxhighlight lang="picat">queens_naive(N, Q) =>
Q = new_list(N),
Q :: 1..N,
foreach(I in 1..N, J in 1..N, I < J)
Q[I] #!= Q[J],
Q[I] + I #!= Q[J] + J,
Q[I] - I #!= Q[J] - J
end,
solve([ff], Q).</syntaxhighlight>
 
 
===Test===
 
{{out}}
<pre>Picat> queens_cp(100, QCP)
QCP = [1,3,5,57,59,4,64,7,58,71,81,60,6,91,82,90,8,83,77,65,73,26,9,45,37,63,66,62,44,10,48,54,43,69,42,47,18,11,72,68,50,56,61,36,33,17,12,51,100,93,97,88,35,84,78,19,13,99,67,76,92,75,87,96,94,85,20,14,95,32,98,55,40,80,49,52,46,53,21,15,41,2,27,34,22,70,74,29,25,30,38,86,16,79,24,39,28,23,31,89]
 
Picat> queens_sat(10,Q)
Q = {{0,0,0,0,0,0,0,0,1,0},{0,1,0,0,0,0,0,0,0,0},{0,0,0,0,1,0,0,0,0,0},{0,0,1,0,0,0,0,0,0,0},{1,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,1,0,0},{0,0,0,0,0,1,0,0,0,0},{0,0,0,1,0,0,0,0,0,0},{0,0,0,0,0,0,1,0,0,0}}</pre>
 
===Number of solutions for N = 1..15===
<pre>Picat> foreach(N in 1..15) println(N=count_all(queens_cp(N,_))) end
1 = 1
2 = 0
3 = 0
4 = 2
5 = 10
6 = 4
7 = 40
8 = 92
9 = 352
10 = 724
11 = 2680
12 = 14200
13 = 73712
14 = 365596
15 = 2279184</pre>
 
===Comparison===
Running these models in Picat shows that the CP encoding with the cp solver is the fastest for most instances.
 
For example, for N=1000, the cp solver takes about 1.5s to find a solution, whereas the SAT solver takes 571s. Both the mip and smt solvers are in general much slower. However the SAT 0/1 encoding+solver might be faster for larger instances, say >= N=2000.
 
The conclusion to draw of this is that it is often useful to test different encodings and different constraints solvers.
 
That being said, the N-queens problem is not interesting (hard) enough to draw any conclusive conclusion about the performances of the solvers. More complex problem are needed for that.
 
=={{header|PicoLisp}}==
===Calling 'permute'===
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/simul.l") # for 'permute'
 
(de queens (N)
Line 6,194 ⟶ 11,808:
(length (uniq (mapcar - L R))) )
(inc 'Cnt) ) )
Cnt ) )</langsyntaxhighlight>
===Permuting inline===
This alternative version does not first pre-generate all permutations with
'permute', but creates them recursively. Also, it directly checks for
duplicates, instead of calling 'uniq' and 'length'. This is much faster.
<langsyntaxhighlight PicoLisplang="picolisp">(de queens (N)
(let (R (range 1 N) L (copy R) X L Cnt 0)
(recur (X) # Permute
Line 6,214 ⟶ 11,828:
(mapcar - L R) )
(inc 'Cnt) ) ) )
Cnt ) )</langsyntaxhighlight>
{{out}} for both cases:
<pre>: (queens 8)
-> 92</pre>
 
=={{header|PowerBASIC}}==
<lang powerbasic> defint a-z
option base 1
input "n=",n
dim a(n), s(n), u(4*n-2)
for i=1 to n: a(i)=i: next
for i=1 to 4*n-2: u(i)=0: next
m=0
i=1
r=2*n-1
goto 20
10 s(i)=j
u(p)=1
u(q+r)=1
incr i
20 if i>n goto 60
j=i
30 z=a(i)
y=a(j)
p=i-y+n
q=i+y-1
a(i)=y
a(j)=z
if u(p)=0 and u(q+r)=0 goto 10
40 incr j
if j<=n goto 30
50 decr j
if j=i goto 70
swap a(i),a(j)
goto 50
60 incr m
for k=1 to n: print a(k);: next: print
70 decr i
if i=0 goto 80
p=i-a(i)+n
q=i+a(i)-1
j=s(i)
u(p)=0
u(q+r)=0
goto 40
80 print m</lang>
 
 
=={{header|PL/I}}==
This code compiles with PL/I compilers ranging from the ancient IBM MVT PL/I F compiler of the 1960s, the IBM PL/I Optimizing compiler, thru the IBM PL/I compiler for MVS and VM, to the z/OS Enterprise PL/I v4.60 compiler; effectively spanning 50 years of PL/I compilers. It only outputs the number of solutions found for a given N instead of printing out each individual chess board solution to avoid filling up spool space for large values of N. It's trivial to add a print-out of the individual solutions.
<syntaxhighlight lang="pli">
<lang PL/I>
NQUEENS: PROC OPTIONS (MAIN);
DCL A(35) BIN FIXED(31) EXTERNAL;
Line 6,342 ⟶ 11,913:
END QUEEN;
END NQUEENS; </langsyntaxhighlight>
 
=={{header|PowerBASIC}}==
=== Recursive version ===
{{trans|Stata}}
<syntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
 
SUB aux(n AS INTEGER, i AS INTEGER, a() AS INTEGER, _
u() AS INTEGER, v() AS INTEGER, m AS QUAD)
 
LOCAL j, k, p, q AS INTEGER
IF i > n THEN
INCR m
FOR k = 1 TO n : PRINT a(k); : NEXT : PRINT
ELSE
FOR j = i TO n
k = a(j)
p = i - k + n
q = i + k - 1
IF u(p) AND v(q) THEN
u(p) = 0 : v(q) = 0
a(j) = a(i) : a(i) = k
CALL aux(n, i + 1, a(), u(), v(), m)
u(p) = 1 : v(q) = 1
a(i) = a(j) : a(j) = k
END IF
NEXT
END IF
END SUB
 
FUNCTION PBMAIN () AS LONG
LOCAL n, i AS INTEGER
LOCAL m AS QUAD
IF COMMAND$(1) <> "" THEN
n = VAL(COMMAND$(1))
REDIM a(1 TO n) AS INTEGER
REDIM u(1 TO 2 * n - 1) AS INTEGER
REDIM v(1 TO 2 * n - 1) AS INTEGER
FOR i = 1 TO n
a(i) = i
NEXT
FOR i = 1 TO 2 * n - 1
u(i) = 1
v(i) = 1
NEXT
m = 0
CALL aux(n, 1, a(), u(), v(), m)
PRINT m
END IF
END FUNCTION</syntaxhighlight>
 
=== Iterative version ===
{{trans|Stata}}
<syntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
 
FUNCTION PBMAIN () AS LONG
LOCAL n, i, j, k, p, q AS INTEGER
LOCAL m AS QUAD
IF COMMAND$(1) <> "" THEN
n = VAL(COMMAND$(1))
REDIM a(1 TO n) AS INTEGER
REDIM s(1 TO n) AS INTEGER
REDIM u(1 TO 2 * n - 1) AS INTEGER
REDIM v(1 TO 2 * n - 1) AS INTEGER
FOR i = 1 TO n
a(i) = i
NEXT
FOR i = 1 TO 2 * n - 1
u(i) = 1
v(i) = 1
NEXT
m = 0
i = 1
1 IF i > n THEN
INCR m
FOR k = 1 TO n : PRINT a(k); : NEXT : PRINT
GOTO 4
END IF
j = i
2 k = a(j)
p = i - k + n
q = i + k - 1
IF u(p) AND v(q) THEN
u(p) = 0 : v(q) = 0
a(j) = a(i) : a(i) = k
s(i) = j
INCR i
GOTO 1
END IF
3 INCR j : IF j <= n GOTO 2
4 DECR i : IF i = 0 THEN PRINT m : EXIT FUNCTION
j = s(i)
k = a(i) : a(i) = a(j) : a(j) = k
p = i - k + n
q = i + k - 1
u(p) = 1 : v(q) = 1
GOTO 3
END IF
END FUNCTION</syntaxhighlight>
 
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang="powershell">
function PlaceQueen ( [ref]$Board, $Row, $N )
{
# For the current row, start with the first column
$Board.Value[$Row] = 0
 
# While haven't exhausted all columns in the current row...
While ( $Board.Value[$Row] -lt $N )
{
# If not the first row, check for conflicts
$Conflict = $Row -and
( (0..($Row-1)).Where{ $Board.Value[$_] -eq $Board.Value[$Row] }.Count -or
(0..($Row-1)).Where{ $Board.Value[$_] -eq $Board.Value[$Row] - $Row + $_ }.Count -or
(0..($Row-1)).Where{ $Board.Value[$_] -eq $Board.Value[$Row] + $Row - $_ }.Count )
# If no conflicts and the current column is a valid column...
If ( -not $Conflict -and $Board.Value[$Row] -lt $N )
{
 
# If this is the last row
# Board completed successfully
If ( $Row -eq ( $N - 1 ) )
{
return $True
}
 
# Recurse
# If all nested recursions were successful
# Board completed successfully
If ( PlaceQueen $Board ( $Row + 1 ) $N )
{
return $True
}
}
# Try the next column
$Board.Value[$Row]++
}
 
# Everything was tried, nothing worked
Return $False
}
function Get-NQueensBoard ( $N )
{
# Start with a default board (array of column positions for each row)
$Board = @( 0 ) * $N
 
# Place queens on board
# If successful...
If ( PlaceQueen -Board ([ref]$Board) -Row 0 -N $N )
{
# Convert board to strings for display
$Board | ForEach { ( @( "" ) + @(" ") * $_ + "Q" + @(" ") * ( $N - $_ ) ) -join "|" }
}
Else
{
"There is no solution for N = $N"
}
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
Get-NQueensBoard 8
''
Get-NQueensBoard 3
''
Get-NQueensBoard 4
''
Get-NQueensBoard 14
</syntaxhighlight>
{{out}}
<pre>
|Q| | | | | | | |
| | | | |Q| | | |
| | | | | | | |Q|
| | | | | |Q| | |
| | |Q| | | | | |
| | | | | | |Q| |
| |Q| | | | | | |
| | | |Q| | | | |
 
There is no solution for N = 3
 
| |Q| | |
| | | |Q|
|Q| | | |
| | |Q| |
 
|Q| | | | | | | | | | | | | |
| | |Q| | | | | | | | | | | |
| | | | |Q| | | | | | | | | |
| | | | | | |Q| | | | | | | |
| | | | | | | | | | | |Q| | |
| | | | | | | | | |Q| | | | |
| | | | | | | | | | | | |Q| |
| | | |Q| | | | | | | | | | |
| | | | | | | | | | | | | |Q|
| | | | | | | | |Q| | | | | |
| |Q| | | | | | | | | | | | |
| | | | | |Q| | | | | | | | |
| | | | | | | |Q| | | | | | |
| | | | | | | | | | |Q| | | |
</pre>
 
=={{header|Processing}}==
{{trans|Java}}
<syntaxhighlight lang="java">
int n = 8;
int[] b = new int[n];
int s = 0;
int y = 0;
 
void setup() {
size(400, 400);
textAlign(CENTER, CENTER);
textFont(createFont("DejaVu Sans", 44));
b[0] = -1;
}
 
void draw() {
if (y >= 0) {
do {
b[y]++;
} while ((b[y] < n) && unsafe(y));
if (b[y] < n) {
if (y < (n-1)) {
b[++y] = -1;
} else {
drawBoard();
}
} else {
y--;
}
} else {
textSize(18);
text("Press any key to restart", width / 2, height - 20);
}
}
 
 
boolean unsafe(int y) {
int x = b[y];
for (int i = 1; i <= y; i++) {
int t = b[y - i];
if (t == x ||
t == x - i ||
t == x + i) {
return true;
}
}
return false;
}
 
void drawBoard() {
float w = width / n;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
fill(255 * ((x + y) % 2));
square(x * w, y * w, w);
if (b[y] == x) {
fill(255 - 255 * ((x + y) % 2));
textSize(42);
text("♕", w / 2 + x *w, w /2 + y * w);
}
}
}
fill(255, 0, 0);
textSize(18);
text("Solution " + (++s), width / 2, height / 90);
}
 
void keyPressed() {
b = new int[n];
s = 0;
y = 0;
b[0] = -1;
}
</syntaxhighlight>
 
==={{header|Processing Python mode}}===
{{trans|Python}}
 
This solution, originally by Raymond Hettinger for demonstrating the power of the itertools module, generates all solutions.
 
<syntaxhighlight lang="python">from itertools import permutations, product
 
n = 8
cols = range(n)
i = 0 # solution shown
 
solutions = [vec for vec in permutations(cols)
if n == len(set(vec[i] + i for i in cols))
== len(set(vec[i] - i for i in cols))]
 
def setup():
size(400, 400)
textAlign(CENTER, CENTER)
textFont(createFont("DejaVu Sans", 44))
 
def draw():
background(0)
w = width / n
for x, y in product(range(n), range(n)):
fill(255 * ((x + y) % 2))
square(x * w, y * w, w)
if solutions[i][y] == x:
fill(255 - 255 * ((x + y) % 2))
text(u'♕', w / 2 + x * w, w / 3 + y * w)
 
def keyPressed(): # show next solution
global i
i = (i + 1) % len(solutions)
</syntaxhighlight>
 
=={{header|Prolog}}==
Line 6,348 ⟶ 12,235:
 
Solution #1:
<langsyntaxhighlight Prologlang="prolog">solution([]).
solution([X/Y|Others]) :-
Line 6,368 ⟶ 12,255:
member(Item,Rest).
template([1/Y1,2/Y2,3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,8/Y8]).</langsyntaxhighlight>
 
Solution #2:
<langsyntaxhighlight Prologlang="prolog">solution(Queens) :-
permutation([1,2,3,4,5,6,7,8], Queens),
safe(Queens).
Line 6,398 ⟶ 12,285:
Y-Y1=\=Xdist,
Dist1 is Xdist + 1,
noattack(Y,Ylist,Dist1).</langsyntaxhighlight>
 
Solution #3:
<langsyntaxhighlight Prologlang="prolog">solution(Ylist) :-
sol(Ylist,[1,2,3,4,5,6,7,8],
[1,2,3,4,5,6,7,8],
Line 6,420 ⟶ 12,307:
del(Item,[First|List],[First|List1]) :-
del(Item,List,List1).</langsyntaxhighlight>
 
[http://ideone.com/Y6olN Output]:
Line 6,428 ⟶ 12,315:
===Alternative version===
Uses non-ISO predicates between/3 and select/3 (available in SWI Prolog and GNU Prolog).
<langsyntaxhighlight lang="prolog">:- initialization(main).
 
 
Line 6,443 ⟶ 12,330:
 
 
main :- findall(Qs, (queens(8,Qs), write(Qs), nl), _), halt.</langsyntaxhighlight>
[http://ideone.com/3bbIx0 Runs in: time: 0.02 memory: 68352]
 
Line 6,449 ⟶ 12,336:
Uses backtracking- a highly efficient mechanism in Prolog to find all solutions.
{{works with|SWI Prolog|version 6.2.6 by Jan Wielemaker, University of Amsterdam}}
<langsyntaxhighlight lang="prolog">% 8 queens problem.
% q(Row) represents a queen, allocated one per row. No rows ever clash.
% The columns are chosen iteratively from available columns held in a
Line 6,471 ⟶ 12,358:
length(Boards, Len), writef('%w solutions:\n', [Len]), % Output solutions
member(R,Boards), reverse(R,Board), writef(' - %w\n', [Board]), fail.
queens.</langsyntaxhighlight>
{{out}}
<pre>?- queens.
Line 6,484 ⟶ 12,371:
- [q(0,7),q(1,3),q(2,0),q(3,2),q(4,5),q(5,1),q(6,6),q(7,4)]
true.</pre>
 
 
===Short version===
SWI-Prolog 7.2.3
<syntaxhighlight lang="prolog">not_diagonal(X, N) :-
maplist(plus, X, N, Z1), maplist(plus, X, Z2, N), is_set(Z1), is_set(Z2).
 
queens(N, Qs) :-
numlist(1, N, P), findall(Q, (permutation(P, Q), not_diagonal(Q, P)), Qs).</syntaxhighlight>
{{out}}
<pre>
?- queens(8, X), length(X, L).
X = [[1, 5, 8, 6, 3, 7, 2, 4], [1, 6, 8, 3, 7, 4, 2|...], [1, 7, 4, 6, 8, 2|...], [1, 7, 5, 8, 2|...], [2, 4, 6, 8|...], [2, 5, 7|...], [2, 5|...], [2|...], [...|...]|...],
L = 92.
</pre>
 
===SWISH Prolog version===
<syntaxhighlight lang="prolog">
% John Devou: 26-Nov-2021
% Short solution to use on https://swish.swi-prolog.org/.
% Works fast for n ≤ 17.
 
:- use_rendering(chess).
 
q(_,0,[],[]).
q(N,R,[(R,C)|Qs],[C|Cs]):- R > 0, S is R-1, q(N,S,Qs,Cs), between(1,N,C),
not((member((U,V),Qs), (V =:= C; R-U =:= abs(C-V)))).
q(N,X):- q(N,N,_,X).
</syntaxhighlight>
 
===CLP(FD): Constraint Logic Programming over Finite Domains Version===
N-Queens - 92 Solutions In SWI-Prolog v8.0.2
 
This code solves the N-Queens problem in Prolog using the CLP(FD): Constraint Logic Programming over Finite Domain Library
<ul>
<li> `nqueens()` creates a 2D array datastructure, representing the board coordinates of each queen
<li> `applyConstraints()` recursively iterates through each queen on the board
<li> `checkConstraints()` applies the constraints: no two queens on same row/column/diagonal; and recurses through the list of remaining queens
<li> `optimizeQueens()` hardcodes each queen to live in a named row, this greatly reduces the computational complixity of the problem
<ul><li> Note: it is not possible to pass `Index + 1` into a prolog function, instead it must be declared and solved first as its own varaiable: `NextIndex is Index + 1`</ul>
<li> `print_board()` + print_line() render the ASCII graphics in a functional manner
<li> `all_nqueens()` uses `findall()` to solve `nqueens()` whilst repeatedly adding previous solutions as future constraints
<li> `print_nqueens_all(N)` is the main display/execution function
</ul>
 
<br/>
<ul>
<li>Source: https://github.com/JamesMcGuigan/ecosystem-research/blob/master/prolog/nqueens.prolog</li>
<li>Full Output: https://github.com/JamesMcGuigan/ecosystem-research/blob/master/prolog/nqueens.txt</li>
</ul>
<br/>
<syntaxhighlight lang="prolog">:- use_module(library(clpfd)).
 
% DOC: http://www.pathwayslms.com/swipltuts/clpfd/clpfd.html
length_(Length, List) :- length(List, Length).
 
applyConstraints([]).
applyConstraints([ Q | Queens ]) :-
checkConstraints(Q, Queens),
applyConstraints(Queens).
 
checkConstraints(_, []).
checkConstraints([Row0, Col0], [ [Row1, Col1] | Queens]) :-
Row0 #\= Row1, % No two queens on same row
Col0 #\= Col1, % No two queens on same columns
Row0 + Col0 #\= Row1 + Col1, % Down diagonals: [8,1], [7,2], [6,3]
Row0 - Col0 #\= Row1 - Col1, % Up diagonals: [1,1], [2,2], [3,3]
checkConstraints([Row0,Col0], Queens).
 
 
% Optimization: pre-assign each queen to a named row
optimizeQueens(Queens) :- optimizeQueens(Queens, 1).
optimizeQueens([],_).
optimizeQueens([[Row,_] | Queens], Index) :-
Row #= Index,
NextIndex is Index + 1,
optimizeQueens(Queens, NextIndex).
 
 
nqueens(N, Queens) :-
% Function Preconditions
N > 0,
 
% Create 2D Datastructure for Queens
length(Queens, N), maplist(length_(2), Queens),
flatten(Queens, QueenArray),
 
% Queens coords must be in range
QueenArray ins 1..N,
 
% Apply Constraints
optimizeQueens(Queens),
applyConstraints(Queens),
 
% Solve
label(QueenArray),
true.
 
 
all_nqueens(N) :- all_nqueens(N, _).
all_nqueens(N, Solutions) :-
findall(Queens, (nqueens(N,Queens), write(Queens), nl), Solutions),
length(Solutions,Count),
write(Count), write(' solutions'), nl,
Count #>= 1.
 
 
print_nqueens_all(N) :- all_nqueens(N, Solutions), print_nqueens(N, Solutions).
print_nqueens(N) :- nqueens(N, Queens), print_board(N, Queens).
print_nqueens(N, [Queens|Remaining]) :- print_count(Remaining), print_board(N, Queens), print_nqueens(N, Remaining).
print_nqueens(_, []).
 
print_count(Remaining) :- length(Remaining, Count), Count1 is Count + 1, nl, write('# '), write(Count1), nl.
print_board(N, [[_,Q] | Queens]) :- print_line(N, '-'), print_line(N, '|', Q), print_board(N, Queens).
print_board(N, []) :- print_line(N, '-').
print_line(0,'-') :- write('-'), nl.
print_line(N,'-') :- write('----'), N1 is N-1, print_line(N1,'-').
print_line(0,'|',_) :- write('|'), nl.
print_line(N,'|',Q) :- write('|'), (( Q == N ) -> write(' Q ') ; write(' ')), N1 is N-1, print_line(N1,'|',Q).
 
%:- initialization main.
main :-
print_nqueens_all(8).
</syntaxhighlight>
{{out}}
<pre>
92 solutions
 
# 92
---------------------------------
| | | | | | | | Q |
---------------------------------
| | | | Q | | | | |
---------------------------------
| Q | | | | | | | |
---------------------------------
| | | Q | | | | | |
---------------------------------
| | | | | | Q | | |
---------------------------------
| | Q | | | | | | |
---------------------------------
| | | | | | | Q | |
---------------------------------
| | | | | Q | | | |
---------------------------------
</pre>
 
=={{header|Pure}}==
From the Pure (programming language) Wikipedia page
 
<syntaxhighlight lang="pure">/*
n-queens.pure
Tectonics:
pure -c queens.pure -o queens
or
pure -q -i queens.pure
*/
using system;
 
queens n = search n 1 [] with
search n i p = [reverse p] if i>n;
= cat [search n (i+1) ((i,j):p) | j = 1..n; safe (i,j) p];
safe (i,j) p = ~any (check (i,j)) p;
check (i1,j1) (i2,j2)
= i1==i2 || j1==j2 || i1+j1==i2+j2 || i1-j1==i2-j2;
end;
 
compiling || (puts("queens 4: " + str(queens 4)) $$
puts("Solutions to queens 7: " + str(#queens 7)));</syntaxhighlight>
 
{{out}}
<pre>prompt$ pure -c queens.pure -o queens
prompt$ time -p ./queens
queens 4: [[(1,2),(2,4),(3,1),(4,3)],[(1,3),(2,1),(3,4),(4,2)]]
Solutions to queens 7: 40
real 0.03
user 0.02
sys 0.00
 
prompt$ pure -i -q queens.pure
> #queens 10;
724
> </pre>
 
=={{header|PureBasic}}==
A recursive approach is taken. A queen is placed in an unused column for each new row. An array keeps track if a queen has already been placed in a given column so that no duplicate columns result. That handles the Rook attacks. Bishop attacks are handled by checking the diagonal alignments of each new placement against the previously placed queens and if an attack is possible the solution backtracks. The solutions are kept track of in a global variable and the routine <tt>queens(n)</tt> is called with the required number of queens specified.
<langsyntaxhighlight PureBasiclang="purebasic">Global solutions
 
Procedure showBoard(Array queenCol(1))
Line 6,566 ⟶ 12,637:
Input()
CloseConsole()
EndIf</langsyntaxhighlight>
Sample output showing the last solution (all are actually displayed) for 1 - 12 queens:
<pre style="height:40ex;overflow:scroll"> Solution 1
Line 6,687 ⟶ 12,758:
=={{header|Python}}==
===Python: Raymond Hettingers permutations based solution===
This solution, originally by [http://code.activestate.com/recipes/576647/ Raymond Hettinger] for demonstrating the power of the itertools module, generates all solutions. On a regular 8x8 board only 40,320 possible queen positions are examined.
 
<langsyntaxhighlight lang="python">from itertools import permutations
 
n = 8
Line 6,696 ⟶ 12,767:
if n == len(set(vec[i]+i for i in cols)) \
== len(set(vec[i]-i for i in cols)):
print ( vec )</langsyntaxhighlight>
 
The output is presented in vector form (each number represents the column position of a queen on consecutive rows).
The vector can be pretty printed by substituting a call to <code>board</code> instead of <code>print</code>, with the same argument, and where board is pre-defined as:
<langsyntaxhighlight lang="python">def board(vec):
print ("\n".join('.' * i + 'Q' + '.' * (n-i-1) for i in vec) + "\n===\n")</langsyntaxhighlight>
 
Raymond's description is:
Line 6,714 ⟶ 12,785:
 
===Python: Alternative Solution===
On a regular 8x8 board only 15,720 possible queen positions are examined.
{{works with|Python|2.6, 3.x}}
<langsyntaxhighlight lang="python"># From: http://wiki.python.org/moin/SimplePrograms, with permission from the author, Steve Howell
BOARD_SIZE = 8
 
Line 6,731 ⟶ 12,803:
return solutions
 
for answer in solve(BOARD_SIZE): print(list(enumerate(answer, start=1)))</langsyntaxhighlight>
 
===Python: Simple Backtracking Solution===
A surprisingly simple change to the above code (changing the list comprehension
to a generator expression) produces a backtracking solution:. On a regular 8x8 board only 15,720 possible queen positions are examined.
{{works with|Python|2.6, 3.x}}
<langsyntaxhighlight lang="python">BOARD_SIZE = 8
 
def under_attack(col, queens):
Line 6,755 ⟶ 12,827:
answers = solve(BOARD_SIZE)
first_answer = next(answers)
print(list(enumerate(first_answer, start=1)))</langsyntaxhighlight>
 
===Python: backtrackingNiklaus onWirth permutationsalgorithm===
The following program is a translation of Niklaus Wirth's solution into the Python programming language, but does without the index arithmetic used in the original and uses simple lists instead, which means that the array ''x'' for recording the solution can be omitted. A generator replaces the procedure (see [https://www.inf.ethz.ch/personal/wirth/ Niklaus Wirth] or [https://informatika-21.ru/ADen/ Fyodor Tkachov]: Algorithms and Data Structures, pages 114 to 118). On a regular 8x8 board only 15,720 possible queen positions are examined.
<syntaxhighlight lang="python">def queens(n: int, i: int, a: list, b: list, c: list):
if i < n:
for j in range(n):
if j not in a and i + j not in b and i - j not in c:
yield from queens(n, i + 1, a + [j], b + [i + j], c + [i - j])
else:
yield a
 
 
for solution in queens(8, 0, [], [], []):
print(solution)</syntaxhighlight>
The algorithm can be easily improved by using permutations and O(1) sets instead of O(n) lists and by avoiding unnecessary copy operations during recursion. An additional list ''x'' was added to record the solution. On a regular 8x8 board only 5,508 possible queen positions are examined.
<syntaxhighlight lang="python">def queens(i: int, a: set):
if a: # set a is not empty
for j in a:
if i + j not in b and i - j not in c:
b.add(i + j); c.add(i - j); x.append(j)
yield from queens(i + 1, a - {j})
b.remove(i + j); c.remove(i - j); x.pop()
else:
yield x
 
 
b = set(); c = set(); x = []
for solution in queens(0, set(range(8))):
print(solution)</syntaxhighlight>
 
===Python: Backtracking on permutations===
Queens positions on a n x n board are encoded as permutations of [0, 1, ..., n]. The algorithms consists in building a permutation from left to right, by swapping elements of the initial [0, 1, ..., n], recursively calling itself unless the current position is not possible. The test is done by checking only diagonals, since rows/columns have by definition of a permutation, only one queen.
 
This is initially a translation of the Fortran 77 solution. The solutions are returned as a generator, using the "yield from" functionality of Python 3.3, described in [https://www.python.org/dev/peps/pep-0380/ PEP-380]. On a regular 8x8 board only 5,508 possible queen positions are examined.
 
<syntaxhighlight lang="python">def queens(n: int):
The solutions are returned as a generator, using the "yield from" functionality of Python 3.3, described in [https://www.python.org/dev/peps/pep-0380/ PEP-380].
 
<lang python> def queenssub(ni: int):
a = list(range( if i < n)):
up = [True]*(2*n - 1)
down = [True]*(2*n - 1)
def sub(i):
if i == n:
yield tuple(a)
else:
for k in range(i, n):
j = a[k]
p =if b[i + j] and c[i - j]:
q = i - j + n - 1
if up[p] and down[q]:
up[p] = down[q] = False
a[i], a[k] = a[k], a[i]
b[i + j] = c[i - j] = False
yield from sub(i + 1)
upb[pi + j] = downc[qi - j] = True
a[i], a[k] = a[k], a[i]
else:
yield a
 
a = list(range(n))
b = [True] * (2 * n - 1)
c = [True] * (2 * n - 1)
yield from sub(0)
 
#Count solutions for n=8:
sum(1 for p in queens(8))
92</lang>
 
sum(1 for p in queens(8)) # count solutions
The preceding function does not enumerate solutions in lexicographic order, see [[Permutations#Recursive implementation]] for an explanation. The following does, but is almost 50% slower, because the exchange is always made (otherwise the loop to shift the array a by one place would not work).
92</syntaxhighlight>
 
The preceding function does not enumerate solutions in lexicographic order, see [[Permutations#Recursive implementation]] for an explanation. The following does, but is almost 50% slower, because the exchange is always made (otherwise, restoring the state of the array after the loop wouldn't work). On a regular 8x8 board only 5,508 possible queen positions are examined.
 
However, it may be interesting to look at the first solution in lexicographic order: for growing n, and apart from a +1 offset, it gets closer and closer to the sequence [http://oeis.org/A065188 A065188] at OEIS. The first n for which the first solutions differ is n=26.
 
<langsyntaxhighlight lang="python">def queens_lex(n: int):
 
a = list(range(n))
updef = [True]*sub(2*n -i: 1int):
down = [True]*(2*n - 1)if i < n:
def sub(i):
if i == n:
yield tuple(a)
else:
for k in range(i, n):
j = a[k]
a[i], a[k] = a[k], a[i]
if b[i + j] =and ac[i - j]:
p = b[i + j] = c[i - j] = False
q = i - j + n - 1
if up[p] and down[q]:
up[p] = down[q] = False
yield from sub(i + 1)
upb[pi + j] = downc[qi - j] = True
xa[i:(n - 1)], a[n - 1] = a[(i + 1):n], a[i]
for k in range(i + 1, n)else:
a[k - 1] =yield a[k]
 
a[n - 1] = x
a = list(range(n))
b = [True] * (2 * n - 1)
c = [True] * (2 * n - 1)
yield from sub(0)
 
 
next(queens(31))
([0, 2, 4, 1, 3, 8, 10, 12, 14, 6, 17, 21, 26, 28, 25, 27, 24, 30, 7, 5, 29, 15, 13, 11, 9, 18, 22, 19, 23, 16, 20)]
 
next(queens_lex(31))
([0, 2, 4, 1, 3, 8, 10, 12, 14, 5, 17, 22, 25, 27, 30, 24, 26, 29, 6, 16, 28, 13, 9, 7, 19, 11, 15, 18, 21, 23, 20)]
 
#Compare to A065188
#1, 3, 5, 2, 4, 9, 11, 13, 15, 6, 8, 19, 7, 22, 10, 25, 27, 29, 31, 12, 14, 35, 37, ...</langsyntaxhighlight>
 
===Python: fold/reduce===
Expressed in terms of nested folds, allowing for graphic display of results, and listing the number of solutions found for boards of various sizes. On a regular 8x8 board only 15,720 possible queen positions are examined.
{{Works with|Python|3.7}}
<syntaxhighlight lang="python">'''N Queens problem'''
 
from functools import reduce
from itertools import chain
 
 
# queenPuzzle :: Int -> Int -> [[Int]]
def queenPuzzle(nCols):
'''All board patterns of this dimension
in which no two Queens share a row,
column, or diagonal.
'''
def go(nRows):
lessRows = nRows - 1
return reduce(
lambda a, xys: a + reduce(
lambda b, iCol: b + [xys + [iCol]] if (
safe(lessRows, iCol, xys)
) else b,
enumFromTo(1)(nCols),
[]
),
go(lessRows),
[]
) if 0 < nRows else [[]]
return go
 
 
# safe :: Int -> Int -> [Int] -> Bool
def safe(iRow, iCol, pattern):
'''True if no two queens in the pattern
share a row, column or diagonal.
'''
def p(sc, sr):
return (iCol == sc) or (
sc + sr == (iCol + iRow)
) or (sc - sr == (iCol - iRow))
return not any(map(p, pattern, range(0, iRow)))
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Number of solutions for boards of various sizes'''
 
n = 5
xs = queenPuzzle(n)(n)
 
print(
str(len(xs)) + ' solutions for a {n} * {n} board:\n'.format(n=n)
)
print(showBoards(10)(xs))
 
print(
fTable(
'\n\n' + main.__doc__ + ':\n'
)(str)(lambda n: str(n).rjust(3, ' '))(
lambda n: len(queenPuzzle(n)(n))
)(enumFromTo(1)(10))
)
 
 
# ---------------------- FORMATTING ----------------------
 
# showBoards :: Int -> [[Int]] -> String
def showBoards(nCols):
'''String representation, with N columns
of a set of board patterns.
'''
def showBlock(b):
return '\n'.join(map(intercalate(' '), zip(*b)))
 
def go(bs):
return '\n\n'.join(map(
showBlock,
chunksOf(nCols)([
showBoard(b) for b in bs
])
))
return go
 
 
# showBoard :: [Int] -> String
def showBoard(xs):
'''String representation of a Queens board.'''
lng = len(xs)
 
def showLine(n):
return ('.' * (n - 1)) + '♛' + ('.' * (lng - n))
return map(showLine, xs)
 
 
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
f -> xs -> tabular string.
'''
def go(xShow, fxShow, f, xs):
ys = [xShow(x) for x in xs]
w = max(map(len, ys))
return s + '\n' + '\n'.join(map(
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
xs, ys
))
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
xShow, fxShow, f, xs
)
 
 
# ----------------------- GENERIC ------------------------
 
# enumFromTo :: (Int, Int) -> [Int]
def enumFromTo(m):
'''Integer enumeration from m to n.'''
return lambda n: range(m, 1 + n)
 
 
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divible, the final list will be shorter than n.
'''
return lambda xs: reduce(
lambda a, i: a + [xs[i:n + i]],
range(0, len(xs), n), []
) if 0 < n else []
 
 
# intercalate :: [a] -> [[a]] -> [a]
# intercalate :: String -> [String] -> String
def intercalate(x):
'''The concatenation of xs
interspersed with copies of x.
'''
return lambda xs: x.join(xs) if isinstance(x, str) else list(
chain.from_iterable(
reduce(lambda a, v: a + [x, v], xs[1:], [xs[0]])
)
) if xs else []
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre>10 solutions for a 5 * 5 board:
 
♛.... ♛.... .♛... .♛... ..♛.. ..♛.. ...♛. ...♛. ....♛ ....♛
..♛.. ...♛. ...♛. ....♛ ♛.... ....♛ ♛.... .♛... .♛... ..♛..
....♛ .♛... ♛.... ..♛.. ...♛. .♛... ..♛.. ....♛ ...♛. ♛....
.♛... ....♛ ..♛.. ♛.... .♛... ...♛. ....♛ ..♛.. ♛.... ...♛.
...♛. ..♛.. ....♛ ...♛. ....♛ ♛.... .♛... ♛.... ..♛.. .♛...
 
Number of solutions for boards of various sizes:
 
1 -> 1
2 -> 0
3 -> 0
4 -> 2
5 -> 10
6 -> 4
7 -> 40
8 -> 92
9 -> 352
10 -> 724</pre>
 
=={{header|Quackery}}==
 
<code>perms</code> is defined at [[Permutations#Quackery]]. The solution used determines the order in which the n-Queen solutions found are listed. The output illustrated here is from the <code>perms</code> solution titled "An Uncommon Ordering".
 
The method used here stems from the following observations.
 
* Queens can attach with rook (castle) moves or bishop moves.
 
* The solutions to the N-rooks problem correspond to the permutations of the numbers 0 to N-1 in a zero-indexed list.
 
* Two queens are attacking one another with bishop moves to the left (from the appropriate point of view) if the sum of the x-coordinate and the y-coordinate for each of the queens is the same.
 
* A bishop move to the right is the mirror image of a bishop move to the left.
 
<syntaxhighlight lang="Quackery"> [ false 0 rot
witheach
[ i + bit
2dup & iff
[ drop dip not
conclude ]
done
| ]
drop ] is l-bishop ( [ --> b )
 
[ reverse l-bishop ] is r-bishop ( [ --> b )
 
[ [] swap perms
witheach
[ dup l-bishop iff
drop done
dup r-bishop iff
drop done
nested join ] ] is queens ( n --> [ )
 
8 queens
dup size echo say " solutions."
cr cr
witheach
[ echo
i^ 1+ 4 mod iff sp else cr ]</syntaxhighlight>
 
{{out}}
 
<pre>92 solutions.
 
[ 4 1 5 0 6 3 7 2 ] [ 5 2 4 6 0 3 1 7 ] [ 5 3 6 0 2 4 1 7 ] [ 2 5 1 6 4 0 7 3 ]
[ 5 2 0 6 4 7 1 3 ] [ 5 1 6 0 2 4 7 3 ] [ 5 3 6 0 7 1 4 2 ] [ 2 5 1 6 0 3 7 4 ]
[ 5 2 6 1 3 7 0 4 ] [ 5 2 6 3 0 7 1 4 ] [ 1 5 0 6 3 7 2 4 ] [ 5 1 6 0 3 7 4 2 ]
[ 5 2 6 1 7 4 0 3 ] [ 4 6 1 5 2 0 3 7 ] [ 4 6 1 5 2 0 7 3 ] [ 3 6 4 2 0 5 7 1 ]
[ 3 6 4 1 5 0 2 7 ] [ 6 4 2 0 5 7 1 3 ] [ 3 1 6 2 5 7 0 4 ] [ 3 1 6 2 5 7 4 0 ]
[ 0 6 3 5 7 1 4 2 ] [ 6 1 5 2 0 3 7 4 ] [ 1 6 2 5 7 4 0 3 ] [ 6 2 0 5 7 4 1 3 ]
[ 4 1 3 6 2 7 5 0 ] [ 2 4 6 0 3 1 7 5 ] [ 4 6 3 0 2 7 5 1 ] [ 4 6 1 3 7 0 2 5 ]
[ 1 4 6 3 0 7 5 2 ] [ 4 6 0 3 1 7 5 2 ] [ 4 2 0 6 1 7 5 3 ] [ 1 4 6 0 2 7 5 3 ]
[ 4 6 0 2 7 5 3 1 ] [ 3 1 6 4 0 7 5 2 ] [ 6 3 1 4 7 0 2 5 ] [ 2 0 6 4 7 1 3 5 ]
[ 1 6 4 7 0 3 5 2 ] [ 0 6 4 7 1 3 5 2 ] [ 3 6 2 7 1 4 0 5 ] [ 3 6 0 7 4 1 5 2 ]
[ 6 1 3 0 7 4 2 5 ] [ 2 6 1 7 4 0 3 5 ] [ 6 2 7 1 4 0 5 3 ] [ 6 3 1 7 5 0 2 4 ]
[ 2 6 1 7 5 3 0 4 ] [ 6 0 2 7 5 3 1 4 ] [ 4 1 3 5 7 2 0 6 ] [ 4 0 3 5 7 1 6 2 ]
[ 4 2 0 5 7 1 3 6 ] [ 3 5 0 4 1 7 2 6 ] [ 5 3 0 4 7 1 6 2 ] [ 5 2 4 7 0 3 1 6 ]
[ 2 5 1 4 7 0 6 3 ] [ 5 0 4 1 7 2 6 3 ] [ 2 5 3 1 7 4 6 0 ] [ 2 5 3 0 7 4 6 1 ]
[ 5 3 1 7 4 6 0 2 ] [ 5 2 0 7 4 1 3 6 ] [ 2 5 7 0 4 6 1 3 ] [ 1 3 5 7 2 0 6 4 ]
[ 3 5 7 2 0 6 4 1 ] [ 3 5 7 1 6 0 2 4 ] [ 2 5 7 1 3 0 6 4 ] [ 2 5 7 0 3 6 4 1 ]
[ 5 2 0 7 3 1 6 4 ] [ 1 5 7 2 0 3 6 4 ] [ 5 7 1 3 0 6 4 2 ] [ 0 5 7 2 6 3 1 4 ]
[ 3 1 4 7 5 0 2 6 ] [ 3 0 4 7 5 2 6 1 ] [ 4 7 3 0 2 5 1 6 ] [ 2 4 1 7 5 3 6 0 ]
[ 0 4 7 5 2 6 1 3 ] [ 4 0 7 5 2 6 1 3 ] [ 3 1 7 5 0 2 4 6 ] [ 7 2 0 5 1 4 6 3 ]
[ 1 7 5 0 2 4 6 3 ] [ 3 7 0 2 5 1 6 4 ] [ 7 3 0 2 5 1 6 4 ] [ 3 0 4 7 1 6 2 5 ]
[ 2 4 7 3 0 6 1 5 ] [ 4 2 7 3 6 0 5 1 ] [ 4 1 7 0 3 6 2 5 ] [ 4 0 7 3 1 6 2 5 ]
[ 4 7 3 0 6 1 5 2 ] [ 2 4 1 7 0 6 3 5 ] [ 3 7 4 2 0 6 1 5 ] [ 3 1 7 4 6 0 2 5 ]
[ 3 7 0 4 6 1 5 2 ] [ 7 1 4 2 0 6 3 5 ] [ 7 1 3 0 6 4 2 5 ] [ 2 7 3 6 0 5 1 4 ]</pre>
 
=={{header|QBasic}}==
{{works with|QBasic}}
{{trans|QB64}}
<syntaxhighlight lang="qbasic">DIM SHARED queens AS INTEGER
CLS
COLOR 15
INPUT "Numero de reinas"; queens
IF queens <= 0 THEN END
 
CLS
PRINT "queens: Calcula el problema de las"; queens; " reinas."
DIM SHARED arrayqcol(queens) AS LONG ' columnas de reinas
DIM SHARED nsoluciones AS LONG
 
dofila (1)' comenzar en la fila 1
COLOR 14: LOCATE 6 + (2 * queens), 1: PRINT "Hay " + STR$(nsoluciones) + " soluciones"
END
 
SUB dofila (ifila) ' comienza con la fila de abajo
FOR icol = 1 TO queens
FOR iqueen = 1 TO ifila - 1 ' Comprueba conflictos con las reinas anteriores
IF arrayqcol(iqueen) = icol THEN GOTO continue1 ' misma columna?
' iqueen también es fila de la reina
IF iqueen + arrayqcol(iqueen) = ifila + icol THEN GOTO continue1 ' diagonal derecha?
IF iqueen - arrayqcol(iqueen) = ifila - icol THEN GOTO continue1 ' diagonal izquierda?
NEXT iqueen
' En este punto podemos añadir una reina
arrayqcol(ifila) = icol ' añadir al array
COLOR 8
LOCATE ifila + 2, icol: PRINT "x"; ' mostrar progreso
COLOR 15
IF ifila = queens THEN ' solucion?
nsoluciones = nsoluciones + 1
LOCATE 4 + queens, 1: PRINT "Solucion #" + STR$(nsoluciones)
FOR i1 = 1 TO queens ' filas
s1$ = STRING$(queens, ".") ' columnas
MID$(s1$, arrayqcol(i1), 1) = "Q" ' Q en la columna reina
PRINT s1$
NEXT i1
PRINT ""
ELSE
dofila (ifila + 1)' llamada recursiva a la siguiente fila
END IF
COLOR 7: LOCATE ifila + 2, icol: PRINT "."; ' quitar reina
continue1:
NEXT icol
END SUB</syntaxhighlight>
 
 
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
DIM SHARED QUEENS AS INTEGER
PRINT "# of queens:";: INPUT QUEENS
IF QUEENS = 0 THEN END
OPEN LTRIM$(STR$(QUEENS)) + "queens.dat" FOR OUTPUT AS #1
PRINT "Queens: Calculates"; QUEENS; " queens problem."
DIM SHARED arrayqcol(QUEENS) AS LONG ' columns of queens
DIM SHARED nsolutions AS LONG
CALL dorow(1) ' start with row 1
LOCATE 22, 1
PRINT STR$(nsolutions) + " solutions"
END
SUB dorow (irow) ' starts with row irow
FOR icol = 1 TO QUEENS
FOR iqueen = 1 TO irow - 1 ' check for conflict with previous queens
IF arrayqcol(iqueen) = icol THEN GOTO continue1 ' same column?
' iqueen is also row of queen
IF iqueen + arrayqcol(iqueen) = irow + icol THEN GOTO continue1 ' right diagonal?
IF iqueen - arrayqcol(iqueen) = irow - icol THEN GOTO continue1 ' left diagonal?
NEXT iqueen
' at this point we can add a queen
arrayqcol(irow) = icol ' add to array
LOCATE irow + 2, icol: PRINT "x"; ' show progress
_DELAY (.001) ' slows processing
IF irow = QUEENS THEN ' solution?
nsolutions = nsolutions + 1
PRINT #1, "Solution #" + MID$(STR$(nsolutions), 2) + "."
FOR i1 = 1 TO QUEENS ' rows
s1$ = STRING$(QUEENS, ".") ' columns
MID$(s1$, arrayqcol(i1), 1) = "x" ' x in queen column
PRINT #1, s1$
NEXT i1
PRINT #1, ""
ELSE
CALL dorow(irow + 1) ' recursive call to next row
END IF
LOCATE irow + 2, icol: PRINT "."; ' remove queen
continue1:
NEXT icol
END SUB
</syntaxhighlight>
 
=={{header|R}}==
<lang r># Brute force, see the "Permutations" page for the next.perm function
safe <- function(p) {
n <- length(p)
for(i in 1:(n-1)) {
for(j in (i+1):n) {
if(abs(p[j] - p[i]) == abs(j - i)) return(FALSE)
}
}
return(TRUE)
}
 
{{trans|Stata}}
queens <- function(n) {
 
p <- 1:n
This solution uses recursive backtracking.
k <- 0
 
while(!is.null(p)) {
<syntaxhighlight lang="r">queens <- function(n) {
if(safe(p)) {
a <- seq(n)
cat(p,"\n")
k u <- krep(T, +2 * n - 1)
v <- rep(T, 2 * n - 1)
}
p m <- next.perm(p)NULL
aux <- function(i) {
}
if (i > n) {
return(k)
m <<- cbind(m, a)
}
} else {
for (j in seq(i, n)) {
k <- a[[j]]
p <- i - k + n
q <- i + k - 1
if (u[[p]] && v[[q]]) {
u[[p]] <<- v[[q]] <<- F
a[[j]] <<- a[[i]]
a[[i]] <<- k
aux(i + 1)
u[[p]] <<- v[[q]] <<- T
a[[i]] <<- a[[j]]
a[[j]] <<- k
}
}
}
}
aux(1)
m
}</syntaxhighlight>
 
Show the first solution found for size 8 as a permutation matrix.
 
<syntaxhighlight lang="r">library(Matrix)
a <- queens(8)
as(a[, 1], "pMatrix")</syntaxhighlight>
 
{{out}}
 
<pre>8 x 8 sparse Matrix of class "pMatrix"
[1,] | . . . . . . .
[2,] . . . . | . . .
[3,] . . . . . . . |
[4,] . . . . . | . .
[5,] . . | . . . . .
[6,] . . . . . . | .
[7,] . | . . . . . .
[8,] . . . | . . . .</pre>
 
Count solutions for board size 4 to 12.
 
<syntaxhighlight lang="r">sapply(4:12, function(n) ncol(queens(n)))</syntaxhighlight>
 
{{out}}
 
<pre>[1] 2 10 4 40 92 352 724 2680 14200</pre>
queens(8)
# 1 5 8 6 3 7 2 4
# ...
# 92</lang>
 
=={{header|Racket}}==
Line 6,858 ⟶ 13,320:
Backtracking algorithm; returns one solution
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 6,889 ⟶ 13,351:
(nqueens 8)
; => (list (Q 3 7) (Q 1 6) (Q 6 5) (Q 2 4) (Q 5 3) (Q 7 2) (Q 4 1) (Q 0 0))
</syntaxhighlight>
</lang>
 
Show result with "How to Design Programs" GUI.
<langsyntaxhighlight lang="racket">
(require htdp/show-queen)
 
Line 6,903 ⟶ 13,365:
 
(show-nqueens 8)
</syntaxhighlight>
</lang>
 
[[image:Racket-nqueens.png]]
Line 6,915 ⟶ 13,377:
Computes all solutions.
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 6,961 ⟶ 13,423:
'() qss-so-far)))
(lazy-filter valid? all-possible-solutions))
</syntaxhighlight>
</lang>
 
Taking the first solution does not compute the other solutions:
 
<langsyntaxhighlight lang="racket">
(car (nqueens 8))
;; => (list (Q 7 3) (Q 6 1) (Q 5 6) (Q 4 2) (Q 3 5) (Q 2 7) (Q 1 4) (Q 0 0))
</syntaxhighlight>
</lang>
 
Computing all solutions is also possible:
 
<langsyntaxhighlight lang="racket">
(define (force-and-print qs)
(define forced (force qs))
Line 6,990 ⟶ 13,452:
;(list (Q 7 3) (Q 6 6) (Q 5 4) (Q 4 1) (Q 3 5) (Q 2 0) (Q 1 2) (Q 0 7))
;(list (Q 7 4) (Q 6 6) (Q 5 1) (Q 4 5) (Q 3 2) (Q 2 0) (Q 1 3) (Q 0 7))
</syntaxhighlight>
</lang>
 
Logic borrowed from the Ruby example
<langsyntaxhighlight lang="racket">
#lang racket
(define (remove x lst)
Line 7,050 ⟶ 13,512:
(define (print-queens n)
(for ([x (queens n)]) (displayln (string-join x))))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-11-29}}
Neither pretty nor efficient, a simple backtracking solution
 
<syntaxhighlight lang="raku" line>sub MAIN(\N = 8) {
sub collision(@field, $row) {
for ^$row -> $i {
my $distance = @field[$i] - @field[$row];
return True if $distance == any(0, $row - $i, $i - $row);
}
False;
}
sub search(@field, $row) {
return @field if $row == N;
for ^N -> $i {
@field[$row] = $i;
return search(@field, $row + 1) || next
unless collision(@field, $row);
}
()
}
for 0 .. N / 2 {
if search [$_], 1 -> @f {
say @f;
last;
}
}
}</syntaxhighlight>
{{out}}
<pre>[0 4 7 5 2 6 1 3]</pre>
 
=={{header|Rascal}}==
 
<langsyntaxhighlight Rascallang="rascal">import Prelude;
 
public set[list[int]] Nqueens(int n){
Line 7,063 ⟶ 13,557:
result += vector;}
return result;
}</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 7,069 ⟶ 13,563:
<br>also changed to allow for the aspect ratio of display terminals to make the chessboard appear square.
 
Logic was added to the REXX program to preserve the color for a black square when a queen is onoccupies it.
 
About half of the REXX code involves presentation (and colorization achieved through dithering) of the chessboard and queens.
<langsyntaxhighlight lang="rexx">/*REXX program places N queens on aan NxN chessboard (the 8eight queens problem). */
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=='",'" then N= 8 /*Not specified: Then use the default.*/
if N<1 then call noSolnOK /*display a message, the board is bad. */
rank= 1; file= 1; #=0 #= 0 /*starting rank&file; #≡number queens.*/
@.= 0; ! pad= left('', 9* (N<18)) ) /*define empty board; set indentation.*/
/* [↓] rank&file ≡ chessboard row&cols*/
do while #<N; @.file.rank= 1 /*keep placing queens until we're done.*/
if ok(file, rank) then do; file= 1; #= #+1 /*Queen not being attacked? Then eureka*/
rank= rank+1 /*use another attempt at another rank. */
iterate /*go and try another queen placement. */
end /* [↑] found a good queen placement. */
@.file.rank= 0 /*It isn't safe. So remove this queen.*/
file= file + 1 /*So, try the next (higher) chess file.*/
do while file>N; rank= rank - 1; if rank==0 then call nOK
do j=1 for N; if \@.j.rank then iterate /*¿ocupado?*/
@.j.rank= 0; #= # - 1; file= j + 1; leave
end /*j*/
end /*while file>N*/
end /*while #<N*/
call show /*display the chess board with queens. */
exit 1 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
nOK: say; say "No solution for" N 'queens.'; say; exit 0
/*──────────────────────────────────────────────────────────────────────────────────────*/
ok: parse arg f,r; fp= f + 1; rm= r - 1 /*if return≡0, then queen isn't safe. */
do k=1 for rm; if @.f.k then return 0; end
f= f-1; do k=rm by -1 for rm while f\==0; if @.f.k then return 0; f= f-1; end
f= fp; do k=rm by -1 for rm while f <=N; if @.f.k then return 0; f= f+1; end
return 1 /*1≡queen is safe. */ /* ↑↑↑↑↑↑↑↑ is queen under attack? */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: say 'A solution for ' N " queens:" /*display a title to the terminal.*/
g= substr( copies("╬═══", N) ,2) /*start of all cells on chessboard*/
say; say pad translate('╔'g"╗", '╦', "╬") /*display top rank (of the board).*/
line = '╠'g"╣"; dither= "▓"; ditherQ= '░' /*define a line for cell boundary.*/
bar = '║' ; queen = "Q" /*kinds: horiz., vert., salad.*/
Bqueen = ditherQ || queen || ditherQ /*glyph befitting a black square Q*/
Wqueen = ' 'queen" " /* " " " white " "*/
do rank=1 for N; if rank\==1 then say pad line; _= /*show rank sep. */
do file=1 for N
B = (file + rank) // 2 /*Is the square black ? Then B=1.*/
if B then Qgylph= Bqueen /*if black square, use dithered Q.*/
else Qgylph= Wqueen /* " white " " white " */
if @.file.rank then _= _ || bar || Qgylph /*Has queen? Use a 3─char Q symbol*/
else if B then _=_ || bar || copies(dither,3) /*dithering */
else _=_ || bar || copies( ' ' ,3) /* 3 blanks */
end /*file*/ /* [↑] preserve square─ish board.*/
say pad _ || bar /*show a single rank of the board.*/
end /*rank*/ /*80 cols can view a 19x19 board.*/
say pad translate('╚'g"╝", '╩', "╬"); return /*display the last rank (of board)*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default of an &nbsp; '''8'''<small>x</small>'''8''' &nbsp; chessboard:}}
<pre>
A solution for 8 queens:
 
╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
do while #<N; @.file.rank=1 /*keep placing queens until we're done.*/
║ Q ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║
if ok(file,rank) then do; #=#+1 /*Queen not being attacked? Then eureka*/
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
file=1 /*use another attempt at another file. */
║▓▓▓║ ║▓▓▓║ ║░Q░║ ║▓▓▓║ ║
rank=rank+1 /*and also bump the rank counter. */
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
iterate /*go and try another queen placement. */
║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║░Q░║
end /* [↑] found a good queen placement. */
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
@.file.rank=0 /*It isn't safe. So remove this queen.*/
║▓▓▓║ ║▓▓▓║ ║▓▓▓║ Q ║▓▓▓║ ║
file=file+1 /*So, try the next (higher) file. */
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
do while file>N; rank=rank-1; if rank==0 then call noSol
║ ║▓▓▓║ Q ║▓▓▓║ ║▓▓▓║ ║▓▓▓║
do j=1 for N; if \@.j.rank then iterate /*occupied?*/
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
file=j; @.file.rank=0; #=#-1; file=j+1; leave /*j*/
║▓▓▓║ ║▓▓▓║ end║▓▓▓║ /*j*/ ║░Q░║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
end /*while file>N*/
end ║░Q░║ ║▓▓▓║ /*while ║▓▓▓║ #<N*/║▓▓▓║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║▓▓▓║ ║▓▓▓║ Q ║▓▓▓║ ║▓▓▓║ ║
╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
</pre>
{{out|output|text=&nbsp; when using &nbsp; '''20'''<small>x</small>'''20''' &nbsp; chessboard with the input of: &nbsp; &nbsp; <tt> 20 </tt>}}
<pre>
A solution for 20 queens:
 
╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
say 'A solution for' N "queens:"; a = substr( copies("┼───", N) ,2); say
║ Q ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║
say ! translate('┌'a"┐", '┬', "┼") /*display the top rank (of the board).*/
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
line = '├'a"┤"; dither='░' /*define a line (bar) for cell boundry.*/
║▓▓▓║ ║░Q░║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║
bar = '│' ; queen='Q' /*kinds: horizontal, vertical, salad. */
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
Bqueen = dither || queen || dither /*glyph befitting a black-square queen.*/
║ ║▓▓▓║ ║▓▓▓║ Q ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║
Wqueen = ' 'queen" " /* " " " white-square " */
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║▓▓▓║ Q ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║▓▓▓║ ║░Q░║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║░Q░║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ Q ║▓▓▓║ ║▓▓▓║ ║▓▓▓║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ Q ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║░Q░║ ║▓▓▓║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ Q ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ Q ║▓▓▓║ ║▓▓▓║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║░Q░║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║░Q░║ ║▓▓▓║ ║▓▓▓║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║░Q░║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║░Q░║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ Q ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ Q ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ Q ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║ ║▓▓▓║ ║▓▓▓║ ║░Q░║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║
╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║░Q░║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║▓▓▓║ ║
╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
</pre>
 
=={{header|Ring}}==
do rank=1 for N; if rank\==1 then say ! line; _= /*display sep for rank*/
<syntaxhighlight lang="ring">
do file=1 for N; B=(file+rank)//2 /*is the square black?*/
Qgylph=Wqueen; if B then Qgylph=Bqueen /*use a dithered queen*/
if @.file.rank then _=_ || bar || Qgylph /*use the 3-char symbol for queen*/
else if B then _=_||bar||copies(dither,3) /*use dithering.*/
else _=_||bar||copies(' ', 3) /*use 3 blanks. */
end /*file*/ /* [↑] preserve square-ish chessboard.*/
say ! _ || bar /*show a single rank of the chessboard.*/
end /*rank*/ /*80 cols can view a 19x19 chessboard.*/
 
// Bert Mariani 2020-07-17
say ! translate('└'a"┘", '┴', "┼") /*display the last rank (of the board).*/
 
exit 1 /*stick a fork in it, we're all done. */
See "Enter value of N : " Give n // Ask User for Size of Board
/*────────────────────────────────────────────────────────────────────────────*/
n = 0 + n
noSol: say; say "No solution for" N 'queens.'; say; exit 0
x = 1:n
/*────────────────────────────────────────────────────────────────────────────*/
See "Possible placements: Value as Column of the Row "+ nl
ok: parse arg f,r; rm=r-1; fm=f-1; fp=f+1
 
do k=1 for rm; if @.f.k then return 0; end
nQueen(1,n) //===>>> START
f=fm; do k=rm by -1 for rm while f\==0; if @.f.k then return 0; f=f-1; end
 
f=fp; do k=rm by -1 for rm while f <=N; if @.f.k then return 0; f=f+1; end
See nl+ nl+"Enter to Exit program: " Give m // To Exit CMD window
return 1 /* [↑] is the queen under attack? */</lang>
 
'''output''' &nbsp; when using the default of an 8x8 chessboard:
//================================
// Returns true only and only if two queens can be placed in same row or column
 
Func Place(k,i)
 
for j = 1 to k-1
if( x[j] = i OR //two queens in same row
fabs(x[j]-i) = fabs(j-k) ) //two queens in same diagonal
return 0;
ok
next
 
return 1;
 
//================================
 
Func nQueen(k,n)
 
for i = 1 to n
if(place(k,i)) //===>>> Call
x[k] = i
if(k=n)
See nl
for i = 1 to n
See " "+ x[i]
next
else
nQueen(k+1,n) //===>>> RECURSION
ok
ok
next
return
 
//================================
 
</syntaxhighlight>
Output:
<pre>
A solution for 8 queens:
 
Enter value of N : 8
┌───┬───┬───┬───┬───┬───┬───┬───┐
Possible placements: Value as Column of the Row
│ Q │░░░│ │░░░│ │░░░│ │░░░│
 
├───┼───┼───┼───┼───┼───┼───┼───┤
1 5 8 6 3 7 2 4
│░░░│ │░░░│ │░Q░│ │░░░│ │
1 6 8 3 7 4 2 5
├───┼───┼───┼───┼───┼───┼───┼───┤
1 7 4 6 8 2 5 3
│ │░░░│ │░░░│ │░░░│ │░Q░│
1 7 5 8 2 4 6 3
├───┼───┼───┼───┼───┼───┼───┼───┤
2 4 6 8 3 1 7 5
│░░░│ │░░░│ │░░░│ Q │░░░│ │
2 5 7 1 3 8 6 4
├───┼───┼───┼───┼───┼───┼───┼───┤
. . . .
│ │░░░│ Q │░░░│ │░░░│ │░░░│
8 2 5 3 1 7 4 6
├───┼───┼───┼───┼───┼───┼───┼───┤
8 3 1 6 2 5 7 4
│░░░│ │░░░│ │░░░│ │░Q░│ │
8 4 1 3 6 2 7 5
├───┼───┼───┼───┼───┼───┼───┼───┤
 
│ │░Q░│ │░░░│ │░░░│ │░░░│
Enter to Exit program:
├───┼───┼───┼───┼───┼───┼───┼───┤
│░░░│ │░░░│ Q │░░░│ │░░░│ │
└───┴───┴───┴───┴───┴───┴───┴───┘
</pre>
'''output''' &nbsp; when using the input of: <tt> 20 </tt>
<pre style="height:50ex">
A solution for 20 queens:
 
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│ Q │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│░░░│ │░Q░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│ │░░░│ │░░░│ Q │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│░░░│ Q │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│ │░░░│ │░Q░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░Q░│ │░░░│ │░░░│ │░░░│ │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ Q │░░░│ │░░░│ │░░░│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ Q │░░░│ │░░░│ │░░░│ │░░░│ │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░Q░│ │░░░│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ Q │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ Q │░░░│ │░░░│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│░░░│ │░░░│ │░░░│ │░░░│ │░Q░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░Q░│ │░░░│ │░░░│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░Q░│ │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│ │░░░│ │░░░│ │░░░│ │░Q░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│░░░│ │░░░│ │░░░│ │░░░│ │░░░│ Q │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│ │░░░│ │░░░│ │░░░│ Q │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ Q │░░░│ │░░░│ │░░░│ │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│ │░░░│ │░░░│ │░Q░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░░░│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│░░░│ │░░░│ │░░░│ │░░░│ │░░░│ │░Q░│ │░░░│ │░░░│ │░░░│ │░░░│ │
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "guilib.ring"
 
size = 8
newSize = size
count = 0
sizeBoard = 0
Queens = list(size)
Board = []
badBoard = []
 
win = null
nMoves = 0
oldx = 0
oldy = 0
bWidth = 0
bHeight = 0
 
moveX = 550 moveY = 140 ### Open Window on Screen Position
sizeX = 800 sizeY = 800 ### Size of Window
 
Button = null
cmbSize = null
Pink = newlist(size,size)
 
Tiles = newlist(size,size)
TitleMoves = null
lineSize = null
LayoutButtonRow = list(size)
LayoutButtonMain = null
 
WQueen = "WQueen.png"
oPic = new QPixmap("WQueen.png")
oPicGray = new QPixmap("Gray.png")
oPicGreen = new QPixmap("Green.png")
 
nMoves = 0
 
wwidth = 0
wheight = 0
WinWidth = 0
WinHeight = 0
 
C_Spacing = 2
C_ButtonFirstStyle = 'border-radius:1px; color:black; background-color: rgb(229,249,203) ;' ### Square pale
###'border-style: outset; border-width: 2px; border-radius: 2px; border-color: gray;'
 
C_ButtonSecondStyle = 'border-radius:1px; color:black; background-color: rgb(179,200,93); ' ### Square dark
###'border-style: outset; border-width: 2px; border-radius: 2px; border-color: darkGray; '
 
C_ButtonPinkStyle = 'border-radius:1px; color:black; background-color: rgb(255,179,191); ' ### light pink
###'border-style: outset; border-width: 2px; border-radius: 2px; border-color: darkGray; '
 
 
app = new qApp
{
DrawWidget()
newWindow(size)
exec()
}
### FUNCTIONS
 
###============================================================
 
Func DrawWidget()
### Global definition for win
 
win = new qWidget()
{
# Set the Window Icon
setWindowIcon(new qIcon(new qPixmap(WQueen)))
win.setminimumwidth(700)
win.setminimumheight(700)
 
Button = newList(size, size) ### Internal Array with Letters
setWindowTitle('Eight Queens Game')
setStyleSheet('background-color:White')
 
workHeight = win.height()
fontSize = 8 + (workHeight / 100)
move(moveX, moveY)
resize(700,700)
 
wwidth = win.width()
wheight = win.height()
bwidth = wwidth/size
bheight = wheight/size
 
myfilter = new qallevents(win)
myfilter.setResizeEvent("resizeBoard()")
installeventfilter(myfilter)
 
###----------------------------------------------
### Title Top Row - Moves Count
TitleMoves = new qLineEdit(win)
{
setStyleSheet("background-color:rgb(255,255,204)")
setFont(new qFont("Calibri",fontsize,100,0))
setAlignment( Qt_AlignHCenter)
setAlignment( Qt_AlignVCenter)
setText(" Moves: "+ nMoves)
}
 
sizeBtn = new QPushButton(win)
{
setStyleSheet("background-color:rgb(255,255,204)")
setFont(new qFont("Calibri",fontsize,100,0))
setText(" Enter size: ")
}
 
lineSize = new qLineEdit(win)
{
setStyleSheet("background-color:rgb(255,255,204)")
setFont(new qFont("Calibri",fontsize,100,0))
setAlignment( Qt_AlignHCenter)
setAlignment( Qt_AlignVCenter)
setreturnPressedEvent("newBoardSize()")
setText(" 8 ")
}
 
SolveGame = new QPushButton(win)
{
setStyleSheet("background-color:rgb(255,204,229)")
setFont(new qFont("Calibri",fontsize,100,0))
setText(" Solve ")
setClickEvent("solveGame()")
}
 
NewGame = new QPushButton(win)
{
setStyleSheet("background-color:rgb(255,204,229)")
setFont(new qFont("Calibri",fontsize,100,0))
setText(" New ")
setClickEvent("newGame()")
}
 
btnQuit = new QPushButton(win)
{
setStyleSheet("background-color:rgb(255,204,229)")
setFont(new qFont("Calibri",fontsize,100,0))
setText("Exit")
setClickEvent("pQuit()")
}
 
###------------------------------------------------
 
### QVBoxLayout lays out widgets in a vertical column, from top to bottom.
### Vertical
LayoutButtonMain = new QVBoxLayout()
LayoutButtonMain.setSpacing(C_Spacing)
LayoutButtonMain.setContentsMargins(5,5,5,5)
 
### Horizontal - TOP ROW
LayoutTitleRow = new QHBoxLayout()
{
setSpacing(C_Spacing)
setContentsMargins(0,0,0,0)
}
LayoutTitleRow.AddWidget(TitleMoves)
LayoutTitleRow.AddWidget(sizeBtn)
LayoutTitleRow.AddWidget(lineSize)
LayoutTitleRow.AddWidget(SolveGame)
LayoutTitleRow.AddWidget(NewGame)
LayoutTitleRow.AddWidget(btnQuit)
LayoutButtonMain.AddLayout(LayoutTitleRow)
###----------------------------------------------
### BUTTON ROWS
 
LayoutButtonRow = list(size)
### QHBoxLayout lays out widgets in a horizontal row, from left to right
odd = 1
for Row = 1 to size
LayoutButtonRow[Row] = new QHBoxLayout() ### Horizontal
{
setSpacing(C_Spacing)
setContentsmargins(0,0,0,0)
}
for Col = 1 to size
### Create Buttons
 
Button[Row][Col] = new QPushButton(win)
{
if odd % 2 = 0
setStyleSheet(C_ButtonFirstStyle)
odd++
else
setStyleSheet(C_ButtonSecondStyle)
odd++
ok
setClickEvent("UserLeftClick(" + string(Row) +
"," + string(Col) + ")")
setSizePolicy(1,1)
resize(bwidth,bheight)
}
### Widget - Add HORZ BOTTON
LayoutButtonRow[Row].AddWidget(Button[Row][Col])
next
if size % 2 = 0
odd++
ok
 
 
### Layout - Add ROW of BUTTONS
LayoutButtonMain.AddLayout(LayoutButtonRow[Row])
next
###-------------------------------------------------
setLayout(LayoutButtonMain)
 
show()
}
 
return
 
###============================================================
 
func newSize()
nSize = cmbSize.currentText()
nSize = number(nSize)
count = 0
newWindow(nSize)
 
###============================================================
 
func newBoardSize()
nrSize = number(lineSize.text())
newWindow(nrSize)
 
###============================================================
 
func newWindow(newSize)
 
for Row = 1 to size
for Col = 1 to size
Button[Row][Col].delete()
next
next
 
size = newSize
nMoves = 0
TitleMoves.settext(" Moves: 0")
 
Tiles = newlist(size,size)
for Row = 1 to size
for Col = 1 to size
Tiles[Row][Col] = 0
next
next
 
wwidth = win.width()
wheight = win.height()
bwidth = wwidth/size
bheight = wheight/size
 
win.resize(500,500)
 
Button = newlist(size,size)
Pink = newlist(size,size)
 
LayoutButtonRow = list(size)
### QHBoxLayout lays out widgets in a horizontal row, from left to right
odd = 1
for Row = 1 to size
LayoutButtonRow[Row] = new QHBoxLayout() ### Horizontal
{
setSpacing(C_Spacing)
setContentsmargins(0,0,0,0)
}
for Col = 1 to size
### Create Buttons
 
Button[Row][Col] = new QPushButton(win)
{
if odd % 2 = 1
setStyleSheet(C_ButtonFirstStyle)
odd++
else
setStyleSheet(C_ButtonSecondStyle)
odd++
ok
setClickEvent("UserLeftClick(" + string(Row) +
"," + string(Col) + ")")
setSizePolicy(1,1)
resize(bwidth,bheight)
}
### Widget - Add HORZ BOTTON
LayoutButtonRow[Row].AddWidget(Button[Row][Col])
next
if size % 2 = 0
odd++
ok
 
### Layout - Add ROW of BUTTONS
LayoutButtonMain.AddLayout(LayoutButtonRow[Row])
next
###-------------------------------------------------
win.setLayout(LayoutButtonMain)
 
 
###============================================================
 
func solveGame()
 
newWindow(size)
 
for Row = 1 to size
for Col = 1 to size
Tiles[Row][Col] = 0
next
next
 
bwidth = (win.width() -8 ) / size // <<< QT FIX because of Win Title
bheight = (win.height() -32) / size // <<< QT FIX because of Win Title
 
odd = 1
for Row = 1 to size
for Col = 1 to size
if odd % 2 = 1
setButtonImage(Button[Row][Col],oPicGray,bwidth-8,bheight)
odd++
else
setButtonImage(Button[Row][Col],oPicGreen,bwidth-8,bheight)
odd++
ok
next
if size % 2 = 0
odd++
ok
next
 
Queens = list(20)
n = size
count = count + 1
if count = 1
Board = []
queen(1,n)
ok
 
sizeBoard = len(Board)/size
num = random(sizeBoard-1) + 1
see "Solution = " + num + nl
 
for n = (num-1)*size+1 to num*size
x = Board[n][2]
y = Board[n][1]
Tiles[x][y] = 1
setButtonImage(Button[x][y],oPic,bwidth-8,bheight)
 
next
 
###============================================================
func prn(n)
 
for i = 1 to n
for j = 1 to n
if Queens[i] = j
add(Board,[i,j])
ok
next
next
 
###============================================================
 
func place(row,column)
 
for i = 1 to row-1
if Queens[i]=column
return 0
else
if fabs(Queens[i]-column) = fabs(i-row)
return 0
ok
ok
next
return 1
 
###============================================================
func queen(row,n)
 
for column = 1 to n
if place(row,column)
Queens[row] = column
if row = n
prn(n)
else
queen(row+1,n)
ok
ok
next
 
###============================================================
 
func newGame
 
newWindow(size)
return
 
###============================================================
 
func canPlace Row,Col
 
badBoard = []
add(badBoard,[Row,Col])
bwidth = (win.width() -8 ) / size // <<< QT FIX because of Win Title
bheight = (win.height() -32) / size // <<< QT FIX because of Win Title
cp1 = 1
for n = 1 to size
if Row < 9
if n != Col and Tiles[Row][n] = 1
cp1 = 0
add(badBoard,[Row,n])
exit
ok
ok
next
 
cp2 = 1
for n = 1 to size
if Col < 9
if n != Row and Tiles[n][Col] = 1
cp2 = 0
add(badBoard,[n,Col])
exit
ok
ok
next
 
cp3 = 1
for x = 1 to size
if Row + x < size + 1 and Col - x > 0
if Tiles[Row+x][Col-x] = 1
cp3 = 0
add(badBoard,[Row+x,Col-x])
exit
ok
ok
next
 
cp4 = 1
for x = 1 to size
if Row - x > 0 and Col + x < size + 1
if Tiles[Row-x][Col+x] = 1
cp4 = 0
add(badBoard,[Row-x,Col+x])
exit
ok
ok
next
 
cp5 = 1
for x = 1 to size
if Row + x < size + 1 and Col + x < size + 1
if Tiles[Row+x][Col+x] = 1
cp5 = 0
add(badBoard,[Row+x,Col+x])
exit
ok
ok
next
 
cp6 = 1
for x = 1 to size
if Row - x > 0 and Col - x > 0
if Tiles[Row-x][Col-x] = 1
cp6 = 0
add(badBoard,[Row-x,Col-x])
exit
ok
ok
next
 
cp7 = cp1 and cp2 and cp3 and cp4 and cp5 and cp6
 
return cp7
 
###============================================================
 
func resizeBoard
 
bwidth = (win.width() - 8) / size
bheight = (win.height() - 32) / size
 
for Row = 1 to size
for Col = 1 to size
if Tiles[Row][Col] = 1
setButtonImage(Button[Row][Col],oPic,bwidth - 8,bheight - 8)
ok
next
next
 
###============================================================
 
func UserLeftClick Row,Col
 
sleep(0.3)
Tiles[Row][Col] = 1
 
bWidthHeight()
 
bool = (Row = oldx) and (Col = oldy)
 
cp8 = canPlace(Row,Col)
 
if Pink[Row][Col] = 1
Pink[Row][Col] = 0
Tiles[Row][Col] = 0
if Row % 2 = 1 and Col % 2 = 1
Button[Row][Col].setStyleSheet(C_ButtonFirstStyle)
setButtonImage(Button[Row][Col],oPicGray,bwidth-8,bheight-8)
ok
if Row % 2 = 0 and Col % 2 = 0
Button[Row][Col].setStyleSheet(C_ButtonFirstStyle)
setButtonImage(Button[Row][Col],oPicGray,bwidth-8,bheight-8)
ok
if Row % 2 = 1 and Col % 2 = 0
Button[Row][Col].setStyleSheet(C_ButtonSecondStyle)
setButtonImage(Button[Row][Col],oPicGreen,bwidth-8,bheight-8)
ok
if Row % 2 = 0 and Col % 2 = 1
Button[Row][Col].setStyleSheet(C_ButtonSecondStyle)
setButtonImage(Button[Row][Col],oPicGreen,bwidth-8,bheight-8)
ok
checkBoard()
return
ok
 
if cp8 = 1 and bool = 0
setButtonImage(Button[Row][Col],oPic,bwidth-8,bheight-8)
Tiles[Row][Col] = 1
nMoves = nMoves + 1
oldx = Row
oldy = Col
TitleMoves.settext(" Moves: " + nMoves)
gameOver()
ok
if cp8 = 1 and bool = 1
if Row % 2 = 1 and Col % 2 = 1
setButtonImage(Button[Row][Col],oPicGray,bwidth-8,bheight-8)
ok
if Row % 2 = 0 and Col % 2 = 0
setButtonImage(Button[Row][Col],oPicGray,bwidth-8,bheight-8)
ok
if Row % 2 = 1 and Col % 2 = 0
setButtonImage(Button[Row][Col],oPicGreen,bwidth-8,bheight-8)
ok
if Row % 2 = 0 and Col % 2 = 1
setButtonImage(Button[Row][Col],oPicGreen,bwidth-8,bheight-8)
ok
Tiles[Row][Col] = 1
ok
 
for Row = 1 to size
for Col = 1 to size
if Tiles[Row][Col] = 1
setButtonImage(Button[Row][Col],oPic,bwidth-8,bheight-8)
ok
next
next
 
if cp8 = 0
pBadCell(Row,Col)
return
ok
 
###============================================================
 
func checkBoard()
 
for Row = 1 to size
for Col = 1 to size
if Pink[Row][Col] = 1
cp9 = canPlace(Row,Col)
if cp9 = 0
Button[Row][Col].setStyleSheet(C_ButtonPinkStyle)
setButtonImage(Button[Row][Col],oPic,bwidth-8,bheight)
else
if Row % 2 = 1 and Col % 2 = 1
Button[Row][Col].setStyleSheet(C_ButtonFirstStyle)
setButtonImage(Button[Row][Col],oPic,bwidth-8,bheight-8)
ok
if Row % 2 = 0 and Col % 2 = 0
Button[Row][Col].setStyleSheet(C_ButtonFirstStyle)
setButtonImage(Button[Row][Col],oPic,bwidth-8,bheight-8)
ok
if Row % 2 = 1 and Col % 2 = 0
Button[Row][Col].setStyleSheet(C_ButtonSecondStyle)
setButtonImage(Button[Row][Col],oPic,bwidth-8,bheight-8)
ok
if Row % 2 = 0 and Col % 2 = 1
Button[Row][Col].setStyleSheet(C_ButtonSecondStyle)
setButtonImage(Button[Row][Col],oPic,bwidth-8,bheight-8)
ok
ok
ok
next
next
 
###============================================================
 
func pBadCell(Row,Col)
 
for n = 1 to len(badBoard)
Row = badBoard[n][1]
Col = badBoard[n][2]
Pink[Row][Col] = 1
Button[Row][Col].setStyleSheet(C_ButtonPinkStyle)
setButtonImage(Button[Row][Col],oPic,bwidth-8,bheight)
next
 
###============================================================
 
func setButtonImage oBtn,oPixmap,width,height
oBtn { setIcon(new qicon(oPixmap.scaled(width(),height(),0,0)))
setIconSize(new QSize(width,height))
}
 
###============================================================
 
func bWidthHeight()
 
bWidth = (win.width() -8 ) / size
bHeight = (win.height() -32) / size
 
###============================================================
 
func msgBox cText
mb = new qMessageBox(win) {
setWindowTitle('Eight Queens')
setText(cText)
setstandardbuttons(QMessageBox_OK)
result = exec()
}
 
###============================================================
 
func pQuit()
win.close()
 
###============================================================
 
func gameOver
 
total = 0
for Row = 1 to size
for Col = 1 to size
if Tiles[Row][Col] = 1
total = total + 1
ok
next
next
 
if total = size
for Row = 1 to size
for Col = 1 to size
Button[Row][Col].setenabled(false)
next
next
msgBox("You Win!")
ok
 
###============================================================
</syntaxhighlight>
[https://www.mediafire.com/file/53bxu7kpuc4tlx5/Images.zip/file Necessary images]
 
=={{header|Ruby}}==
This implements the heuristics found on the wikipedia page to return just one solution
<langsyntaxhighlight lang="ruby"># 1. Divide n by 12. Remember the remainder (n is 8 for the eight queens
# puzzle).
# 2. Write a list of the even numbers from 2 to n in order.
Line 7,204 ⟶ 14,464:
# list, place the second-column queen in the row with the second number in
# the list, etc.
 
 
def n_queens(n)
Line 7,212 ⟶ 14,473:
return ""
end
 
evens = (2..n).step(2).to_a
odds = (1..n).step(2).to_a
 
rem = n % 12 # (1)
nums = evens # (2)
 
nums.push(nums.shift)rotate if rem == 3 or rem == 9 # (3)
 
# (4)
if rem == 8
odds = odds.each_slice(2).injectflat_map([]&:reverse) {|ary, (a,b)| ary += [b,a]}
end
nums.concat(odds)
 
# (5)
if rem == 2
nums[nums.index(1)], nums[nums.index(3)] = nums[nums.index(3)], nums[nums.index(1)]
idx = []
[1,3,5].eachnums {|i| idx[i] =<< nums.indexdelete(i5)}
nums[idx[1]], nums[idx[3]] = nums[idx[3]], nums[idx[1]]
nums.slice!(idx[5])
nums.push(5)
end
 
# (6)
if rem == 3 or rem == 9
nums << nums.delete(1)
[1,3].each do |i|
nums << nums.slice!delete( nums.index(i) 3)
nums.push(i)
end
end
 
# (7)
nums.map do |q|
board = Array.new(n) {Array.new(n) {"."}}
a = Array.new(n,".")
n.times {|i| board[i][nums[i] - 1] = "Q"}
a[q-1] = "Q"
board.inject("") {|str, row| str << row.join(" ") << "\n"}
a*(" ")
end
end
 
(1 .. 15).each {|n| puts "n=#{n}"; puts n_queens(n); puts}</langsyntaxhighlight>
 
{{out}}
Line 7,404 ⟶ 14,662:
===Alternate solution===
If there is not specification, it outputs all solutions.
<langsyntaxhighlight lang="ruby">class Queen
attr_reader :count
def initialize(num=8)
@num = num
end
def solveinitialize(num=8, out=true)
@num = num
@out = out
@row = *0...@num
@frame = "+-" + "--" * @num + "+"
@count = 0
add = Array.new(2 * @num - 1, true) # \ direction check
sub = Array.new(2 * @num - 1, true) # / direction check
_solvesolve([], add, sub)
@count
end
private
def _solve(row, add, sub)
def solve(row, add, sub)
y = row.size
if y == @num
Line 7,429 ⟶ 14,686:
next unless add[x+y] and sub[x-y]
add[x+y] = sub[x-y] = false
_solvesolve(row+[x], add, sub)
add[x+y] = sub[x-y] = true
end
Line 7,443 ⟶ 14,700:
puts @frame
end
end</langsyntaxhighlight>
 
'''Example:'''
<langsyntaxhighlight lang="ruby">(1..6).each do |n|
puzzle = Queen.new(n)
puts " #{n} Queen : #{puzzle.solvecount}"
end
 
(7..12).each do |n|
puzzle = Queen.new(n, false) # do not display
puts " #{n} Queen : #{puzzle.solve(false)count}" # no display
end</langsyntaxhighlight>
 
{{out}}
Line 7,590 ⟶ 14,847:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">[loop]
input "How many queens (N>=4)";n
if n < 4 then
Line 7,701 ⟶ 14,958:
end if
end if
next</langsyntaxhighlight>
<pre>abcdefgh
* 8
Line 7,713 ⟶ 14,970:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">const N: usize = 8;
 
fn try(mut board: &mut [[bool; N]; N], row: usize, mut count: &mut i64) {
Line 7,745 ⟶ 15,002:
try (&mut board, 0, &mut count);
println!("Found {} solutions", count)
}</langsyntaxhighlight>
 
===Using Iterators===
Solution to the puzzle using an iterator that yields the 92 solutions for 8 queens.
 
<syntaxhighlight lang="rust">
use std::collections::LinkedList;
use std::iter::IntoIterator;
 
fn main() {
for (n, s) in NQueens::new(8).enumerate() {
println!("Solution #{}:\n{}\n", n + 1, s.to_string());
}
}
 
fn permutations<'a, T, I>(collection: I) -> Box<Iterator<dyn Item=LinkedList<T>> + 'a>
where I: 'a + IntoIterator<Item=T> + Clone,
T: 'a + PartialEq + Copy + Clone {
if collection.clone().into_iter().count() == 0 {
Box::new(vec![LinkedList::new()].into_iter())
}
else {
Box::new(
collection.clone().into_iter().flat_map(move |i| {
permutations(collection.clone().into_iter()
.filter(move |&i0| i != i0)
.collect::<Vec<_>>())
.map(move |mut l| {l.push_front(i); l})
})
)
}
}
 
pub struct NQueens {
iterator: Box<Iterator<dyn Item=NQueensSolution>>
}
 
impl NQueens {
pub fn new(n: u32) -> NQueens {
NQueens {
iterator: Box::new(permutations(0..n)
.filter(|vec| {
let iter = vec.iter().enumerate();
iter.clone().all(|(col, &row)| {
iter.clone().filter(|&(c,_)| c != col)
.all(|(ocol, &orow)| {
col as i32 - row as i32 !=
ocol as i32 - orow as i32 &&
col as u32 + row != ocol as u32 + orow
})
})
})
.map(|vec| NQueensSolution(vec))
)
}
}
}
 
impl Iterator for NQueens {
type Item = NQueensSolution;
fn next(&mut self) -> Option<NQueensSolution> {
self.iterator.next()
}
}
 
pub struct NQueensSolution(LinkedList<u32>);
 
impl ToString for NQueensSolution {
fn to_string(&self) -> String {
let mut str = String::new();
for &row in self.0.iter() {
for r in 0..self.0.len() as u32 {
if r == row {
str.push_str("Q ");
} else {
str.push_str("- ");
}
}
str.push('\n');
}
str
}
}
</syntaxhighlight>
 
===Permutation with Filtering===
 
Using Itertools and arrays.
 
{{trans|D}}
 
<syntaxhighlight lang="rust">
extern crate itertools;
 
use itertools::Itertools;
 
fn main() {
const N: usize = 8;
 
let permutations = (0..N).permutations(N);
let solution_count = permutations
.filter(|p| {
let mut diag1 = [false; 2 * N - 1];
let mut diag2 = [false; 2 * N - 1];
for (i, &row) in p.iter().enumerate() {
if diag1[row + i] || diag2[N - 1 + row - i] {
return false; // Queens mutual threat
}
diag1[row + i] = true;
diag2[N - 1 + row - i] = true;
}
true // No Queens mutual threat
})
.count();
 
println!("{}", solution_count);
}
</syntaxhighlight>
 
=={{header|SAS}}==
<langsyntaxhighlight lang="sas">/* Store all 92 permutations in a SAS dataset. Translation of Fortran 77 */
data queens;
array a{8} p1-p8;
Line 7,805 ⟶ 15,183:
put n m;
keep p1-p8;
run;</langsyntaxhighlight>
 
=={{header|Scala}}==
The algorithm below is lazy. It returns an iterator, and each
solution is computed as you ask for the next element of the
iterator. If you ask for one element, it will only compute
one solution.
 
Extends a <code>Tuple2[T,T]</code> (also represented as <code>(T, T)</code>) using an enriched implicit class to define check that positions are safe or threatened.
The test for legal moves is a bit redundant, as the algorithm
can never generate two positions in the same row.
 
Lazily generates permutations with an <code>Iterator</code>.
<lang scala>case class Pos(row: Int, column: Int) {
 
def sameRow(p: Pos) = row == p.row
<syntaxhighlight lang="scala">
def sameColumn(p: Pos) = column == p.column
object NQueens {
def sameDiag(p: Pos) = (p.column - column).abs == (p.row - row).abs
 
def illegal(p: Pos) = sameRow(p) || sameColumn(p) || sameDiag(p)
private implicit class RichPair[T](
def legal(p: Pos) = !illegal(p)
pair: (T,T))(
implicit num: Numeric[T]
) {
import num._
 
def safe(x: T, y: T): Boolean =
pair._1 - pair._2 != abs(x - y)
}
def solve(n: Int): Iterator[Seq[Int]] = {
(0 to n-1)
.permutations
.filter { v =>
(0 to n-1).forall { y =>
(y+1 to n-1).forall { x =>
(x,y).safe(v(x),v(y))
}
}
}
}
 
def main(args: Array[String]): Unit = {
val n = args.headOption.getOrElse("8").toInt
val (solns1, solns2) = solve(n).duplicate
solns1
.zipWithIndex
.foreach { case (soln, i) =>
Console.out.println(s"Solution #${i+1}")
output(n)(soln)
}
val n_solns = solns2.size
if (n_solns == 1) {
Console.out.println("Found 1 solution")
} else {
Console.out.println(s"Found $n_solns solutions")
}
}
 
def output(n: Int)(board: Seq[Int]): Unit = {
board.foreach { queen =>
val row =
"_|" * queen + "Q" + "|_" * (n-queen-1)
Console.out.println(row)
}
}
}
</syntaxhighlight>
 
<pre>
def rowSet(size: Int, row: Int) = Iterator.tabulate(size)(column => Pos(row, column))
scala> NQueens.main(Array("8"))
Solution #1
Q|_|_|_|_|_|_|_
_|_|_|_|Q|_|_|_
_|_|_|_|_|_|_|Q
_|_|_|_|_|Q|_|_
_|_|Q|_|_|_|_|_
_|_|_|_|_|_|Q|_
_|Q|_|_|_|_|_|_
_|_|_|Q|_|_|_|_
 
Solution #2
def expand(solutions: Iterator[List[Pos]], size: Int, row: Int) =
Q|_|_|_|_|_|_|_
for {
_|_|_|_|_|Q|_|_
solution <- solutions
_|_|_|_|_|_|_|Q
pos <- rowSet(size, row)
_|_|Q|_|_|_|_|_
if solution forall (_ legal pos)
_|_|_|_|_|_|Q|_
} yield pos :: solution
_|_|_|Q|_|_|_|_
_|Q|_|_|_|_|_|_
_|_|_|_|Q|_|_|_
...
Found 92 solutions
</pre>
 
=={{header|Scheme}}==
 
This is a simple breadth-first technique to retrieve all solutions.
 
<syntaxhighlight lang="scheme">
(import (scheme base)
(scheme write)
(srfi 1))
 
;; return list of solutions to n-queens problem
(define (n-queens n) ; breadth-first solution
(define (place-initial-row) ; puts a queen on each column of row 0
(list-tabulate n (lambda (col) (list (cons 0 col)))))
(define (place-on-row soln-so-far row)
(define (invalid? col)
(any (lambda (posn)
(or (= col (cdr posn)) ; on same column
(= (abs (- row (car posn))) ; on same diagonal
(abs (- col (cdr posn))))))
soln-so-far))
;
(do ((col 0 (+ 1 col))
(res '() (if (invalid? col)
res
(cons (cons (cons row col) soln-so-far)
res))))
((= col n) res)))
;
(do ((res (place-initial-row)
(apply append
(map (lambda (soln-so-far) (place-on-row soln-so-far row))
res)))
(row 1 (+ 1 row)))
((= row n) res)))
 
;; display solutions in 2-d array form
(define (pretty-print solutions n)
(define (posn->index posn)
(+ (* n (cdr posn))
(car posn)))
(define (pp solution)
(let ((board (make-vector (square n) ".")))
(for-each (lambda (queen) (vector-set! board
(posn->index queen)
"Q"))
solution)
(let loop ((row 0)
(col 0))
(cond ((= row n)
(newline))
((= col n)
(newline)
(loop (+ 1 row) 0))
(else
(display (vector-ref board (posn->index (cons row col))))
(loop row (+ 1 col)))))))
;
(display (string-append "Found "
(number->string (length solutions))
" solutions for n="
(number->string n)
"\n\n"))
(for-each pp solutions))
 
;; create table of number of solutions
(do ((n 1 (+ 1 n)))
((> n 10) )
(display n)
(display " ")
(display (length (n-queens n)))
(newline))
 
;; show some examples
(pretty-print (n-queens 1) 1)
(pretty-print (n-queens 2) 2)
(pretty-print (n-queens 3) 3)
(pretty-print (n-queens 4) 4)
(pretty-print (n-queens 5) 5)
(pretty-print (n-queens 8) 8)
</syntaxhighlight>
 
{{out}}
<pre>
1 1
2 0
3 0
4 2
5 10
6 4
7 40
8 92
9 352
10 724
Found 1 solutions for n=1
 
Q
 
Found 0 solutions for n=2
 
Found 0 solutions for n=3
 
Found 2 solutions for n=4
 
.Q..
...Q
Q...
..Q.
 
..Q.
Q...
...Q
.Q..
 
Found 10 solutions for n=5
 
Q....
...Q.
.Q...
....Q
..Q..
 
Q....
..Q..
....Q
.Q...
...Q.
 
[[ etc ]]
Found 92 solutions for n=8
 
Q.......
......Q.
....Q...
.......Q
.Q......
...Q....
.....Q..
..Q.....
 
Q.......
......Q.
...Q....
.....Q..
.......Q
.Q......
....Q...
..Q.....
 
[[ etc ]]
</pre>
 
=={{header|Scilab}}==
Naive brute-force search.
<syntaxhighlight lang="scilab">//Length of board side
Board_size = 8;
 
function flag_out = no_attack(side, board, pos)
//Evaluates (pos(1),pos(2)) in board if it's not on any queen attacking range
//side (scalar): board's side length
//board (sidexside matrix): matrix of 0s and 1s representing queens on a board
//pos (1x2 matrix): postition on board to be evaluated
//flag_out (bool): %T if position is available, and %F otherwise
//Counting queens on rows and columns
row_col = sum(board(pos(1),:)) + sum(board(:,pos(2)));
//Counting queens on first diagonal
diag_1 = sum(...
diag(board, 0 +...
(pos(2)>pos(1))*(pos(2)-pos(1)) +...
(pos(1)>pos(2))*(pos(2)-pos(1))...
)...
);
//Counting queens on second diagonal
a = pos(1) + pos(2);
if a<=side+1 then
rows = [1:a-1]
cols = a - rows;
else
d = 2*(side+1)-a-1;
rows = [side:-1:side-d+1]
cols = a - rows;
end
 
diag_2 = 0;
for i = 1:length(rows)
diag_2 = diag_2 + board(rows(i),cols(i));
end
//Check if there's any queen
flag_out = ( ~(row_col | diag_1 | diag_2) );
endfunction
 
//Solution counter
Sol_count = 0;
//"Soltion found" flag
Sol_found = %F;
//Empty board
Board = zeros(Board_size,Board_size);
//Matrix for backtracking
Queens= zeros(Board_size,2);
 
//Queens counter
N_queens = Board_size;
 
//Row and column counters
i = 1; j = 1;
 
//Start counting time
tic();
 
//Begin search
while i <= Board_size
while j <= Board_size
//Availability flag: check position (i,j)
flag = %F;
if (0 < i & 0 < j) & (i <= Board_size & j <= Board_size) then
flag = no_attack(Board_size,Board,[i j]);
end
//Reset solution flag
Sol_found = %F;
if flag then
//Put a queen on the board if position is available
Board(i,j) = 1;
//Update number of remaining queens
N_queens = N_queens - 1;
//Keep track of queens positions
Queens(Board_size - N_queens,:) = [i j];
//Jump to next row end of line is reached
if i+1<=Board_size
i = i + 1;
end
//Start over from the begining of new line
j = 0;
//Count and flag a solution if all queens have
//been placed on the board
if N_queens == 0 then
Sol_count = Sol_count + 1;
Sol_found = %T;
break
end
end
//Increment column number
j = j + 1;
end
//Increment row number and start from first column
if ~Sol_found then
i = i + 1;
j = 1;
//Limiting placement of the first queen to the first row
//Stop searching solutions if columns of first row
//have been tested
if i == 2 & j == 1 & sum(Board) == 0 then
break
end
end
//Backtracking: if (i,j) reaches the and of the board
//and there are queens left to be placed on it
if ~Sol_found & i == Board_size + 1 & j == 1 then
ind = Board_size - N_queens;
if ind > 0 then
//Recover last queen's position
i = Queens(ind,1);
j = Queens(ind,2);
//Remove it from the board and from the counter
Board(i,j) = 0;
Queens(ind,:) = [0 0];
N_queens = N_queens + 1;
//Move to next column
j = j + 1;
end
end
end
 
//Printing result on console
disp("There are "+string(Sol_count)+" solutions for a "+...
string(Board_size)+"x"+string(Board_size)+" board.");
//Time elapsed
disp("Time: "+string(toc())+"s.");</syntaxhighlight>
 
{{out}}
 
<pre> There are 92 solutions for a 8x8 board.
def seed(size: Int) = rowSet(size, 0) map (sol => List(sol))
 
Time: 58.705327s.</pre>
def solve(size: Int) = (1 until size).foldLeft(seed(size)) (expand(_, size, _))</lang>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
var array integer: board is 8 times 0;
Line 7,888 ⟶ 15,621:
end if;
end while;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<syntaxhighlight lang="ruby">func N_queens_solution(N = 8) {
 
func collision(field, row) {
for i in (^row) {
var distance = (field[i] - field[row])
distance ~~ [0, row-i, i-row] && return true
}
return false
}
 
func search(field, row) {
row == N && return field
for i in (^N) {
field[row] = i
if (!collision(field, row)) {
return (__FUNC__(field, row+1) || next)
}
}
return []
}
 
for i in (0 .. N>>1) {
if (var r = search([i], 1)) {
return r
}
}
}
 
for n in (1..15) {
say "#{'%2d' % n}: #{N_queens_solution(n) || 'No solution'}"
}</syntaxhighlight>
{{out}}
<pre>
1: [0]
2: No solution
3: No solution
4: [1, 3, 0, 2]
5: [0, 2, 4, 1, 3]
6: [1, 3, 5, 0, 2, 4]
7: [0, 2, 4, 6, 1, 3, 5]
8: [0, 4, 7, 5, 2, 6, 1, 3]
9: [0, 2, 5, 7, 1, 3, 8, 6, 4]
10: [0, 2, 5, 7, 9, 4, 8, 1, 3, 6]
11: [0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9]
12: [0, 2, 4, 7, 9, 11, 5, 10, 1, 6, 8, 3]
13: [0, 2, 4, 1, 8, 11, 9, 12, 3, 5, 7, 10, 6]
14: [0, 2, 4, 6, 11, 9, 12, 3, 13, 8, 1, 5, 7, 10]
15: [0, 2, 4, 1, 9, 11, 13, 3, 12, 8, 5, 14, 6, 10, 7]
</pre>
 
=={{header|SNOBOL4}}==
<syntaxhighlight lang="snobol4">
<lang SNOBOL4>
* N queens problem
* Set N to the desired number. The program prints out all solution boards.
Line 7,915 ⟶ 15,700:
PRTLOOP B LEN(NP1) . OUTPUT = :S(PRTLOOP)F(RETURN)
END
</syntaxhighlight>
</lang>
 
=={{header|Sparkling}}==
This is somewhat a transliteration of the "shortened" C++ code above.
 
<langsyntaxhighlight lang="sparkling">let print_table = function (pos) {
pos.foreach(function (_, i) {
stdout.printf(" %c", 'a' + i);
Line 7,973 ⟶ 15,758:
};
 
stdout.printf("%d solutions\n", n_queens(range(8), 0));</langsyntaxhighlight>
 
=={{header|SQL}}==
Line 7,979 ⟶ 15,764:
This implementation, which solves the problem for n=8, makes use of Common Table Expressions and has been tested with SQLite (>=3.8.3) and Postgres (please note the related comment in the code). It might be compatible with other SQL dialects as well. A gist with the SQL file and a Python script that runs it using SQLite is available on Github: https://gist.github.com/adewes/5e5397b693eb50e67f07
 
<syntaxhighlight lang="sql">
<lang SQL>
WITH RECURSIVE
positions(i) as (
Line 8,010 ⟶ 15,795:
SELECT board,n_queens FROM solutions WHERE n_queens = 8;
 
</syntaxhighlight>
</lang>
 
=={{header|SQL PL}}==
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<syntaxhighlight lang="sql pl">
-- A column of a matrix.
CREATE TYPE INTEGER_ARRAY AS INTEGER ARRAY[]@
-- The whole matrix of any size.
CREATE TYPE INTEGER_MATRIX AS INTEGER_ARRAY ARRAY[]@
/**
* Retrieves the value from a matrix at a specific position.
*
* IN X: Row number.
* IN Y: Column number.
* IN M: Matrix.
* RETURN the integer value at that position.
*/
CREATE OR REPLACE FUNCTION GET_INTEGER_VALUE(
IN X SMALLINT,
IN Y SMALLINT,
IN M INTEGER_MATRIX)
RETURNS INTEGER
F_GET_INTEGER_VALUE: BEGIN
DECLARE A INTEGER_ARRAY;
DECLARE RET INTEGER;
SET A = M[X];
SET RET = A[Y];
RETURN RET;
END F_GET_INTEGER_VALUE
@
 
/**
* Establishes the given value at a specific position in the matrix.
*
* IN X: Row number.
* IN Y: Column number.
* INOUT M: Matrix.
* IN VAL: Value to set in the matrix.
*/
CREATE OR REPLACE PROCEDURE SET_INTEGER_VALUE(
IN X SMALLINT,
IN Y SMALLINT,
INOUT M INTEGER_MATRIX,
IN VAL INTEGER)
P_SET_INTEGER_VALUE: BEGIN
DECLARE A INTEGER_ARRAY;
SET A = M[X];
SET A[Y] = VAL;
SET M[X] = A;
END P_SET_INTEGER_VALUE
@
 
/**
* Initializes the matriz at a given size with the same value in all positions.
*
* INOUT M: Matrix.
* IN X: Number of rows.
* IN Y: Number of columns per row.
* IN VAL: Value to set in the matrix.
*/
CREATE OR REPLACE PROCEDURE INIT_INTEGER_MATRIX(
INOUT M INTEGER_MATRIX,
IN X SMALLINT,
IN Y SMALLINT,
IN VAL INTEGER)
P_INIT_INTEGER_MATRIX: BEGIN
DECLARE I SMALLINT DEFAULT 1;
DECLARE J SMALLINT;
DECLARE A INTEGER_ARRAY;
WHILE (I <= X) DO
SET A = ARRAY[];
SET J = 1;
WHILE (J <= Y) DO
SET A[J] = VAL;
SET J = J + 1;
END WHILE;
SET M[I] = A;
SET I = I + 1;
END WHILE;
END P_INIT_INTEGER_MATRIX
@
 
/**
* Prints the content of the matrix to the standard output.
*
* INOUT M: Matrix.
*/
CREATE OR REPLACE PROCEDURE PRINT_INTEGER_MATRIX(
IN M INTEGER_MATRIX)
P_PRINT_INTEGER_MATRIX: BEGIN
DECLARE I SMALLINT DEFAULT 1;
DECLARE J SMALLINT;
DECLARE X SMALLINT;
DECLARE Y SMALLINT;
DECLARE VAL INTEGER;
DECLARE A INTEGER_ARRAY;
DECLARE RET VARCHAR(256);
SET X = CARDINALITY(M);
CALL DBMS_OUTPUT.PUT_LINE('>>>>>');
WHILE (I <= X) DO
SET A = M[I];
SET RET = '[';
SET Y = CARDINALITY(A);
SET J = 1;
WHILE (J <= Y) DO
SET VAL = A[J];
SET RET = RET || VAL;
SET J = J + 1;
IF (J <= Y) THEN
SET RET = RET || ',';
END IF;
END WHILE;
SET RET = RET || ']';
CALL DBMS_OUTPUT.PUT_LINE(RET);
SET I = I + 1;
END WHILE;
CALL DBMS_OUTPUT.PUT_LINE('<<<<<');
END P_PRINT_INTEGER_MATRIX
@
 
/**
* Checks if a queen is safe in the given position.
*
* IN M: Matrix representing the chessboard.
* IN ROW: Row of the queen.
* IN COL: Column in the row for the queen.
* IN SIZE: Size of the chessboard (max row, max col).
* RETURNS true if the position is safe.
*/
CREATE OR REPLACE FUNCTION IS_SAFE(
IN M INTEGER_MATRIX,
IN ROW SMALLINT,
IN COL SMALLINT,
IN SIZE SMALLINT)
MODIFIES SQL DATA
RETURNS BOOLEAN
F_IS_SAFE: BEGIN
DECLARE I SMALLINT;
DECLARE J SMALLINT;
DECLARE VAL INTEGER;
-- Debug purposes.
--CALL SET_INTEGER_VALUE(ROW, COL, M, -1);
--CALL PRINT_INTEGER_MATRIX(M);
--CALL SET_INTEGER_VALUE(ROW, COL, M, 0);
 
SET I = 1;
WHILE (I <= COL) DO
SET VAL = GET_INTEGER_VALUE(ROW, I, M);
IF (VAL = 1) THEN
RETURN FALSE;
END IF;
SET I = I + 1;
END WHILE;
SET I = ROW;
SET J = COL;
WHILE (I >= 1 AND J >= 1) DO
SET VAL = GET_INTEGER_VALUE(I, J, M);
IF (VAL = 1) THEN
CALL SET_INTEGER_VALUE(ROW, COL, M, 0);
RETURN FALSE;
END IF;
SET I = I - 1;
SET J = J - 1;
END WHILE;
 
SET I = ROW;
SET J = COL;
WHILE (J >= 1 AND I <= SIZE) DO
SET VAL = GET_INTEGER_VALUE(I, J, M);
IF (VAL = 1) THEN
RETURN FALSE;
END IF;
SET I = I + 1;
SET J = J - 1;
END WHILE;
 
RETURN TRUE;
END F_IS_SAFE
@
 
/**
* Dummy procedure for the recurssion.
*
* IN SIZE: Size of the chessboard (max row, max col).
* IN COL: Column to analyse.
* OUT RET: True if it was possible to put all queens
*/
CREATE OR REPLACE PROCEDURE SOLVE_N_QUEENS(
INOUT M INTEGER_MATRIX,
IN SIZE SMALLINT,
IN COL SMALLINT,
OUT RET BOOLEAN)
P_SOLVE_N_QUEENS: BEGIN
END P_SOLVE_N_QUEENS
@
 
/**
* Solves the n-queens algoritm.
*
* IN SIZE: Size of the chessboard (max row, max col).
* IN COL: Column to analyse.
* OUT RET: True if it was possible to put all queens
*/
CREATE OR REPLACE PROCEDURE SOLVE_N_QUEENS(
INOUT M INTEGER_MATRIX,
IN SIZE SMALLINT,
IN COL SMALLINT,
OUT RET BOOLEAN)
MODIFIES SQL DATA
P_SOLVE_N_QUEENS: BEGIN
DECLARE I SMALLINT;
DECLARE SAFE BOOLEAN;
DECLARE SOLVED BOOLEAN;
 
-- Debug purposes.
--CALL PRINT_INTEGER_MATRIX(M);
SET RET = FALSE;
IF (COL > SIZE) THEN
SET RET = TRUE;
ELSE
SET I = 1;
WHILE (I <= SIZE AND NOT RET) DO
SET SAFE = IS_SAFE(M, I, COL, SIZE);
IF (SAFE) THEN
CALL SET_INTEGER_VALUE(I, COL, M, 1);
CALL SOLVE_N_QUEENS(M, SIZE, COL + 1, SOLVED);
IF (SOLVED) THEN
SET RET = TRUE;
ELSE
CALL SET_INTEGER_VALUE(I, COL, M, 0); -- Backtrack.
END IF;
END IF;
SET I = I + 1;
END WHILE;
 
END IF;
END P_SOLVE_N_QUEENS
@
 
/**
* Main procedure to solve the n-queen algoritm.
*
* IN SIZE: Size of the chessboard. The bigger it is, the more time it takes.
*/
CREATE OR REPLACE PROCEDURE N_QUEENS(
IN SIZE SMALLINT)
P_N_QUEENS: BEGIN
DECLARE M INTEGER_MATRIX;
DECLARE SOL BOOLEAN DEFAULT FALSE;
CALL INIT_INTEGER_MATRIX(M, SIZE, SIZE, 0);
CALL SOLVE_N_QUEENS(M, SIZE, 1, SOL);
IF (SOL = TRUE) THEN
CALL PRINT_INTEGER_MATRIX(M);
ELSE
CALL DBMS_OUTPUT.PUT_LINE('Solution does not exist.');
END IF;
END P_N_QUEENS
@
 
--#SET TERMINATOR ;
 
-- Activates the standard output for the current session.
SET SERVEROUTPUT ON;
 
CALL N_QUEENS(4);
 
CALL N_QUEENS(8);
 
CALL N_QUEENS(16);
 
 
</syntaxhighlight>
Output:
<pre>
db2 -td@
db2 => CREATE TYPE INTEGER_ARRAY AS INTEGER ARRAY[]@
DB20000I The SQL command completed successfully.
...
quit@
db2 -t
db2 => SET SERVEROUTPUT ON;
DB20000I The SET SERVEROUTPUT command completed successfully.
db2 => CALL N_QUEENS(4);
 
Return Status = 0
 
>>>>>
[0,0,1,0]
[1,0,0,0]
[0,0,0,1]
[0,1,0,0]
<<<<<
db2 => CALL N_QUEENS(8);
 
Return Status = 0
 
>>>>>
[1,0,0,0,0,0,0,0]
[0,0,0,0,0,0,1,0]
[0,0,0,0,1,0,0,0]
[0,0,0,0,0,0,0,1]
[0,1,0,0,0,0,0,0]
[0,0,0,1,0,0,0,0]
[0,0,0,0,0,1,0,0]
[0,0,1,0,0,0,0,0]
<<<<<
</pre>
 
=={{header|Standard ML}}==
This implementation uses failure continuations for backtracking.
<syntaxhighlight lang="standard ml">
<lang Standard ML>
(*
* val threat : (int * int) -> (int * int) -> bool
Line 8,054 ⟶ 16,158:
(* NONE *)
queens(2);
</syntaxhighlight>
</lang>
 
=={{header|Stata}}==
=== Iterative version ===
Adapted from the Fortran 77 program, to illustrate the '''[http://www.stata.com/help.cgi?m2_goto goto]''' statement in Stata.
 
<syntaxhighlight lang="stata">mata
real matrix queens(real scalar n) {
real scalar i, j, k, p, q
real rowvector a, s, u, v
real matrix m
m = J(0, n, .)
a = 1..n
s = J(1, n, 0)
u = J(1, 2*n-1, 1)
v = J(1, 2*n-1, 1)
i = 1
L1: if (i > n) {
m = m\a
goto L4
}
j=i
L2: k = a[j]
p = i-k+n
q = i+k-1
if (u[p] & v[q]) {
u[p] = v[q] = 0
a[j] = a[i]
a[i] = k
s[i++] = j
goto L1
}
L3: if (++j <= n) goto L2
L4: if (--i == 0) return(m)
j = s[i]
k = a[i]
a[i] = a[j]
a[j] = k
p = i-k+n
q = i+k-1
u[p] = v[q] = 1
goto L3
}
 
a = queens(8)
e = I(8)
1:/e[a[1,.],.]
1 2 3 4 5 6 7 8
+---------------------------------+
1 | 1 . . . . . . . |
2 | . . . . 1 . . . |
3 | . . . . . . . 1 |
4 | . . . . . 1 . . |
5 | . . 1 . . . . . |
6 | . . . . . . 1 . |
7 | . 1 . . . . . . |
8 | . . . 1 . . . . |
+---------------------------------+
 
 
rows(a)
92
end</syntaxhighlight>
 
It's also possible to save the solutions to a Stata dataset:
 
<syntaxhighlight lang="stata">clear
mata: a=queens(8)
getmata (a*)=a
save queens, replace</syntaxhighlight>
 
=== Recursive version ===
 
The recursive solution is adapted from one of the Python programs.
 
<syntaxhighlight lang="stata">mata
real matrix queens_rec(real scalar n) {
real rowvector a, u, v
real matrix m
a = 1..n
u = J(1, 2*n-1, 1)
v = J(1, 2*n-1, 1)
m = J(0, n, .)
queens_aux(n, 1, a, u, v, m)
return(m)
}
 
void queens_aux(real scalar n, real scalar i, real rowvector a,
real rowvector u, real rowvector v, real matrix m) {
real scalar j, k
if (i > n) {
m = m\a
} else {
for (j = i; j <= n; j++) {
k = a[j]
p = i-k+n
q = i+k-1
if (u[p] & v[q]) {
u[p] = v[q] = 0
a[j] = a[i]
a[i] = k
queens_aux(n, i+1, a, u, v, m)
u[p] = v[q] = 1
a[i] = a[j]
a[j] = k
}
}
}
}
end</syntaxhighlight>
 
The iterative and the recursive programs are equivalent:
 
<syntaxhighlight lang="stata">queens(8) == queens_rec(8)
1</syntaxhighlight>
 
=={{header|Swift}}==
Port of the optimized C code above
<syntaxhighlight lang="swift">
let maxn = 31
 
func nq(n: Int) -> Int {
var cols = Array(repeating: 0, count: maxn)
var diagl = Array(repeating: 0, count: maxn)
var diagr = Array(repeating: 0, count: maxn)
var posibs = Array(repeating: 0, count: maxn)
var num = 0
for q0 in 0...n-3 {
for q1 in q0+2...n-1 {
let bit0: Int = 1<<q0
let bit1: Int = 1<<q1
var d: Int = 0
cols[0] = bit0 | bit1 | (-1<<n)
diagl[0] = (bit0<<1|bit1)<<1
diagr[0] = (bit0>>1|bit1)>>1
 
var posib: Int = ~(cols[0] | diagl[0] | diagr[0])
 
while (d >= 0) {
while(posib != 0) {
let bit: Int = posib & -posib
let ncols: Int = cols[d] | bit
let ndiagl: Int = (diagl[d] | bit) << 1;
let ndiagr: Int = (diagr[d] | bit) >> 1;
let nposib: Int = ~(ncols | ndiagl | ndiagr);
posib^=bit
num += (ncols == -1 ? 1 : 0)
if (nposib != 0){
if(posib != 0) {
posibs[d] = posib
d += 1
}
cols[d] = ncols
diagl[d] = ndiagl
diagr[d] = ndiagr
posib = nposib
}
}
d -= 1
posib = d<0 ? n : posibs[d]
 
}
}
 
}
return num*2
}
if(CommandLine.arguments.count == 2) {
 
let board_size: Int = Int(CommandLine.arguments[1])!
print ("Number of solutions for board size \(board_size) is: \(nq(n:board_size))")
 
} else {
print("Usage: 8q <n>")
}
 
</syntaxhighlight>
 
=={{header|SystemVerilog}}==
Create a random board configuration, with the 8-queens as a constraint
<langsyntaxhighlight SystemVeriloglang="systemverilog">program N_queens;
 
parameter SIZE_LOG2 = 3;
Line 8,095 ⟶ 16,378:
 
endprogram
</syntaxhighlight>
</lang>
 
=={{header|Tailspin}}==
A functional-ish solution utilising tailspin's data flows
<syntaxhighlight lang="tailspin">
templates queens
def n: $;
templates addColumn
def prev: $;
templates addIfPossible
def row: $;
def minor: $ - $prev::length - 1;
def major: $ + $prev::length + 1;
// If prev is not an array that contains row, send it on...
$prev -> \(when <~[<=$row>]> do $ !\)
-> \(when <?($ -> \[i]($ - $i !\) <~[<=$minor>]>)> do $ !\)
-> \(when <?($ -> \[i]($ + $i !\) <~[<=$major>]>)> do $ !\)
-> [ $..., $row] !
end addIfPossible
1..$n -> addIfPossible !
end addColumn
1..$n -> [$] -> #
when <[]($n)> do $ !
otherwise $ -> addColumn -> #
end queens
 
def solutions: [ 8 -> queens ];
'For 8 queens there are $solutions::length; solutions
' -> !OUT::write
 
def columns: ['abcdefgh'...];
'One of them is $solutions(1) -> \[i]('$columns($i);$;' !\);
' -> !OUT::write
 
'For 3 queens there are $:[3 -> queens] -> $::length; solutions
' -> !OUT::write
</syntaxhighlight>
{{out}}
<pre>
For 8 queens there are 92 solutions
One of them is [a1, b5, c8, d6, e3, f7, g2, h4]
For 3 queens there are 0 solutions
</pre>
 
A solution using state to find one solution if any exist
<syntaxhighlight lang="tailspin">
templates queens
def n: $;
templates getRowColumn
when <?($@queens.freeRows($.r::raw) <=0>)> do 0 !
when <?($@queens.freeMaxs($.r::raw + $.c::raw) <=0>)> do 0 !
when <?($@queens.freeMins($.c::raw - $.r::raw + $n) <=0>)> do 0 !
otherwise 1!
end getRowColumn
 
sink setRowColumn
def p: $;
@queens.freeRows($p.r::raw): $p.val::raw;
@queens.freeMaxs($p.c::raw + $p.r::raw): $p.val::raw;
@queens.freeMins($p.c::raw - $p.r::raw + $n): $p.val::raw;
end setRowColumn
 
data done <=1>
 
templates placeQueen
def c: $;
row´1 -> #
when <done> do 1!
when <=row´($n+1)> do 0 !
when <?({r: $, c: $c} -> getRowColumn <=1>)> do
def r: $;
@queens.queenRows($r::raw): $c;
{r: $, c: $c, val: 0} -> !setRowColumn
$c -> \(<=col´$n> done´1!
<?(col´($c::raw + 1) -> placeQueen <=1>)> done´1!
<>
{r: $r, c: $c, val: 1} -> !setRowColumn
row´($r::raw + 1) !\) -> #
otherwise row´($::raw + 1) -> #
end placeQueen
 
@: { freeRows: [1..$n -> 1],
freeMaxs: [1..$n*2 -> 1],
freeMins: [1..$n*2 -> 1],
queenRows: [1..$n -> -1] };
col´1 -> placeQueen -> \(<=1> $@queens.queenRows ! <> 'non-existent'!\)!
end queens
 
'A solution to the 8 queens problem is $:8 -> queens;
' -> !OUT::write
'A solution to the 4 queens problem is $:4 -> queens;
' -> !OUT::write
'A solution to the 3 queens problem is $:3 -> queens;
' -> !OUT::write
</syntaxhighlight>
{{out}}
<pre>
A solution to the 8 queens problem is [1, 7, 5, 8, 2, 4, 6, 3]
A solution to the 4 queens problem is [3, 1, 4, 2]
A solution to the 3 queens problem is non-existent
</pre>
 
=={{header|Tcl}}==
Line 8,101 ⟶ 16,484:
 
{{works with|Tcl|8.5}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
proc unsafe {y} {
Line 8,147 ⟶ 16,530:
}
 
main [expr {$argc ? int(0+[lindex $argv 0]) : 8}]</langsyntaxhighlight>
{{out}}
<pre>$ tclsh8.5 8queens.tcl 6
Line 8,185 ⟶ 16,568:
|_|_|_|Q|_|_|
|_|Q|_|_|_|_|</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bash}}
The total number of solutions for 8 queens is displayed at the end of the run. The code could be adapted to display a selected solution or multiple solutions. This code runs anywhere you can get bash to run.
 
<syntaxhighlight lang="bash">#!/bin/bash
# variable declaration
typeset -i BoardSize=8
typeset -i p=0
typeset -i total=0
typeset -i board
# initialization
function init
{
for (( i=0;i<$BoardSize;i++ ))
do
(( board[$i]=-1 ))
done
}
# check if queen can be placed
function place
{
typeset -i flag=1
for (( i=0;i<$1;i++ ))
do
if [[ (${board[$i]}-${board[$1]} -eq ${i}-${1}) || (${board[$i]}-${board[$1]} -eq ${1}-${i}) || (${board[$i]} -eq ${board[$1]}) ]]
then
(( flag=0 ))
fi
done
[[ $flag -eq 0 ]]
return $?
}
# print the result
function out
{
printf "Problem of queen %d:%d\n" $BoardSize $total
}
# free the variables
function depose
{
unset p
unset total
unset board
unset BoardSize
}
# back tracing
function work
{
while [[ $p -gt -1 ]]
do
(( board[$p]++ ))
if [[ ${board[$p]} -ge ${BoardSize} ]]
then # back tracing
(( p-- ))
else # try next position
place $p
if [[ $? -eq 1 ]]
then
(( p++ ))
if [[ $p -ge ${BoardSize} ]]
then
(( total++ ))
(( p-- ))
else
(( board[$p]=-1 ))
fi
fi
fi
done
}
# entry
init
work
out
depose</syntaxhighlight>
 
=={{header|Ursala}}==
Line 8,190 ⟶ 16,656:
n is a number greater than 3. Multiple solutions may be reported
but reflections and rotations thereof are omitted.
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
 
Line 8,207 ⟶ 16,673:
-<&l^|*DlrTS/~& ~&iiDlSzyCK9hlPNNXXtCS,
^jrX/~& @rZK20lrpblPOlrEkPK13lhPK2 ~&i&& nleq$-&lh+-,
^/~&NNXS+iota -<&l+ ~&plll2llr2lrPrNCCCCNXS*=irSxPSp+ ^H/block iota; *iiK0 ^/~& sum+-</langsyntaxhighlight>
The output shows one solution on each line.
A solution is reported as a sequence of <math>n</math> numbers
Line 8,221 ⟶ 16,687:
$ queens 6
4 3 0 2 1 5
</pre>
 
=={{header|VBA}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="vb">'N-queens problem - non recursive & structured - vba - 26/02/2017
Sub n_queens()
Const l = 15 'number of queens
Const b = False 'print option
Dim a(l), s(l), u(4 * l - 2)
Dim n, m, i, j, p, q, r, k, t, z
For i = 1 To UBound(a): a(i) = i: Next i
For n = 1 To l
m = 0
i = 1
j = 0
r = 2 * n - 1
Do
i = i - 1
j = j + 1
p = 0
q = -r
Do
i = i + 1
u(p) = 1
u(q + r) = 1
z = a(j): a(j) = a(i): a(i) = z 'Swap a(i), a(j)
p = i - a(i) + n
q = i + a(i) - 1
s(i) = j
j = i + 1
Loop Until j > n Or u(p) Or u(q + r)
If u(p) = 0 Then
If u(q + r) = 0 Then
m = m + 1 'm: number of solutions
If b Then
Debug.Print "n="; n; "m="; m
For k = 1 To n
For t = 1 To n
Debug.Print IIf(a(n - k + 1) = t, "Q", ".");
Next t
Debug.Print
Next k
End If
End If
End If
j = s(i)
Do While j >= n And i <> 0
Do
z = a(j): a(j) = a(i): a(i) = z 'Swap a(i), a(j)
j = j - 1
Loop Until j < i
i = i - 1
p = i - a(i) + n
q = i + a(i) - 1
j = s(i)
u(p) = 0
u(q + r) = 0
Loop
Loop Until i = 0
Debug.Print n, m 'number of queens, number of solutions
Next n
End Sub 'n_queens</syntaxhighlight>
{{out}}
<pre>
1 1
2 0
3 0
4 2
5 10
6 4
7 40
8 92
9 352
10 724
11 2680
12 14200
13 73712
14 365596
15 2279184
</pre>
 
=={{header|VBScript}}==
{{trans|BBC BASIC}}
To have the solutions printed (raw format) uncomment the ad hoc statement.
<syntaxhighlight lang="vb">'N-queens problem - non recursive & structured - vbs - 24/02/2017
const l=15
dim a(),s(),u(): redim a(l),s(l),u(4*l-2)
for i=1 to l: a(i)=i: next
for n=1 to l
m=0
i=1
j=0
r=2*n-1
Do
i=i-1
j=j+1
p=0
q=-r
Do
i=i+1
u(p)=1
u(q+r)=1
z=a(j): a(j)=a(i): a(i)=z 'swap a(i),a(j)
p=i-a(i)+n
q=i+a(i)-1
s(i)=j
j=i+1
Loop Until j>n Or u(p)<>0 Or u(q+r)<>0
If u(p)=0 Then
If u(q+r)=0 Then
m=m+1 'm: number of solutions
'x="": for k=1 to n: x=x&" "&a(k): next: msgbox x,,m
End If
End If
j=s(i)
Do While j>=n And i<>0
Do
z=a(j): a(j)=a(i): a(i)=z 'swap a(i),a(j)
j=j-1
Loop Until j<i
i=i-1
p=i-a(i)+n
q=i+a(i)-1
j=s(i)
u(p)=0
u(q+r)=0
Loop
Loop Until i=0
wscript.echo n &":"& m
next 'n</syntaxhighlight>
{{out}}
<pre>
1 : 1
2 : 0
3 : 0
4 : 2
5 : 10
6 : 4
7 : 40
8 : 92
9 : 352
10 : 724
11 : 2680
12 : 14200
13 : 73712
14 : 365596
15 : 2279184
</pre>
 
=={{header|Visual Basic}}==
{{works with|Visual Basic|VB6 Standard}}
{{trans|BBC BASIC}}
<syntaxhighlight lang="vb">'N-queens problem - non recursive & structured - vb6 - 25/02/2017
Sub n_queens()
Const l = 15 'number of queens
Const b = False 'print option
Dim a(l), s(l), u(4 * l - 2)
Dim n, m, i, j, p, q, r, k, t, z
For i = 1 To UBound(a): a(i) = i: Next i
For n = 1 To l
m = 0
i = 1
j = 0
r = 2 * n - 1
Do
i = i - 1
j = j + 1
p = 0
q = -r
Do
i = i + 1
u(p) = 1
u(q + r) = 1
z = a(j): a(j) = a(i): a(i) = z 'Swap a(i), a(j)
p = i - a(i) + n
q = i + a(i) - 1
s(i) = j
j = i + 1
Loop Until j > n Or u(p) Or u(q + r)
If u(p) = 0 Then
If u(q + r) = 0 Then
m = m + 1 'm: number of solutions
If b Then
Debug.Print "n="; n; "m="; m
For k = 1 To n
For t = 1 To n
Debug.Print IIf(a(n - k + 1) = t, "Q", ".");
Next t
Debug.Print
Next k
End If
End If
End If
j = s(i)
Do While j >= n And i <> 0
Do
z = a(j): a(j) = a(i): a(i) = z 'Swap a(i), a(j)
j = j - 1
Loop Until j < i
i = i - 1
p = i - a(i) + n
q = i + a(i) - 1
j = s(i)
u(p) = 0
u(q + r) = 0
Loop
Loop Until i = 0
Debug.Print n, m 'number of queens, number of solutions
Next n
End Sub 'n_queens</syntaxhighlight>
{{out}}
<pre>
1 1
2 0
3 0
4 2
5 10
6 4
7 40
8 92
9 352
10 724
11 2680
12 14200
13 73712
14 365596
15 2279184
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="vb">'N-queens problem - non recursive & structured - vb.net - 26/02/2017
Module Mod_n_queens
Sub n_queens()
Const l = 15 'number of queens
Const b = False 'print option
Dim a(l), s(l), u(4 * l - 2)
Dim n, m, i, j, p, q, r, k, t, z
Dim w As String
For i = 1 To UBound(a) : a(i) = i : Next i
For n = 1 To l
m = 0
i = 1
j = 0
r = 2 * n - 1
Do
i = i - 1
j = j + 1
p = 0
q = -r
Do
i = i + 1
u(p) = 1
u(q + r) = 1
z = a(j) : a(j) = a(i) : a(i) = z 'Swap a(i), a(j)
p = i - a(i) + n
q = i + a(i) - 1
s(i) = j
j = i + 1
Loop Until j > n Or u(p) Or u(q + r)
If u(p) = 0 Then
If u(q + r) = 0 Then
m = m + 1 'm: number of solutions
If b Then
Debug.Print("n=" & n & " m=" & m) : w = ""
For k = 1 To n
For t = 1 To n
w = w & If(a(n - k + 1) = t, "Q", ".")
Next t
Debug.Print(w)
Next k
End If
End If
End If
j = s(i)
Do While j >= n And i <> 0
Do
z = a(j) : a(j) = a(i) : a(i) = z 'Swap a(i), a(j)
j = j - 1
Loop Until j < i
i = i - 1
p = i - a(i) + n
q = i + a(i) - 1
j = s(i)
u(p) = 0
u(q + r) = 0
Loop
Loop Until i = 0
Debug.Print(n & vbTab & m) 'number of queens, number of solutions
Next n
End Sub 'n_queens
End Module</syntaxhighlight>
{{out}}
<pre>
1 1
2 0
3 0
4 2
5 10
6 4
7 40
8 92
9 352
10 724
11 2680
12 14200
13 73712
14 365596
15 2279184
</pre>
 
=={{header|Wart}}==
<syntaxhighlight lang="wart">def (nqueens n queens)
prn "step: " queens # show progress
if (len.queens = n)
prn "solution! " queens
# else
let row (if queens (queens.zero.zero + 1) 0)
for col 0 (col < n) ++col
let new_queens (cons (list row col) queens)
if (no conflicts.new_queens)
(nqueens n new_queens)
 
# check if the first queen in 'queens' lies on the same column or diagonal as
# any of the others
def (conflicts queens)
let (curr ... rest) queens
or (let curr_column curr.1
(some (fn(_) (= _ curr_column))
(map cadr rest))) # columns
(some (fn(_) (diagonal_match curr _))
rest)
 
def (diagonal_match curr other)
(= (abs (curr.0 - other.0))
(abs (curr.1 - other.1)))</syntaxhighlight>
 
=={{header|Wren}}==
{{trans|Kotlin}}
Very slow for the larger boards.
<syntaxhighlight lang="wren">var count = 0
var c = []
var f = []
 
var nQueens // recursive
nQueens = Fn.new { |row, n|
for (x in 1..n) {
var outer = false
var y = 1
while (y < row) {
if ((c[y] == x) || (row - y == (x -c[y]).abs)) {
outer = true
break
}
y = y + 1
}
if (!outer) {
c[row] = x
if (row < n) {
nQueens.call(row + 1, n)
} else {
count = count + 1
if (count == 1) f = c.skip(1).map { |i| i - 1 }.toList
}
}
}
}
 
for (n in 1..14) {
count = 0
c = List.filled(n+1, 0)
f = []
nQueens.call(1, n)
System.print("For a %(n) x %(n) board:")
System.print(" Solutions = %(count)")
if (count > 0) System.print(" First is %(f)")
System.print()
}</syntaxhighlight>
 
{{out}}
<pre>
For a 1 x 1 board:
Solutions = 1
First is [0]
 
For a 2 x 2 board:
Solutions = 0
 
For a 3 x 3 board:
Solutions = 0
 
For a 4 x 4 board:
Solutions = 2
First is [1, 3, 0, 2]
 
For a 5 x 5 board:
Solutions = 10
First is [0, 2, 4, 1, 3]
 
For a 6 x 6 board:
Solutions = 4
First is [1, 3, 5, 0, 2, 4]
 
For a 7 x 7 board:
Solutions = 40
First is [0, 2, 4, 6, 1, 3, 5]
 
For a 8 x 8 board:
Solutions = 92
First is [0, 4, 7, 5, 2, 6, 1, 3]
 
For a 9 x 9 board:
Solutions = 352
First is [0, 2, 5, 7, 1, 3, 8, 6, 4]
 
For a 10 x 10 board:
Solutions = 724
First is [0, 2, 5, 7, 9, 4, 8, 1, 3, 6]
 
For a 11 x 11 board:
Solutions = 2680
First is [0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9]
 
For a 12 x 12 board:
Solutions = 14200
First is [0, 2, 4, 7, 9, 11, 5, 10, 1, 6, 8, 3]
 
For a 13 x 13 board:
Solutions = 73712
First is [0, 2, 4, 1, 8, 11, 9, 12, 3, 5, 7, 10, 6]
 
For a 14 x 14 board:
Solutions = 365596
First is [0, 2, 4, 6, 11, 9, 12, 3, 13, 8, 1, 5, 7, 10]
</pre>
 
Line 8,226 ⟶ 17,126:
 
Copied from http://www.cs.bu.edu/~hwxi/Xanadu/Examples/
<syntaxhighlight lang="xanadu">
<lang Xanadu>
int abs(i: int) {
if (i >= 0) return i; else return -i;
Line 8,295 ⟶ 17,195:
int main () {
return queen (8);
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
[[File:NQueensXPL0.GIF|right]]
<syntaxhighlight lang="xpl0">def N=8; \board size (NxN)
int R, C; \row and column of board
char B(N,N); \board
include c:\cxpl\codes;
 
proc Try; \Try adding a queen to the board
int R; \row, for each level of recursion
 
func Okay;
\Returns 'true' if no row, column, or diagonal from square R,C has a queen
int I;
[for I:= 0 to N-1 do
[if B(I,C) then return false; \row is occupied
if B(R,I) then return false; \column is occupied
if R+I<N & C+I<N then
if B(R+I, C+I) then return false; \diagonal down right
if R-I>=0 & C-I>=0 then
if B(R-I, C-I) then return false; \diagonal up left
if R-I>=0 & C+I<N then
if B(R-I, C+I) then return false; \diagonal up right
if R+I<N & C-I>=0 then
if B(R+I, C-I) then return false; \diagonal down left
];
return true;
]; \Okay
 
[ \Try
if C>=N then
[for R:= 0 to N-1 do \display solution
[ChOut(0, ^ ); \(avoids scrolling up a color)
for C:= 0 to N-1 do
[Attrib(if (R|C)&1 then $0F else $4F); \checkerboard pattern
ChOut(6, if B(R,C) then $F2 else ^ ); \cute queen symbol
ChOut(6, if B(R,C) then $F3 else ^ );
];
CrLf(0);
];
exit; \one solution is enough
];
for R:= 0 to N-1 do
[if Okay(R,C) then \a queen can be placed here
[B(R,C):= true; \ so do it
C:= C+1; \move to next column
Try; \ and try from there
C:= C-1; \didn't work: backup
B(R,C):= false; \undo queen placement
];
];
]; \Try
 
 
[for R:= 0 to N-1 do \clear the board
for C:= 0 to N-1 do
B(R,C):= false;
C:= 0; \start at left column
Try;
]</syntaxhighlight>
 
=={{header|XSLT}}==
Line 8,301 ⟶ 17,261:
(either by XSLT processors saxon-6.5.5, xsltproc, xalan,
or any of the big5 browsers):
<langpre>
15863724
16837425
Line 8,307 ⟶ 17,267:
83162574
84136275
</langpre>
 
You can view the results directly in your browser (Chrome/FF/IE/Opera/Safari) here: [[http://stamm-wilbrandt.de/en/xsl-list/n-queens/8-queens.xsl.xml]]
Line 8,326 ⟶ 17,286:
 
Here is stylesheet 8-queens.xsl.xml which produces the (simple) output by having itself as input: [[http://stamm-wilbrandt.de/en/xsl-list/n-queens/8-queens.xsl.xml]]
<langsyntaxhighlight lang="xml">
<!-- 8-queens.xsl disguised as XML file for the browsers -->
 
Line 8,453 ⟶ 17,413:
 
</xsl:stylesheet>
</syntaxhighlight>
</lang>
 
=={{header|XPL0Yabasic}}==
<syntaxhighlight lang="yabasic">DOCU The N Queens Problem:
[[File:NQueensXPL0.GIF|right]]
<langDOCU XPL0>defPlace N Queens on an N=8;NxN chess \board size (NxN)
DOCU such that they don't threaten each other.
int R, C; \row and column of board
char B(N,N); \board
include c:\cxpl\codes;
 
N = 8 // try some other sizes
proc Try; \Try adding a queen to the board
int R; \row, for each level of recursion
 
sub threat(q1r, q1c, q2r, q2c)
func Okay;
// do two queens threaten each other?
\Returns 'true' if no row, column, or diagonal from square R,C has a queen
int I;
if [forq1c I:= 0q2c tothen N-1 do return true
elsif (q1r - q1c) = [if(q2r B(I,C- q2c) then return false; \row is occupiedtrue
elsif (q1r + q1c) = if(q2r B(R,I+ q2c) then return false; \column is occupiedtrue
elsif q1r = q2r then if R+I<N & C+I<Nreturn thentrue
if B(R+I, C+I) thenelse return false; \diagonal down right
end if
if R-I>=0 & C-I>=0 then
end sub
if B(R-I, C-I) then return false; \diagonal up left
if R-I>=0 & C+I<N then
if B(R-I, C+I) then return false; \diagonal up right
if R+I<N & C-I>=0 then
if B(R+I, C-I) then return false; \diagonal down left
];
return true;
]; \Okay
 
sub conflict(r, c, queens$)
[ \Try
// Would square p cause a conflict with other queens on board so far?
if C>=N then
local r2, c2
[for R:= 0 to N-1 do \display solution
[ChOut(0, ^ ); \(avoids scrolling up a color)
for C:= 0 to N-1 do
[Attrib(if (R|C)&1 then $0F else $4F); \checkerboard pattern
ChOut(6, if B(R,C) then $F2 else ^ ); \cute queen symbol
ChOut(6, if B(R,C) then $F3 else ^ );
];
CrLf(0);
];
exit; \one solution is enough
];
for R:= 0 to N-1 do
[if Okay(R,C) then \a queen can be placed here
[B(R,C):= true; \ so do it
C:= C+1; \move to next column
Try; \ and try from there
C:= C-1; \didn't work: backup
B(R,C):= false; \undo queen placement
];
];
]; \Try
 
for i = 1 to len(queens$) step 2
r2 = val(mid$(queens$,i,1))
c2 = val(mid$(queens$,i+1,1))
if threat(r, c, r2, c2) then
return true
end if
next i
return false
end sub
 
sub print_board(queens$)
// print a solution, showing the Queens on the board
local k$
 
print at(1, 1);
print "Solution #", soln, "\n\n ";
for c = asc("a") to (asc("a") + N - 1)
print chr$(c)," ";
next c
print
for r = 1 to N
print r using "##"," ";
for c = 1 to N
pos = instr(queens$, (str$(r)+str$(c)))
if pos and mod(pos, 2) then
queens$ = mid$(queens$,pos)
print "Q ";
else
print ". ";
end if
next c
print
next r
print "\nPress Enter. (q to quit) "
while(true)
k$ = inkey$
if lower$(k$) = "q" then
exit
elsif k$ = "enter" then
break
end if
wend
end sub
</syntaxhighlight>
 
=={{header|Zig}}==
Outputs all 92 solutions.
<syntaxhighlight lang="zig">
const std = @import("std");
const stdout = std.io.getStdOut().outStream();
 
var board = [_]i8{-1} ** 8;
 
inline fn abs(x: var) @TypeOf(x) {
return if (x < 0) -x else x;
}
 
fn safe(c: i32, r: i32) bool {
var i: i32 = 0;
return while (i < c) : (i += 1) {
const q = board[@intCast(u3, i)];
if (r == q or c == i + abs(q - r))
break false;
} else true;
}
 
pub fn main() !void {
var i: i32 = 0;
while (i >= 0) {
var j = board[@intCast(u3, i)] + 1;
while (j < 8) : (j += 1) {
if (safe(i, j)) {
board[@intCast(u3, i)] = j;
i += 1;
break;
}
} else {
board[@intCast(u3, i)] = -1;
i -= 1;
}
if (i == 8) { // found a solution
for (board) |q|
try stdout.print("{} ", .{q + 1});
try stdout.print("\n", .{});
i -= 1; // create a failure to try new solutions.
}
}
}
</syntaxhighlight>
{{Out}}
<pre>
$ zig run 8q.zig | head -n 4
1 5 8 6 3 7 2 4
1 6 8 3 7 4 2 5
1 7 4 6 8 2 5 3
1 7 5 8 2 4 6 3
</pre>
 
[for R:= 0 to N-1 do \clear the board
for C:= 0 to N-1 do
B(R,C):= false;
C:= 0; \start at left column
Try;
]</lang>
 
=={{header|zkl}}==
Modified from a Haskell version (if I remember correctly)
<langsyntaxhighlight lang="zkl">fcn isAttacked([(q, x,y)],a,b) // explode list( (r,c), x,y ) into: argsis xqueen &at r,c attacked by q@(x,y)?
{ r,c:=q; (xr==ax or yc==by or xr+yc==ax+by or xr-yc==ax-by) }
fcn isSafe(ar,bc,qs){ // queen safe at (notr,c)?, qs.filter1=(isAttacked (r,ac),(r,bc)..)} // stop atsolution firstso attackfar
{ ( not qs.filter1(isAttacked,r,c) ) }
fcn queensN(N=8,row=1,queens=T){ // T is read only list
fcn queensN(N=8,row=1,queens=T){
qs := [1..N].filter(isSafe.fpM("101",row,queens)) // fpM makes r&q first & third args
qs:=[1..N].filter(isSafe.fpM("101",row,queens)) #isSafe(row,?,( (r,c),(r,c).. )
.apply(fcn(c,r,qs){qs+T(r,c)},row,queens);
.apply(fcn(c,r,qs){ qs.append(T(r,c)) },row,queens);
if (row == N) return(qs);
return(qs.apply(self.fcn.fp(N,row+1)).flatten()); // recurse
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">queens := queensN(4);
println(queens.len()," solution(s):");
queens.apply2(Console.println);</syntaxhighlight>
{{out}}
--> 2 solution(s):
<pre>
2 solution(s):
L(L(1,2),L(2,4),L(3,1),L(4,3))
L(L(1,3),L(2,1),L(3,4),L(4,2))</lang>
</pre>
 
[[Category:Puzzles]]
305

edits