N-queens problem: Difference between revisions

m
→‎Python: Backtracking on permutations: Restored the original look
m (→‎Python: Backtracking on permutations: Restored the original look)
(171 intermediate revisions by 51 users not shown)
Line 1:
{{taskTask}}
[[File:chess_queen.jpg|400px||right]]
 
Line 9:
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://oeis.org/A000170|OEIS: oeis.org sequence A170A000170]].
 
 
Line 23:
<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}}==
Line 28 ⟶ 92:
Translated from the Fortran 77 solution.<br>
For maximum compatibility, this program uses only the basic instruction set (S/360).
<langsyntaxhighlight lang="360asm">* N-QUEENS PROBLEM 04/09/2015
MACRO
&LAB XDECO &REG,&TARGET
Line 169 ⟶ 233:
U DC (4*LL-2)H'0' stack
REGS make sure to include copybook jcl
END NQUEENS</langsyntaxhighlight>
{{out}}
<pre>
Line 188 ⟶ 252:
</pre>
 
=={{header|PDP-116502 Assembly}}==
{{trans|Java}}
<lang PDP-11 Assembly>
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.
; "eight queens problem" benchmark test
 
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.
.radix 16
 
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.
.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
</lang>
 
<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 593 ⟶ 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 645 ⟶ 713:
end loop;
Put_Line (" A B C D E F G H");
end Queens;</langsyntaxhighlight>
{{out}}
<pre>
Line 664 ⟶ 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 712 ⟶ 780:
Put_Line (Long_Integer'Image (Queens (N)));
end loop;
end CountQueens;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 722 ⟶ 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 772 ⟶ 840:
FI
OD
)</langsyntaxhighlight>
 
=={{header|Dyalog APL}}==
{{works with|Dyalog APL}}
More or less copied from the "DFS" lesson on tryapl.org .
<syntaxhighlight lang="apl">
<lang APL>
⍝Solution
accm←{⍺,((⍴⍵)=⍴⊃⍺)↑⊂⍵}
Line 788 ⟶ 857:
⍝Example
printqueens 6
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 822 ⟶ 891:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">-- Finds all possible solutions and the unique patterns.
 
property Grid_Size : 8
Line 985 ⟶ 1,054:
return newRows
end Reflect</langsyntaxhighlight>
 
=={{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:
<langsyntaxhighlight Lisplang="lisp">(def nqueens (n (o queens))
(if (< len.queens n)
(let row (if queens (+ 1 queens.0.0) 0)
Line 1,008 ⟶ 1,086:
(def diagonal-match (curr other)
(is (abs (- curr.0 other.0))
(abs (- curr.1 other.1))))</langsyntaxhighlight>
{{out}}
The output is one solution per line, each solution in the form `((row col) (row col) (row col) ...)`:
Line 1,014 ⟶ 1,092:
((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 1,097 ⟶ 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 1,181 ⟶ 1,414:
Output .= "|`n" , yyy++
}
}</langsyntaxhighlight>
=== Includes a solution browser GUI ===
This implementation supports N = 4..12 queens, and will find ALL solutions
Line 1,187 ⟶ 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 1,276 ⟶ 1,509:
 
GuiClose:
ExitApp</langsyntaxhighlight>
[[image:N-Queens_SolutionBrowserGUI.png]]
 
Line 1,285 ⟶ 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 1,345 ⟶ 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,378 ⟶ 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,384 ⟶ 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,519 ⟶ 1,752:
i, n, all)
}
</syntaxhighlight>
</lang>
 
=={{header|Befunge}}==
Line 1,525 ⟶ 1,758:
This algorithm works with any board size from 4 upwards.
 
<langsyntaxhighlight 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-</langsyntaxhighlight>
 
{{out}}
Line 1,553 ⟶ 1,786:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( printBoard
= board M L x y S R row line
. :?board
Line 1,613 ⟶ 1,846:
| out$(found !solutions solutions)
)
);</langsyntaxhighlight>
{{out}} (tail):
<pre>
Line 1,658 ⟶ 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,688 ⟶ 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,727 ⟶ 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,819 ⟶ 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,892 ⟶ 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,998 ⟶ 2,365:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}Output for N = 4:
<pre> a b c d
Line 2,012 ⟶ 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 2,270 ⟶ 2,637:
std::cout << queens( N ) << "\n";
}
</syntaxhighlight>
</lang>
{{out}} for N=4:
<pre>
Line 2,291 ⟶ 2,658:
=== Alternate version ===
Windows-only
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <iostream>
Line 2,392 ⟶ 2,759:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,421 ⟶ 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,510 ⟶ 2,877:
}
//--------------------------------------------------------------------------------------------------
</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 -->
<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; }
}
}</lang>
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 ...)
<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; }
}
}</lang>
=== 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 -->
<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();
}
}
}</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,670 ⟶ 2,903:
(println s))
 
(println (count solutions) "solutions")</langsyntaxhighlight>
===Short Version===
<langsyntaxhighlight 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))))) </langsyntaxhighlight>
===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,792 ⟶ 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,814 ⟶ 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,857 ⟶ 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,881 ⟶ 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,944 ⟶ 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,983 ⟶ 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 3,047 ⟶ 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 3,067 ⟶ 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 3,080 ⟶ 4,400:
n.iota.map!(i => p[i] - i).array.sort().uniq.count == n)
.count.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>92</pre>
Line 3,087 ⟶ 4,407:
This version shows all the solutions.
{{trans|C}}
<langsyntaxhighlight lang="d">enum side = 8;
__gshared int[side] board;
 
Line 3,130 ⟶ 4,450:
y--;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,167 ⟶ 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 3,256 ⟶ 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 3,265 ⟶ 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 3,329 ⟶ 4,649:
void main() {
enumerate(4);
}</langsyntaxhighlight>
{{out}}
<pre>* Q * *
Line 3,341 ⟶ 4,661:
* Q * *
</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program N_queens_problem;
 
{$APPTYPE CONSOLE}
=={{header|EasyLang}}==
 
uses
<lang>n = 8
System.SysUtils;
len queen_row[] n
 
#
var
func print_solution . .
h$i: = "┏"Integer;
q: boolean;
for i range n - 1
a: array[0..8] of boolean;
h$ &= "━━━┳"
b: array[0..16] of boolean;
.
c: array[0..14] of boolean;
h$ &= "━━━┓"
x: array[0..8] of Integer;
pr h$
 
h$ = "┣"
procedure TryMove(i: Integer);
for i range n - 1
begin
h$ &= "━━━╋"
var j := 1;
.
while True do
h$ &= "━━━┫"
begin
for i range n
s$q := "┃"false;
if a[j] and b[i + j] and c[i - j + 7] then
for j range n
begin
if j = queen_row[i]
x[i] s$ &:= " Q ┃"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
s$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 ""
pr s$.
print ""
if i = n - 1
.
h$ = "┗"
subr test
for j range n - 1
ok = 1
h$ &= "━━━┻"
for i = 1 to y - 1
if x[y] = x[i] or abs (x[i] - x[y]) = abs (y - i)
ok = 0
.
.
h$ &= "━━━┛"
.
pr h$
.
pr ""
.
n = 8
#
len x[] n
func test_free col row . free .
freey = 1
x[1] = 1
while i < row and free = 1
while y >= 1
if queen_row[i] = col or abs (queen_row[i] - col) = abs (row - i)
free = 0test
if ok = 1 and y + 1 <= n
.
i y += 1
x[y] = 1
.
else
.
if ok = 1
#
n_sol += 1
pr "First solution: "
if n_sol <= 1
pr ""
show_sol
n_solutions = 0
func try_row row . .
if row = n
n_solutions += 1
if n_solutions <= 1
call print_solution
.
else
for col range n
call test_free col row free
if free = 1
queen_row[row] = col
call try_row row + 1
.
while y >= 1 and x[y] = n
.
y -= 1
.
.
if y >= 1
x[y] += 1
.
.
.
print n_sol & " solutions"
call try_row 0
</syntaxhighlight>
pr "Number of solutions: " & n_solutions</lang>
{{out}}
<pre>First solution:Solution 1
 
Q . . . . . . .
┏━━━┳━━━┳━━━┳━━━┳━━━┳━━━┳━━━┳━━━┓
. . . . Q . . .
┃ Q ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃
. . . . . . . Q
┣━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━┫
. . . . . Q . .
┃ ┃ ┃ ┃ ┃ Q ┃ ┃ ┃ ┃
. . Q . . . . .
┣━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━┫
. . . . . . Q .
┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ Q ┃
. Q . . . . . .
┣━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━┫
. . ┃ ┃ ┃ ┃. Q . . . .
┣━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━┫
┃ ┃ ┃ Q ┃ ┃ ┃ ┃ ┃ ┃
┣━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━┫
┃ ┃ ┃ ┃ ┃ ┃ ┃ Q ┃ ┃
┣━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━┫
┃ ┃ Q ┃ ┃ ┃ ┃ ┃ ┃ ┃
┣━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━┫
┃ ┃ ┃ ┃ Q ┃ ┃ ┃ ┃ ┃
┗━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┛
 
Number of92 solutions: 92</pre>
</pre>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; square num is i + j*N
(define-syntax-rule (sq i j) (+ i (* j N)))
Line 3,495 ⟶ 4,919:
(define (task up-to-n)
(for ((i up-to-n)) (writeln i ' ♕ (q-count i) 'solutions)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,515 ⟶ 4,939:
</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 3,586 ⟶ 5,180:
end
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,622 ⟶ 5,216:
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule RC do
def queen(n, display \\ true) do
solve(n, [], [], [], display)
Line 3,659 ⟶ 5,253:
Enum.each(7..12, fn n ->
IO.puts " #{n} Queen : #{RC.queen(n, false)}" # no display
end)</langsyntaxhighlight>
 
{{out}}
Line 3,792 ⟶ 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,880 ⟶ 5,564:
Board = solve( N ),
display( Board ).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,899 ⟶ 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,986 ⟶ 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}}==
<langsyntaxhighlight lang="fsharp">
let rec iterate f value = seq {
yield value
Line 4,033 ⟶ 5,735:
 
printNumberOfSolutions()
</syntaxhighlight>
</lang>
 
The output:
 
<langpre>
| | | |X| | | | | |
| |X| | | | | | | |
Line 4,059 ⟶ 5,761:
10 724
11 2680
</langpre>
 
=={{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 4,069 ⟶ 5,772:
:: safe? ( board q -- ? )
[let q board nth :> x
q <iota> [
x swap
[ board nth ] keep
Line 4,079 ⟶ 5,782:
 
: solution? ( board -- ? )
dup length <iota> [ dupd safe? ] all? nip ;
 
: queens ( n -- l )
<iota> all-permutations [ solution? ] filter ;
 
: .queens ( n -- )
Line 4,088 ⟶ 5,791:
[
[ 1 + "%d " printf ] each nl
] each ;</langsyntaxhighlight>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/N-queens_problem this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">variable solutions
variable nodes
 
Line 4,127 ⟶ 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 4,133 ⟶ 5,881:
 
Using a back tracking method to find one solution
<langsyntaxhighlight lang="fortran">program Nqueens
implicit none
 
Line 4,233 ⟶ 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 4,357 ⟶ 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 4,429 ⟶ 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 4,483 ⟶ 6,231:
print *, n, m
end do
end program</langsyntaxhighlight>
 
===Alternate Fortran 95 solution with OpenMP===
Line 4,492 ⟶ 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 4,617 ⟶ 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
<langsyntaxhighlight lang="freebasic">' version 13-04-2017
' compile with: fbc -s console
Dim Shared As ULong count, c()
Line 4,675 ⟶ 6,747:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> 1 3 5 2 4
Line 4,706 ⟶ 6,778:
=== Alternate version : recursive ===
 
<langsyntaxhighlight lang="freebasic">Sub aux(n As Integer, i As Integer, a() As Integer, _
u() As Integer, v() As Integer, ByRef m As LongInt)
 
Line 4,746 ⟶ 6,818:
aux(n, 1, a(), u(), v(), m)
Print m
End If</langsyntaxhighlight>
 
=== Alternate version : iterative ===
 
<langsyntaxhighlight lang="freebasic">Dim As Integer n, i, j, k, p, q
Dim m As LongInt = 0
 
Line 4,792 ⟶ 6,864:
u(p) = 1 : v(q) = 1
Goto L3
End If</langsyntaxhighlight>
 
=={{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 4,798 ⟶ 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 4,875 ⟶ 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 4,885 ⟶ 7,000:
// Data Structures = Programs."
package main
 
import "fmt"
 
var (
i int
Line 4,896 ⟶ 7,011:
x [9]int
)
 
func try(i int) {
for j := 1; ; j++ {
Line 4,921 ⟶ 7,036:
}
}
 
func main() {
for i := 1; i <= 8; i++ {
Line 4,938 ⟶ 7,053:
}
}
}</langsyntaxhighlight>
 
{{out}}
<pre>
Line 4,949 ⟶ 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>
 
Line 4,958 ⟶ 7,212:
[[N-queens_problem/dlx_go|dlx packge]].
 
<langsyntaxhighlight Golang="go">package main
 
import (
Line 5,093 ⟶ 7,347:
}
return nil
}</langsyntaxhighlight>
{{out}}
<pre>
Line 5,127 ⟶ 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 5,150 ⟶ 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 5,204 ⟶ 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 5,215 ⟶ 7,469:
else { println "first:${qus[0]}"; println "last:${qus[-1]}" }
println()
}</langsyntaxhighlight>
 
Interpreting the Results:
Line 5,229 ⟶ 7,483:
 
In other words, this:
<pre>|///| Q |///| ---- ---- ---- ---- |
3 | |////| ♛ |////|
--- --- --- ---
---- ---- ---- ----
| |///| |/Q/|
2 |/♛/| |////| |
--- --- --- ---
---- ---- ---- ----
|/Q/| |///| |
1 | |////| |/♛/|
--- --- --- ---
---- ---- ---- ----
| |///| Q |///|</pre>
0 |////| ♛ |////| |
---- ---- ---- ----
0 1 2 3</pre>
 
Results:
Line 5,282 ⟶ 7,539:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad
import Data.List
 
Line 5,311 ⟶ 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 5,326 ⟶ 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 5,341 ⟶ 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}}
<langsyntaxhighlight lang="haskell">import Data.List (transposeintercalate, intercalatetranspose)
 
import Data.Bool (bool)
--------------------- N QUEENS PROBLEM -------------------
 
queenPuzzle :: Int -> Int -> [[Int]]
Line 5,353 ⟶ 7,611:
| nRows <= 0 = [[]]
| otherwise =
foldr
(\qsx ay -> y <> foldr (go x) [] [1 .. nCols])
a ++[]
$ queenPuzzle foldr(pred nRows) nCols
where
(\iCol b -> bool b (b ++ [qs ++ [iCol]]) (safe (nRows - 1) iCol qs))
go qs iCol []b
| safe (nRows - 1) iCol qs = b <> [1qs ..<> nCols[iCol]])
[]| otherwise = b
(queenPuzzle (nRows - 1) nCols)
 
safe :: Int -> Int -> [Int] -> Bool
safe iRow iCol qs =
(not . or) $
zipWith
( \sc sr ->
(iCol == sc) || (sc + sr == (iCol + iRow)) || (sc - sr == (iCol - iRow)))
|| (sc - sr == (iCol - iRow))
qs
[0 .. iRow - 1])
qs
[0 .. iRow - 1]
 
-- TEST -------------------------- TEST -------------------------
-- 10 columns of solutions for the 7*7 board:
showSolutions :: Int -> Int -> [String]
showSolutions nCols nSize =
unlines <$>
(( . fmap (intercalate " ") . transpose . fmap boardLines) <$>
. transpose
chunksOf nCols (queenPuzzle nSize nSize))
. map boardLines
<$> chunksOf nCols (queenPuzzle nSize nSize)
where
go r x
| r == x = '♛'
| otherwise = '.'
boardLines rows =
(\r[ -> (bool '.' '♛' . (==go r)) <$> [1 .. (length rows)]) <$> rows
| r <- rows
]
 
chunksOf :: Int -> [a] -> [[a]]
chunksOf i xs = take i <$> ($ (:)) (splits xs) []
where
splits [] _ n = []
splits l c= ntake =i l `c`: splits (drop i l) c n
 
main :: IO ()
main = (putStrLn . unlines) $ showSolutions 10 7</langsyntaxhighlight>
{{Out}}
<pre>......♛ ......♛ ......♛ ......♛ .....♛. .....♛. .....♛. .....♛. .....♛. .....♛.
Line 5,425 ⟶ 7,691:
 
===Breadth-first search and Depth-first search===
<langsyntaxhighlight lang="haskell">import Control.Monad
import System.Environment
 
Line 5,467 ⟶ 7,733:
-- | Breadth-first search
bfs :: Int -> [(State, Thread)] -> (State, Thread)
bfs n [] = error "Couldn'tCould not find a feasible solution"
bfs n sts | (not.empty) goal = head goal
| otherwise = bfs n sts'sts2
where
goal = filter (isGoal n) sts'sts2
sts'sts2 = filter (feasible n) $ (moves n) <*> sts
 
-- | Depth-first search
Line 5,479 ⟶ 7,745:
| infeasible n st = [emptySt]
| otherwise = do x <- [1..n]
st'st2 <- dfs n $ move x st
guard $ st'st2 /= emptySt
return st'st2
 
main = do
Line 5,487 ⟶ 7,753:
let n = read narg :: Int
print (bfs n [emptySt])
print (head $ dfs n emptySt)</langsyntaxhighlight>
 
{{Out}}
Line 5,494 ⟶ 7,760:
 
=={{header|Heron}}==
<langsyntaxhighlight lang="heron">module NQueens {
inherits {
Heron.Windows.Console;
Line 5,588 ⟶ 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 5,607 ⟶ 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 5,618 ⟶ 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 5,630 ⟶ 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 5,664 ⟶ 7,930:
write()
return # Comment out to see all possible solutions
end</langsyntaxhighlight>
 
A sample run for <tt>N = 6</tt>:
Line 5,688 ⟶ 7,954:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "NQueens.bas"
110 TEXT 80
120 DO
Line 5,725 ⟶ 7,991:
450 LET T(I)=1
460 NEXT
470 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
Line 5,731 ⟶ 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: 1981 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.
Line 5,740 ⟶ 8,006:
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 5,798 ⟶ 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.
<langsyntaxhighlight lang="javascript">function queenPuzzle(rows, columns) {
if (rows <= 0) {
return [[]];
Line 5,835 ⟶ 8,101:
}
 
console.log(queenPuzzle(8,8));</langsyntaxhighlight>
 
===ES6===
Translating the ES5 version, and adding a function to display columns of solutions.
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'"use strict'";
 
// N QUEENS PROBLEM ------------------------------------- N QUEENS PROBLEM -----------------
 
// queenPuzzle :: Int -> Int -> [[Int]]
const queenPuzzle = (nRows, nCols)intCols => {
nRows// <=All 0solutions ?for [a given number
// of columns and []rows.
]const :go = queenPuzzle(nRows - 1, nCols)=>
.reduce((a, solution) nRows <=> 0 ? [
append(a, enumFromTo(0, nCols - 1)[]
] : go(nRows - 1).reduce((b, iCol) =>
safe(nRows - 1, iCola, solution) ?=> ([
...a, b.concat([solution.concat.(iCol)])
) : b, [] 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) => !any(
!zip([sc, sr]solution) =>(
(iCol === sc) || enumFromTo(sc + sr === iCol + iRow0) || (sciRow - sr === iCol - iRow1),
)
zip(solution, enumFromTo(0, iRow - 1))
); .some(
([sc, sr]) => (iCol === sc) || (
sc + sr === iCol + iRow
) || (sc - sr === iCol - iRow)
);
 
// GENERIC FUNCTIONS ------------------------------ TEST -----------------------
// Ten columns of solutions to the 7*7 board
 
// absmain :: NumIO a => a -> a()
const absmain = Math.abs() =>
// eslint-disable-next-line no-console
console.log(
showSolutions(10)(7)
);
 
// --------------------- DISPLAY ---------------------
// any :: (a -> Bool) -> [a] -> Bool
const any = (f, xs) => xs.some(f);
 
// (++)showSolutions :: [a]Int -> [a]Int -> [a]String
const appendshowSolutions = (xs, ys)nCols => xs.concat(ys);
// 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");
 
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = (n, xs) =>
xs.reduce((a, _, i, xs) =>
i % n ? a : a.concat([xs.slice(i, i + n)]), []);
 
// ---------------- GENERIC FUNCTIONS ----------------
// concat :: [[a]] -> [a] | [String] -> String
const concat = xs => {
if (xs.length > 0) {
const unit = typeof xs[0] === 'string' ? '' : [];
return unit.concat.apply(unit, xs);
} else return [];
};
 
// concatMapchunksOf :: (aInt -> [ba]) -> [[a] -> [b]
const concatMapchunksOf = (f, xs)n => [].concat.apply([], xs.map(f));{
// 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) ? [
// 2 or more arguments
chunk, ...go(xs.slice(n))
// curry :: Function -> Function
const curry = (f, ...args) => { ] : [];
};
const go = xs => xs.length >= f.length ? (f.apply(null, xs)) :
 
function () {
return go(xs.concat([].slice.apply(arguments)));
};
return go([].slice.call(args, 1));
};
 
 
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
n => Array.from({
length: Math.floor(1 + n - m) + 1
}, (_, i) => m + i);
 
// intercalate :: String -> [a] -> String
const intercalate = curry((s, xs) => xs.join(s));
 
// maptranspose_ :: ([[a -> b)]] -> [[a] -> [b]
const maptranspose = (f, xs)rows => xs.map(f)
// 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]
)
) : [];
 
// transpose :: [[a]] -> [[a]]
const transpose = xs =>
xs[0].map((_, iCol) => xs.map(row => row[iCol]));
 
// unlineszip :: [Stringa] -> String[b] -> [(a, b)]
const unlineszip = xs => xs.join('\n');
// 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]]);
 
// zip :: [a]MAIN -> [b] -> [(a,b)]-
const zip =return main(xs, ys) =>;
})();</syntaxhighlight>
xs.slice(0, Math.min(xs.length, ys.length))
{{Out}}
.map((x, i) => [x, ys[i]]);
<pre>....... ....... ....... ....... ♛...... ♛...... ♛...... ♛...... ♛...... ♛......
.♛..... ..♛.... ...♛... ....♛.. ..♛.... ..♛.... ...♛... ...♛... ...♛... ....♛..
...♛... .....♛. ♛...... ..♛.... ....... ....♛.. ....... .♛..... .....♛. .♛.....
.....♛. .♛..... ....♛.. ♛...... .....♛. ....... ..♛.... ....... ..♛.... .....♛.
♛...... ....♛.. .♛..... .....♛. ...♛... .♛..... .....♛. .....♛. ....... ..♛....
..♛.... ♛...... .....♛. ...♛... .♛..... ...♛... .♛..... ..♛.... .♛..... .......
....♛.. ...♛... ..♛.... .♛..... ....♛.. .....♛. ....♛.. ....♛.. ....♛.. ...♛...
 
♛...... .♛..... .♛..... .♛..... .♛..... .♛..... .♛..... ..♛.... ..♛.... ..♛....
// TEST ------------------------------------------------------------------
.....♛. ....... ....... ...♛... ....♛.. .....♛. .....♛. ....... ....... ♛......
// Ten columns of solutions to the 7*7 board
...♛... ....♛.. ....♛.. .....♛. ♛...... ♛...... ..♛.... .♛..... ...♛... .....♛.
.♛..... ♛...... ..♛.... ♛...... ...♛... ..♛.... ....... ....♛.. ♛...... ...♛...
....... ...♛... ♛...... ..♛.... ....... ....♛.. ...♛... ♛...... ....♛.. .♛.....
....♛.. .....♛. .....♛. ....♛.. ..♛.... ....... ♛...... .....♛. .♛..... .......
..♛.... ..♛.... ...♛... ....... .....♛. ...♛... ....♛.. ...♛... .....♛. ....♛..
 
..♛.... ..♛.... ..♛.... ...♛... ...♛... ...♛... ...♛... ...♛... ...♛... ....♛..
// showSolutions :: Int -> Int -> String
....♛.. .....♛. .....♛. ....... ....... ♛...... .♛..... .....♛. .....♛. .......
const showSolutions = (nCols, nBoardSize) =>
....... .♛..... ...♛... ..♛.... ....♛.. ....♛.. ....... ♛...... ♛...... .♛.....
intercalate('\n\n', map(unlines,
.♛..... ....♛.. ♛...... .....♛. ..♛.... .♛..... ....♛.. ..♛.... ....♛.. ...♛...
map(col => map(intercalate(" "), transpose(map(rows =>
...♛... ♛...... ....♛.. .♛..... ♛...... .....♛. ..♛.... ....♛.. .♛..... .....♛.
map(r => concat(concatMap(c =>
.....♛. ...♛... ....... ....♛.. .....♛. ..♛.... ♛...... ....... ....... ♛......
c === r ? '♛' : '.',
♛...... ....... .♛..... ♛...... .♛..... ....... .....♛. .♛..... ..♛.... ..♛....
enumFromTo(1, rows.length))), rows), col))),
chunksOf(nCols, queenPuzzle(nBoardSize, nBoardSize))
)));
 
....♛.. ....♛.. ....♛.. ....♛.. ....♛.. ....♛.. .....♛. .....♛. .....♛. .....♛.
return showSolutions(10, 7);
♛...... .♛..... .♛..... .♛..... ..♛.... ..♛.... ♛...... .♛..... ..♛.... ...♛...
})();</lang>
...♛... ....... ...♛... .....♛. ♛...... .....♛. ..♛.... ....♛.. ....... .♛.....
....... ..♛.... .....♛. ..♛.... .....♛. ....... ....♛.. ♛...... ...♛... .......
..♛.... .....♛. ....... ....... ...♛... .♛..... ....... ...♛... ♛...... ....♛..
.....♛. ...♛... ..♛.... ...♛... .♛..... ...♛... .♛..... ....... ....♛.. ..♛....
.♛..... ♛...... ♛...... ♛...... ....... ♛...... ...♛... ..♛.... .♛..... ♛......</pre>
 
=={{header|jq}}==
Line 5,946 ⟶ 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 5,970 ⟶ 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 5,980 ⟶ 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 5,996 ⟶ 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 6,014 ⟶ 8,340:
 
count( permutations(n) | select(allowable) );
</syntaxhighlight>
</lang>
'''Example''':
<syntaxhighlight lang ="jq">queens(8)</langsyntaxhighlight>
{{out}}
92
Line 6,022 ⟶ 8,348:
=={{header|Julia}}==
 
<langsyntaxhighlight lang="ruby">"""
#!/usr/bin/env julia
 
__precompile__(true)
 
"""
# EightQueensPuzzle
 
Line 6,035 ⟶ 8,356:
module EightQueensPuzzle
 
export mainBoard, solve!
 
typemutable struct Board
cols::Int
nodes::Int
Line 6,049 ⟶ 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 6,082 ⟶ 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 6,121 ⟶ 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}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
var count = 0
Line 6,204 ⟶ 8,517:
println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 6,265 ⟶ 8,578:
=={{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 6,351 ⟶ 8,664:
End Function
 
</syntaxhighlight>
</lang>
 
=={{header|Locomotive Basic}}==
Line 6,357 ⟶ 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 6,408 ⟶ 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 6,414 ⟶ 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 6,430 ⟶ 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 6,467 ⟶ 8,780:
else
print(string.format("No solution for %d queens.\n", N))
end</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
{{trans|VBA}}
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module N_queens {
Const l = 15 'number of queens
Line 6,530 ⟶ 8,843:
}
N_queens
</syntaxhighlight>
</lang>
 
=={{header|Mathematicam4}}==
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 6,542 ⟶ 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 6,566 ⟶ 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 6,573 ⟶ 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 6,622 ⟶ 9,102:
 
solveNqueens[8] // dispSol
</syntaxhighlight>
</lang>
<pre>- - - - Q - - -
- Q - - - - - -
Line 6,631 ⟶ 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 6,650 ⟶ 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 6,712 ⟶ 10,433:
Quit
Do Queens
</syntaxhighlight>
</lang>
<div style="overflow:scroll; height:400px;">
<syntaxhighlight lang="mumps">
<lang MUMPS>
+--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+
8 | |##| Q|##| |##| |##| | |##| Q|##| |##| |##| | |##| | Q| |##| |##|
Line 6,922 ⟶ 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 6,934 ⟶ 10,655:
return false
 
proc solve(n: int): autoseq[seq[int]] =
result = newSeq[seq[int]]()
result.add(@[])
Line 6,940 ⟶ 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 6,946 ⟶ 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 7,011 ⟶ 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 7,074 ⟶ 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 7,107 ⟶ 10,859:
else 8 in
 
solutions n</langsyntaxhighlight>
{{out}}
<pre>$ ocaml queens.ml 6
Line 7,141 ⟶ 10,893:
=={{header|Oz}}==
A pretty naive solution, using constraint programming:
<langsyntaxhighlight lang="oz">declare
fun {Queens N}
proc {$ Board}
Line 7,208 ⟶ 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 7,294 ⟶ 11,045:
14 365596
15 2279184
16 14772512 }</langsyntaxhighlight>
 
===Alternative===
Line 7,312 ⟶ 11,063:
Solution found</pre>
<langsyntaxhighlight lang="pascal">program NQueens;
{$IFDEF FPC}
{$MODE DELPHI}
Line 7,423 ⟶ 11,174:
end;
WriteLn('Fertig');
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 7,445 ⟶ 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 7,486 ⟶ 11,355:
 
#print for @solutions; # un-comment to see all solutions
print "total " . @solutions . " solutions\n";</langsyntaxhighlight>
{{out}}
<pre>total 14200 solutions</pre>
 
=={{header|Perl 6}}==
{{works with|rakudo|2015-11-29}}
Neither pretty nor efficient, a simple backtracking solution
 
<lang perl6>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;
}
}
}</lang>
{{out}}
<pre>[0 4 7 5 2 6 1 3]</pre>
 
=={{header|Phix}}==
<!--(phixonline)-->
<lang Phix>--
<syntaxhighlight lang="phix">
with javascript_semantics
--
-- demo\rosetta\n_queens.exw
-- =========================
Line 7,531 ⟶ 11,372:
bd, -- backward diagonals
board
 
atom count
 
procedure solve(integer row, integer N, integer show)
for col=1 to N do
Line 7,542 ⟶ 11,383:
and not bd[bdi] then
board[row][col] = 'Q'
co[col] = 1true
fd[fdi] = 1true
bd[bdi] = 1true
if row=N then
if show then
Line 7,555 ⟶ 11,396:
end if
board[row][col] = '.'
co[col] = 0false
fd[fdi] = 0false
bd[bdi] = 0false
end if
end if
end for
end procedure
 
procedure n_queens(integer N=8, integer show=1)
co = repeat(0false,N)
fd = repeat(0false,N*2-1)
bd = repeat(0false,N*2-1)
board = repeat(repeat('.',N),N)
count = 0
Line 7,572 ⟶ 11,413:
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</lang>
</syntaxhighlight>
{{out}}
<pre>
Line 7,605 ⟶ 11,447:
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
N=14 takes about 10s
 
=={{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 7,623 ⟶ 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 7,688 ⟶ 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 7,695 ⟶ 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 7,725 ⟶ 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 7,734 ⟶ 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 7,757 ⟶ 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 7,784 ⟶ 11,622:
&nbsp<input type="submit" value="Process" />
</form>
 
_END;
 
?>
</body>
</html></lang>
</syntaxhighlight>
 
<h2>Solution with recursion</h2>
=={{header|Picat}}==
<lang Picat>import cp.
 
<syntaxhighlight lang="php">
% CP approach
<html>
queens_cp(N, Q) =>
<body>
Q = new_list(N),
<pre>
Q :: 1..N,
<?php
 
/*************************************************************************
all_different(Q),
*
all_different([$Q[I]-I : I in 1..N]),
* This algorithm solves the 8 queens problem using backtracking.
all_different([$Q[I]+I : I in 1..N]),
* Please try with N<=25 * * * *************************************************************************/
solve([ff],Q).
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>
</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.
 
% SAT approach (using a N x N matrix)
queens_sat(N,Q) =>
Q = new_array(N,N),
Line 7,824 ⟶ 11,733:
sum([Q[I,J] : I in 1..N]) #= 1
end,
solve([inout],Q).</syntaxhighlight>
 
===Constraint programming===
</lang>
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) =>
Output:
Q = new_list(N),
<pre>
Q :: 1..N,
Picat> queens_cp(100, QCP)
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
Line 7,852 ⟶ 11,785:
13 = 73712
14 = 365596
15 = 2279184</pre>
 
===Comparison===
</pre>
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 7,868 ⟶ 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 7,888 ⟶ 11,828:
(mapcar - L R) )
(inc 'Cnt) ) ) )
Cnt ) )</langsyntaxhighlight>
{{out}} for both cases:
<pre>: (queens 8)
-> 92</pre>
 
=={{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;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">
NQUEENS: PROC OPTIONS (MAIN);
DCL A(35) BIN FIXED(31) EXTERNAL;
DCL COUNT BIN FIXED(31) EXTERNAL;
COUNT = 0;
DECLARE SYSIN FILE;
DCL ABS BUILTIN;
DECLARE SYSPRINT FILE;
DECLARE N BINARY FIXED (31); /* COUNTER */
/* MAIN LOOP STARTS HERE */
GET LIST (N) FILE(SYSIN); /* N QUEENS, N X N BOARD */
PUT SKIP (1) FILE(SYSPRINT);
PUT SKIP LIST('BEGIN N QUEENS PROCESSING *****') FILE(SYSPRINT);
PUT SKIP LIST('SOLUTIONS FOR N: ',N) FILE(SYSPRINT);
PUT SKIP (1) FILE(SYSPRINT);
IF N < 4 THEN DO;
/* LESS THAN 4 MAKES NO SENSE */
PUT SKIP (2) FILE(SYSPRINT);
PUT SKIP LIST (N,' N TOO LOW') FILE (SYSPRINT);
PUT SKIP (2) FILE(SYSPRINT);
RETURN (1);
END;
IF N > 35 THEN DO;
/* WOULD TAKE WEEKS */
PUT SKIP (2) FILE(SYSPRINT);
PUT SKIP LIST (N,' N TOO HIGH') FILE (SYSPRINT);
PUT SKIP (2) FILE(SYSPRINT);
RETURN (1);
END;
CALL QUEEN(N);
PUT SKIP (2) FILE(SYSPRINT);
PUT SKIP LIST (COUNT,' SOLUTIONS FOUND') FILE(SYSPRINT);
PUT SKIP (1) FILE(SYSPRINT);
PUT SKIP LIST ('END OF PROCESSING ****') FILE(SYSPRINT);
RETURN(0);
/* MAIN LOOP ENDS ABOVE */
PLACE: PROCEDURE (PS);
DCL PS BIN FIXED(31);
DCL I BIN FIXED(31) INIT(0);
DCL A(50) BIN FIXED(31) EXTERNAL;
DO I=1 TO PS-1;
IF A(I) = A(PS) THEN RETURN(0);
IF ABS ( A(I) - A(PS) ) = (PS-I) THEN RETURN(0);
END;
RETURN (1);
END PLACE;
 
QUEEN: PROCEDURE (N);
DCL N BIN FIXED (31);
DCL K BIN FIXED (31);
DCL A(50) BIN FIXED(31) EXTERNAL;
DCL COUNT BIN FIXED(31) EXTERNAL;
K = 1;
A(K) = 0;
DO WHILE (K > 0);
A(K) = A(K) + 1;
DO WHILE ( ( A(K)<= N) & (PLACE(K) =0) );
A(K) = A(K) +1;
END;
IF (A(K) <= N) THEN DO;
IF (K = N ) THEN DO;
COUNT = COUNT + 1;
END;
ELSE DO;
K= K +1;
A(K) = 0;
END; /* OF INSIDE ELSE */
END; /* OF FIRST IF */
ELSE DO;
K = K -1;
END;
END; /* OF EXTERNAL WHILE LOOP */
END QUEEN;
END NQUEENS; </syntaxhighlight>
 
=={{header|PowerBASIC}}==
=== Recursive version ===
{{trans|Stata}}
<langsyntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
 
Line 7,941 ⟶ 11,963:
PRINT m
END IF
END FUNCTION</langsyntaxhighlight>
 
=== Iterative version ===
{{trans|Stata}}
<langsyntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
 
Line 7,991 ⟶ 12,013:
GOTO 3
END IF
END FUNCTION</langsyntaxhighlight>
 
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function PlaceQueen ( [ref]$Board, $Row, $N )
{
Line 8,055 ⟶ 12,077:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
Get-NQueensBoard 8
''
Line 8,064 ⟶ 12,086:
''
Get-NQueensBoard 14
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 8,099 ⟶ 12,121:
</pre>
 
=={{header|PL/IProcessing}}==
{{trans|Java}}
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;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="java">
<lang pli>
int n = 8;
NQUEENS: PROC OPTIONS (MAIN);
int[] b = new int[n];
DCL A(35) BIN FIXED(31) EXTERNAL;
int s = 0;
DCL COUNT BIN FIXED(31) EXTERNAL;
int y = 0;
COUNT = 0;
 
DECLARE SYSIN FILE;
void setup() {
DCL ABS BUILTIN;
size(400, 400);
DECLARE SYSPRINT FILE;
textAlign(CENTER, CENTER);
DECLARE N BINARY FIXED (31); /* COUNTER */
textFont(createFont("DejaVu Sans", 44));
/* MAIN LOOP STARTS HERE */
b[0] = -1;
GET LIST (N) FILE(SYSIN); /* N QUEENS, N X N BOARD */
}
PUT SKIP (1) FILE(SYSPRINT);
PUT SKIP LIST('BEGIN N QUEENS PROCESSING *****') FILE(SYSPRINT);
PUT SKIP LIST('SOLUTIONS FOR N: ',N) FILE(SYSPRINT);
PUT SKIP (1) FILE(SYSPRINT);
IF N < 4 THEN DO;
/* LESS THAN 4 MAKES NO SENSE */
PUT SKIP (2) FILE(SYSPRINT);
PUT SKIP LIST (N,' N TOO LOW') FILE (SYSPRINT);
PUT SKIP (2) FILE(SYSPRINT);
RETURN (1);
END;
IF N > 35 THEN DO;
/* WOULD TAKE WEEKS */
PUT SKIP (2) FILE(SYSPRINT);
PUT SKIP LIST (N,' N TOO HIGH') FILE (SYSPRINT);
PUT SKIP (2) FILE(SYSPRINT);
RETURN (1);
END;
CALL QUEEN(N);
PUT SKIP (2) FILE(SYSPRINT);
PUT SKIP LIST (COUNT,' SOLUTIONS FOUND') FILE(SYSPRINT);
PUT SKIP (1) FILE(SYSPRINT);
PUT SKIP LIST ('END OF PROCESSING ****') FILE(SYSPRINT);
RETURN(0);
/* MAIN LOOP ENDS ABOVE */
PLACE: PROCEDURE (PS);
DCL PS BIN FIXED(31);
DCL I BIN FIXED(31) INIT(0);
DCL A(50) BIN FIXED(31) EXTERNAL;
DO I=1 TO PS-1;
IF A(I) = A(PS) THEN RETURN(0);
IF ABS ( A(I) - A(PS) ) = (PS-I) THEN RETURN(0);
END;
RETURN (1);
END PLACE;
 
void draw() {
QUEEN: PROCEDURE (N);
if (y >= 0) {
DCL N BIN FIXED (31);
do {
DCL K BIN FIXED (31);
b[y]++;
DCL A(50) BIN FIXED(31) EXTERNAL;
} while ((b[y] < n) && unsafe(y));
DCL COUNT BIN FIXED(31) EXTERNAL;
if (b[y] < n) {
K = 1;
if (y < (n-1)) {
A(K) = 0;
b[++y] = -1;
DO WHILE (K > 0);
} else {
A(K) = A(K) + 1;
drawBoard();
DO WHILE ( ( A(K)<= N) & (PLACE(K) =0) );
}
A(K) = A(K) +1;
} else {
END;
y--;
IF (A(K) <= N) THEN DO;
}
IF (K = N ) THEN DO;
} else {
COUNT = COUNT + 1;
textSize(18);
END;
text("Press any key to restart", width / 2, height - 20);
ELSE DO;
}
K= K +1;
}
A(K) = 0;
 
END; /* OF INSIDE ELSE */
 
END; /* OF FIRST IF */
boolean unsafe(int y) {
ELSE DO;
int x = b[y];
K = K -1;
for (int i = 1; i <= y; i++) {
END;
int t = b[y - i];
END; /* OF EXTERNAL WHILE LOOP */
if (t == x ||
END QUEEN;
t == x - i ||
t == x + i) {
END NQUEENS; </lang>
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 8,185 ⟶ 12,235:
 
Solution #1:
<langsyntaxhighlight Prologlang="prolog">solution([]).
solution([X/Y|Others]) :-
Line 8,205 ⟶ 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 8,235 ⟶ 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 8,257 ⟶ 12,307:
del(Item,[First|List],[First|List1]) :-
del(Item,List,List1).</langsyntaxhighlight>
 
[http://ideone.com/Y6olN Output]:
Line 8,265 ⟶ 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 8,280 ⟶ 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 8,286 ⟶ 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 8,308 ⟶ 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 8,321 ⟶ 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
<langsyntaxhighlight Prologlang="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).</langsyntaxhighlight>
{{out}}
<pre>
Line 8,334 ⟶ 12,385:
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>
 
Line 8,339 ⟶ 12,522:
From the Pure (programming language) Wikipedia page
 
<langsyntaxhighlight lang="pure">/*
n-queens.pure
Tectonics:
Line 8,357 ⟶ 12,540:
 
compiling || (puts("queens 4: " + str(queens 4)) $$
puts("Solutions to queens 7: " + str(#queens 7)));</langsyntaxhighlight>
 
{{out}}
Line 8,375 ⟶ 12,558:
=={{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 8,454 ⟶ 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 8,575 ⟶ 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 8,584 ⟶ 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 8,602 ⟶ 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 8,619 ⟶ 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 8,643 ⟶ 12,827:
answers = solve(BOARD_SIZE)
first_answer = next(answers)
print(list(enumerate(first_answer, start=1)))</langsyntaxhighlight>
 
===Python: SimpleNiklaus BacktrackingWirth Solution (functional style)algorithm===
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.
This simple version, which uses a generator function and lists, has an excellent performance with PyPy.
<langsyntaxhighlight lang="python">def solvequeens(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:
foryield solutionfrom in solvequeens(n, i + 1, a + [j], b + [i + j], c + [i - j]):
yield solution
else:
yield a
 
for solution in solve(8, 0, [], [], []):
print(solution)</lang>
 
for solution in queens(8, 0, [], [], []):
===Python: backtracking on permutations===
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}}
<langsyntaxhighlight Pythonlang="python">'''N Queens problem'''
 
from functools import reduce
Line 8,736 ⟶ 12,931:
 
# queenPuzzle :: Int -> Int -> [[Int]]
def queenPuzzle(nRows, nCols):
'''All board patterns of this dimension
in which no two Queens share a row,
column, or diagonal.
'''
def go(nRows, nCols):
lessRows = nRows - 1
return reduce(
lambda a, xys: a + reduce(
lambda b, iCol: b + [xys + [iCol]] if (
safe(nRows - 1lessRows, iCol, xys)
) else b,
enumFromTo(1)(nCols),
[]
),
go(nRows - 1, nColslessRows),
[]
) if nRows0 >< 0nRows else [[]]
return go(nRows, nCols)
 
 
Line 8,768 ⟶ 12,964:
 
 
# TEST --------------------------- TEST -------------------------
# main :: IO ()
def main():
Line 8,774 ⟶ 12,970:
 
n = 5
xs = queenPuzzle(n, )(n)
 
print(
Line 8,785 ⟶ 12,981:
'\n\n' + main.__doc__ + ':\n'
)(str)(lambda n: str(n).rjust(3, ' '))(
lambda n: len(queenPuzzle(n, )(n))
)(enumFromTo(1)(10))
)
 
 
# GENERIC --------------------------- FORMATTING ----------------------
 
# enumFromTo :: (Int, Int) -> [Int]
def enumFromTo(m):
'''Integer enumeration from m to n.'''
return lambda n: list(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 []
 
 
# FORMATTING ----------------------------------------------
 
# showBoards :: Int -> [[Int]] -> String
Line 8,836 ⟶ 12,999:
return '\n\n'.join(map(
showBlock,
chunksOf(nCols)([
list(map(showBoard,(b) for b in bs))
])
))
return lambda boards: go(boards)
 
 
Line 8,850 ⟶ 13,013:
def showLine(n):
return ('.' * (n - 1)) + '♛' + ('.' * (lng - n))
return list(map(showLine, xs))
 
 
Line 8,869 ⟶ 13,032:
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()</langsyntaxhighlight>
{{Out}}
<pre>10 solutions for a 5 * 5 board:
Line 8,895 ⟶ 13,091:
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">
<lang QB64>
DIM SHARED QUEENS AS INTEGER
PRINT "# of queens:";: INPUT QUEENS
Line 8,937 ⟶ 13,252:
NEXT icol
END SUB
</syntaxhighlight>
</lang>
 
=={{header|R}}==
<lang r># Brute force, see the "Permutations" page for the next.perm function
safe <- function(p) {
n <- length(p)
for (i in seq(1, n - 1)) {
for (j in seq(i + 1, n)) {
if (abs(p[j] - p[i]) == abs(j - i)) return(F)
}
}
return(T)
}
 
{{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 <- catseq(p, "\n")
u <- rep(T, 2 k* <-n k +- 1)
v <- rep(T, 2 * n - 1)
m <- NULL
aux <- function(i) {
if (i > n) {
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
}
}
}
p <- next.perm(p)
}
returnaux(k1)
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 8,973 ⟶ 13,320:
Backtracking algorithm; returns one solution
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 9,004 ⟶ 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 9,018 ⟶ 13,365:
 
(show-nqueens 8)
</syntaxhighlight>
</lang>
 
[[image:Racket-nqueens.png]]
Line 9,030 ⟶ 13,377:
Computes all solutions.
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 9,076 ⟶ 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 9,105 ⟶ 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 9,165 ⟶ 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 9,178 ⟶ 13,557:
result += vector;}
return result;
}</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 9,187 ⟶ 13,566:
 
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 an NxN chessboard (the eight 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 nOK then call nOK /*display a message, the board is bad. */
rank= 1; file= 1; #file=0 1; #= 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 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. */
call show
exit 1 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 9,223 ⟶ 13,602:
line = '╠'g"╣"; dither= "▓"; ditherQ= '░' /*define a line for cell boundary.*/
bar = '║' ; queen = "Q" /*kinds: horiz., vert., salad.*/
Bqueen = ditherQ || queen || ditherQ 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 /*square black ? */
QgylphB = Wqueen;(file + rank) // 2 if B then Qgylph= Bqueen /*useIs ditheredthe Qsquare black ? Then B=1.*/
if @.file.rank thenB _= _ || bar || Qgylph then Qgylph= Bqueen /*if black square, use /*3─chardithered Q symbol.*/
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 */
Line 9,234 ⟶ 13,615:
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)*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default of an &nbsp; '''8'''<small>x</small>'''8''' &nbsp; chessboard:}}
<pre>
Line 9,303 ⟶ 13,684:
╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
 
// Bert Mariani 2020-07-17
 
See "Enter value of N : " Give n // Ask User for Size of Board
n = 0 + n
x = 1:n
See "Possible placements: Value as Column of the Row "+ nl
 
nQueen(1,n) //===>>> START
 
See nl+ nl+"Enter to Exit program: " Give m // To Exit CMD window
 
//================================
// 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>
 
Enter value of N : 8
Possible placements: Value as Column of the Row
 
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
2 4 6 8 3 1 7 5
2 5 7 1 3 8 6 4
. . . .
8 2 5 3 1 7 4 6
8 3 1 6 2 5 7 4
8 4 1 3 6 2 7 5
 
Enter to Exit program:
 
</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 9,362 ⟶ 14,508:
end
(1 .. 15).each {|n| puts "n=#{n}"; puts n_queens(n); puts}</langsyntaxhighlight>
 
{{out}}
Line 9,516 ⟶ 14,662:
===Alternate solution===
If there is not specification, it outputs all solutions.
<langsyntaxhighlight lang="ruby">class Queen
attr_reader :count
Line 9,554 ⟶ 14,700:
puts @frame
end
end</langsyntaxhighlight>
 
'''Example:'''
<langsyntaxhighlight lang="ruby">(1..6).each do |n|
puzzle = Queen.new(n)
puts " #{n} Queen : #{puzzle.count}"
Line 9,565 ⟶ 14,711:
puzzle = Queen.new(n, false) # do not display
puts " #{n} Queen : #{puzzle.count}"
end</langsyntaxhighlight>
 
{{out}}
Line 9,701 ⟶ 14,847:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">[loop]
input "How many queens (N>=4)";n
if n < 4 then
Line 9,812 ⟶ 14,958:
end if
end if
next</langsyntaxhighlight>
<pre>abcdefgh
* 8
Line 9,824 ⟶ 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 9,856 ⟶ 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.
 
<lang rust>use std::collections::LinkedList;
<syntaxhighlight lang="rust">
use std::collections::LinkedList;
use std::iter::IntoIterator;
 
Line 9,869 ⟶ 15,017:
}
 
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 {
Line 9,888 ⟶ 15,036:
 
pub struct NQueens {
iterator: Box<Iterator<dyn Item=NQueensSolution>>
}
 
Line 9,936 ⟶ 15,084:
str
}
}
}</lang>
</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 9,996 ⟶ 15,183:
put n m;
keep p1-p8;
run;</langsyntaxhighlight>
 
=={{header|Scala}}==
Line 10,004 ⟶ 15,191:
Lazily generates permutations with an <code>Iterator</code>.
 
<langsyntaxhighlight lang="scala">
object NQueens {
 
Line 10,054 ⟶ 15,241:
}
}
</syntaxhighlight>
</lang>
 
<pre>
Line 10,085 ⟶ 15,272:
This is a simple breadth-first technique to retrieve all solutions.
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme write)
Line 10,160 ⟶ 15,347:
(pretty-print (n-queens 5) 5)
(pretty-print (n-queens 8) 8)
</syntaxhighlight>
</lang>
 
{{out}}
Line 10,234 ⟶ 15,421:
=={{header|Scilab}}==
Naive brute-force search.
<syntaxhighlight lang="scilab">//Length of board side
Board_size = 8;
 
Line 10,375 ⟶ 15,562:
string(Board_size)+"x"+string(Board_size)+" board.");
//Time elapsed
disp("Time: "+string(toc())+"s.");</langsyntaxhighlight>
 
{{out}}
Line 10,384 ⟶ 15,571:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
var array integer: board is 8 times 0;
Line 10,434 ⟶ 15,621:
end if;
end while;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="ruby">func N_queens_solution(N = 8) {
 
func collision(field, row) {
Line 10,468 ⟶ 15,655:
for n in (1..15) {
say "#{'%2d' % n}: #{N_queens_solution(n) || 'No solution'}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 10,489 ⟶ 15,676:
 
=={{header|SNOBOL4}}==
<syntaxhighlight lang="snobol4">
<lang SNOBOL4>
* N queens problem
* Set N to the desired number. The program prints out all solution boards.
Line 10,513 ⟶ 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 10,571 ⟶ 15,758:
};
 
stdout.printf("%d solutions\n", n_queens(range(8), 0));</langsyntaxhighlight>
 
=={{header|SQL}}==
Line 10,577 ⟶ 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 10,608 ⟶ 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 10,652 ⟶ 16,158:
(* NONE *)
queens(2);
</syntaxhighlight>
</lang>
 
=={{header|Stata}}==
Line 10,658 ⟶ 16,164:
Adapted from the Fortran 77 program, to illustrate the '''[http://www.stata.com/help.cgi?m2_goto goto]''' statement in Stata.
 
<langsyntaxhighlight lang="stata">mata
real matrix queens(real scalar n) {
real scalar i, j, k, p, q
Line 10,715 ⟶ 16,221:
rows(a)
92
end</langsyntaxhighlight>
 
It's also possible to save the solutions to a Stata dataset:
 
<langsyntaxhighlight lang="stata">clear
mata: a=queens(8)
getmata (a*)=a
save queens, replace</langsyntaxhighlight>
 
=== Recursive version ===
Line 10,728 ⟶ 16,234:
The recursive solution is adapted from one of the Python programs.
 
<langsyntaxhighlight lang="stata">mata
real matrix queens_rec(real scalar n) {
real rowvector a, u, v
Line 10,764 ⟶ 16,270:
}
}
end</langsyntaxhighlight>
 
The iterative and the recursive programs are equivalent:
 
<langsyntaxhighlight lang="stata">queens(8) == queens_rec(8)
1</langsyntaxhighlight>
 
=={{header|Swift}}==
Port of the optimized C code above
<syntaxhighlight lang="swift">
<lang Swift>
let maxn = 31
 
Line 10,831 ⟶ 16,337:
}
 
</syntaxhighlight>
</lang>
 
 
=={{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 10,873 ⟶ 16,378:
 
endprogram
</syntaxhighlight>
</lang>
 
=={{header|Tailspin}}==
A functional-ish solution utilising tailspin's data flows
<langsyntaxhighlight lang="tailspin">
templates queens
def n: $it;
templates addColumn
def prev: $it;
templates addIfPossible
def row: $it;
def minor: $it - $prev::length - 1;
def major: $it + $prev::length + 1;
// If any element of prev matches row, array is non-emptynot andan noarray resultthat proceedscontains pastrow, thesend firstit lineon...
$prev -> \(when <~[<=$row>]> do $ !\)
// Similar for the major and minor diagonals
-> \(when <?($prev -> \[i](<$row> 0- $i !\) -> (<~[<=$minor>](0>)> do $prev !\)
-> [i]\(when <?($it -> \[i]($i <+ $minor>)> 0i !\) -> (<~[<=$major>](0>)> do $prev !\)
-> [i](<?($it + $i..., <$major>)> 0 !) -> (<[row](0)> $prev !)
-> [ $prev..., $row] !
end addIfPossible
1..$n -> addIfPossible !
end addColumn
1..$n -> [$it] -> #
when <[]($n)> do $it !
<>otherwise $it -> addColumn -> #
end queens
 
Line 10,905 ⟶ 16,409:
 
def columns: ['abcdefgh'...];
'One of them is $($solutions(1) -> \[i]('$columns($i);$it;' !\));
' -> !OUT::write
 
'For 3 queens there are $(:[3 -> queens] -> $it::length); solutions
' -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 10,919 ⟶ 16,423:
 
A solution using state to find one solution if any exist
<langsyntaxhighlight lang="tailspin">
templates queens
def n: $it;
templates getRowColumn
when <?($@queens.freeRows($it.r::raw) <=0>)> do 0 !
when <?($@queens.freeMaxs($it.r::raw + $it.c::raw) <=0>)> do 0 !
when <?($@queens.freeMins($it.c::raw - $it.r::raw + $n) <=0>)> do 0 !
<>otherwise 1!
end getRowColumn
 
templatessink setRowColumn
def p: $it;
@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: $it;
row´1 -> #
when <done> do 1!
<-1> 1! // Use -1 to signal successful completion
when <=row´($n+1)> do 0 !
when <?({r: $it, c: $c} -> getRowColumn <=1>)> do
def r: $it;
@queens.queenRows($r::raw): $c;
{r: $it, c: $c, val: 0} -> !setRowColumn -> void
$c -> \(<=col´$n> -done´1!
<?(col´($c::raw + 1) -> placeQueen <=1>)> -done´1!
<>
{r: $r, c: $c, val: 1} -> !setRowColumn -> void
row´($r::raw + 1) !\) -> #
<>otherwise row´($it::raw + 1) -> #
end placeQueen
 
Line 10,957 ⟶ 16,463:
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>
</lang>
{{out}}
<pre>
Line 10,978 ⟶ 16,484:
 
{{works with|Tcl|8.5}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
proc unsafe {y} {
Line 11,024 ⟶ 16,530:
}
 
main [expr {$argc ? int(0+[lindex $argv 0]) : 8}]</langsyntaxhighlight>
{{out}}
<pre>$ tclsh8.5 8queens.tcl 6
Line 11,067 ⟶ 16,573:
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.
 
<langsyntaxhighlight lang="bash">#!/bin/bash
# variable declaration
Line 11,144 ⟶ 16,650:
work
out
depose</langsyntaxhighlight>
 
=={{header|Ursala}}==
Line 11,150 ⟶ 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 11,167 ⟶ 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 11,185 ⟶ 16,691:
=={{header|VBA}}==
{{trans|BBC BASIC}}
<langsyntaxhighlight lang="vb">'N-queens problem - non recursive & structured - vba - 26/02/2017
Sub n_queens()
Const l = 15 'number of queens
Line 11,242 ⟶ 16,748:
Debug.Print n, m 'number of queens, number of solutions
Next n
End Sub 'n_queens</langsyntaxhighlight>
{{out}}
<pre>
Line 11,265 ⟶ 16,771:
{{trans|BBC BASIC}}
To have the solutions printed (raw format) uncomment the ad hoc statement.
<langsyntaxhighlight 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)
Line 11,310 ⟶ 16,816:
Loop Until i=0
wscript.echo n &":"& m
next 'n</langsyntaxhighlight>
{{out}}
<pre>
Line 11,333 ⟶ 16,839:
{{works with|Visual Basic|VB6 Standard}}
{{trans|BBC BASIC}}
<langsyntaxhighlight lang="vb">'N-queens problem - non recursive & structured - vb6 - 25/02/2017
Sub n_queens()
Const l = 15 'number of queens
Line 11,390 ⟶ 16,896:
Debug.Print n, m 'number of queens, number of solutions
Next n
End Sub 'n_queens</langsyntaxhighlight>
{{out}}
<pre>
Line 11,412 ⟶ 16,918:
=={{header|Visual Basic .NET}}==
{{trans|BBC BASIC}}
<langsyntaxhighlight lang="vb">'N-queens problem - non recursive & structured - vb.net - 26/02/2017
Module Mod_n_queens
Sub n_queens()
Line 11,472 ⟶ 16,978:
Next n
End Sub 'n_queens
End Module</langsyntaxhighlight>
{{out}}
<pre>
Line 11,493 ⟶ 16,999:
 
=={{header|Wart}}==
<langsyntaxhighlight Wartlang="wart">def (nqueens n queens)
prn "step: " queens # show progress
if (len.queens = n)
Line 11,516 ⟶ 17,022:
def (diagonal_match curr other)
(= (abs (curr.0 - other.0))
(abs (curr.1 - other.1)))</langsyntaxhighlight>
 
=={{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>
 
=={{header|Xanadu}}==
 
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 11,590 ⟶ 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 11,596 ⟶ 17,261:
(either by XSLT processors saxon-6.5.5, xsltproc, xalan,
or any of the big5 browsers):
<langpre>
15863724
16837425
Line 11,602 ⟶ 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 11,621 ⟶ 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 11,748 ⟶ 17,413:
 
</xsl:stylesheet>
</syntaxhighlight>
</lang>
 
=={{header|XPL0}}==
[[File:NQueensXPL0.GIF|right]]
<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;
]</lang>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">DOCU The N Queens Problem:
DOCU Place N Queens on an NxN chess board
DOCU such that they don't threaten each other.
Line 11,875 ⟶ 17,480:
wend
end sub
</syntaxhighlight>
</lang>
 
=={{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>
 
 
=={{header|zkl}}==
Modified from a Haskell version (if I remember correctly)
<langsyntaxhighlight lang="zkl">fcn isAttacked(q, x,y) // ( (r,c), x,y ) : is queen at r,c attacked by q@(x,y)?
{ r,c:=q; (r==x or c==y or r+c==x+y or r-c==x-y) }
fcn isSafe(r,c,qs) // queen safe at (r,c)?, qs=( (r,c),(r,c)..) solution so far
Line 11,888 ⟶ 17,547:
if (row == N) return(qs);
return(qs.apply(self.fcn.fp(N,row+1)).flatten());
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">queens := queensN(4);
println(queens.len()," solution(s):");
queens.apply2(Console.println);</langsyntaxhighlight>
{{out}}
<pre>
305

edits