N-queens problem: Difference between revisions

m
→‎Python: Backtracking on permutations: Restored the original look
m (→‎Python: Backtracking on permutations: Restored the original look)
(118 intermediate revisions by 41 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 22:
* [[Solve the no connection puzzle]]
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">-V BoardSize = 8
 
F underAttack(col, queens)
I col C queens
R 1B
L(x) queens
I abs(col - x) == queens.len - L.index
R 1B
R 0B
 
F solve(n)
V result = [[Int]()]
[[Int]] newSolutions
L(row) 1 .. n
L(solution) result
L(i) 1 .. BoardSize
I !underAttack(i, solution)
newSolutions.append(solution [+] [i])
swap(&result, &newSolutions)
newSolutions.clear()
R result
 
print(‘Solutions for a chessboard of size ’String(BoardSize)‘x’String(BoardSize))
print()
 
L(answer) solve(BoardSize)
L(col) answer
V row = L.index
I row > 0
print(‘ ’, end' ‘’)
print(Char(code' ‘a’.code + row)‘’col, end' ‘’)
print(end' I L.index % 4 == 3 {"\n"} E ‘ ’)</syntaxhighlight>
 
{{out}}
<pre>
Solutions for a chessboard of size 8x8
 
a1 b5 c8 d6 e3 f7 g2 h4 a1 b6 c8 d3 e7 f4 g2 h5 a1 b7 c4 d6 e8 f2 g5 h3 a1 b7 c5 d8 e2 f4 g6 h3
a2 b4 c6 d8 e3 f1 g7 h5 a2 b5 c7 d1 e3 f8 g6 h4 a2 b5 c7 d4 e1 f8 g6 h3 a2 b6 c1 d7 e4 f8 g3 h5
a2 b6 c8 d3 e1 f4 g7 h5 a2 b7 c3 d6 e8 f5 g1 h4 a2 b7 c5 d8 e1 f4 g6 h3 a2 b8 c6 d1 e3 f5 g7 h4
a3 b1 c7 d5 e8 f2 g4 h6 a3 b5 c2 d8 e1 f7 g4 h6 a3 b5 c2 d8 e6 f4 g7 h1 a3 b5 c7 d1 e4 f2 g8 h6
a3 b5 c8 d4 e1 f7 g2 h6 a3 b6 c2 d5 e8 f1 g7 h4 a3 b6 c2 d7 e1 f4 g8 h5 a3 b6 c2 d7 e5 f1 g8 h4
a3 b6 c4 d1 e8 f5 g7 h2 a3 b6 c4 d2 e8 f5 g7 h1 a3 b6 c8 d1 e4 f7 g5 h2 a3 b6 c8 d1 e5 f7 g2 h4
a3 b6 c8 d2 e4 f1 g7 h5 a3 b7 c2 d8 e5 f1 g4 h6 a3 b7 c2 d8 e6 f4 g1 h5 a3 b8 c4 d7 e1 f6 g2 h5
a4 b1 c5 d8 e2 f7 g3 h6 a4 b1 c5 d8 e6 f3 g7 h2 a4 b2 c5 d8 e6 f1 g3 h7 a4 b2 c7 d3 e6 f8 g1 h5
a4 b2 c7 d3 e6 f8 g5 h1 a4 b2 c7 d5 e1 f8 g6 h3 a4 b2 c8 d5 e7 f1 g3 h6 a4 b2 c8 d6 e1 f3 g5 h7
a4 b6 c1 d5 e2 f8 g3 h7 a4 b6 c8 d2 e7 f1 g3 h5 a4 b6 c8 d3 e1 f7 g5 h2 a4 b7 c1 d8 e5 f2 g6 h3
a4 b7 c3 d8 e2 f5 g1 h6 a4 b7 c5 d2 e6 f1 g3 h8 a4 b7 c5 d3 e1 f6 g8 h2 a4 b8 c1 d3 e6 f2 g7 h5
a4 b8 c1 d5 e7 f2 g6 h3 a4 b8 c5 d3 e1 f7 g2 h6 a5 b1 c4 d6 e8 f2 g7 h3 a5 b1 c8 d4 e2 f7 g3 h6
a5 b1 c8 d6 e3 f7 g2 h4 a5 b2 c4 d6 e8 f3 g1 h7 a5 b2 c4 d7 e3 f8 g6 h1 a5 b2 c6 d1 e7 f4 g8 h3
a5 b2 c8 d1 e4 f7 g3 h6 a5 b3 c1 d6 e8 f2 g4 h7 a5 b3 c1 d7 e2 f8 g6 h4 a5 b3 c8 d4 e7 f1 g6 h2
a5 b7 c1 d3 e8 f6 g4 h2 a5 b7 c1 d4 e2 f8 g6 h3 a5 b7 c2 d4 e8 f1 g3 h6 a5 b7 c2 d6 e3 f1 g4 h8
a5 b7 c2 d6 e3 f1 g8 h4 a5 b7 c4 d1 e3 f8 g6 h2 a5 b8 c4 d1 e3 f6 g2 h7 a5 b8 c4 d1 e7 f2 g6 h3
a6 b1 c5 d2 e8 f3 g7 h4 a6 b2 c7 d1 e3 f5 g8 h4 a6 b2 c7 d1 e4 f8 g5 h3 a6 b3 c1 d7 e5 f8 g2 h4
a6 b3 c1 d8 e4 f2 g7 h5 a6 b3 c1 d8 e5 f2 g4 h7 a6 b3 c5 d7 e1 f4 g2 h8 a6 b3 c5 d8 e1 f4 g2 h7
a6 b3 c7 d2 e4 f8 g1 h5 a6 b3 c7 d2 e8 f5 g1 h4 a6 b3 c7 d4 e1 f8 g2 h5 a6 b4 c1 d5 e8 f2 g7 h3
a6 b4 c2 d8 e5 f7 g1 h3 a6 b4 c7 d1 e3 f5 g2 h8 a6 b4 c7 d1 e8 f2 g5 h3 a6 b8 c2 d4 e1 f7 g5 h3
a7 b1 c3 d8 e6 f4 g2 h5 a7 b2 c4 d1 e8 f5 g3 h6 a7 b2 c6 d3 e1 f4 g8 h5 a7 b3 c1 d6 e8 f5 g2 h4
a7 b3 c8 d2 e5 f1 g6 h4 a7 b4 c2 d5 e8 f1 g3 h6 a7 b4 c2 d8 e6 f1 g3 h5 a7 b5 c3 d1 e6 f8 g2 h4
a8 b2 c4 d1 e7 f5 g3 h6 a8 b2 c5 d3 e1 f7 g4 h6 a8 b3 c1 d6 e2 f5 g7 h4 a8 b4 c1 d3 e6 f2 g7 h5
</pre>
 
=={{header|360 Assembly}}==
Line 27 ⟶ 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 168 ⟶ 233:
U DC (4*LL-2)H'0' stack
REGS make sure to include copybook jcl
END NQUEENS</langsyntaxhighlight>
{{out}}
<pre>
Line 187 ⟶ 252:
</pre>
 
=={{header|6502 Assembly}}==
{{trans|Java}}
A few optimization techniques are used in this implementation. One goal was to get 8-queens to run in under 2 seconds on a 1 MHz computer.
 
Zero page values are stored where frequent use of the immediate addressing mode can be used as a speed up. This can be seen where a byte is referenced as variablename+1. INC and DEC instructions are used instead of ADC and SBC instructions for the comparison offsets.
 
The solution count is a 64-bit little endian value stored in memory starting at $0020, or $0D20 if the [[#Zero Page stub|Zero Page stub]] routine is used.
 
<syntaxhighlight lang="6502asm">n equ 8 ; queens
maximum equ 32 ; only limited by time
place equ $00
count equ maximum+place ; 64 bits (8 bytes)
length equ maximum+8
org $80
start
LDY #n ; n queens on an n x n board
STY greater+1
DEY
STY safe+1
LDX #length
LDA #$00
clear
STA place,X
DEX
BPL clear
next
INX
LDA #$FF
STA place,X
loop
INC place,X
LDA place,X
greater
CMP #n
BCS max
STX index+1
index
LDY #$00 ; index+1
BEQ safe
DEY
STA compare+1
STA add+1 ; compare
STA sub+1 ; compare
issafe
LDA place,Y
compare
CMP #$00 ; compare+1
BEQ loop ; unsafe
INC add+1
add
CMP #$00 ; add+1
BEQ loop ; unsafe
DEC sub+1
sub
CMP #$00 ; sub+1
BEQ loop ; unsafe
DEY
BPL issafe
safe
CPX #n-1
BNE next
INC count ; 64 bits (8 bytes)
BNE loop
INC count+1
BNE loop
INC count+2
BNE loop
INC count+3
BNE loop
INC count+4
BNE loop
INC count+5
BNE loop
INC count+6
BNE loop
INC count+7
BNE loop
BRK
max
DEX
BPL loop
; RTS</syntaxhighlight>
The code was assembled using Merlin32. The code length is 104 bytes not including the final 6 cycle RTS instruction.
<pre> n solutions cycles
1 1 443
2 0 710
3 0 1440
4 2 4359
5 10 17134
6 4 75848
7 40 337161
8 92 1616054
9 352 8044019
10 724 41556729
11 2680 230829955
12 14200 1378660940
13 73712 8684130248
14 365596 58185218171
15 2279184 412358679630
</pre>
==== Zero Page stub ====
The 6502 N-queens problem code resides within the zero page starting at $80 which can make running the program a bit tricky on some platforms. A stub is provided to facilitate running the zero page code. The stub routine turns off interrupts and swaps the zero page memory with an area residing at $D00 to $DFF, runs the zero page code, and swaps memory again. The cycle counts listed above do not include the time to run this stub. With the final RTS instruction included, the 105 byte N-queens zero page code must be in memory starting at $D80.
<syntaxhighlight lang="6502asm"> org $C00
PHP
SEI
JSR swap
JSR $0080
JSR swap
PLP
jmp end
swap
LDX #$00
loop
LDY $D00,X
LDA $00,X
STY $00,X
STA $D00,X
INX
BNE loop
RTS
end
; RTS</syntaxhighlight>
=={{header|ABAP}}==
<syntaxhighlight lang="abap">
<lang ABAP>
TYPES: BEGIN OF gty_matrix,
1 TYPE c,
Line 474 ⟶ 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 526 ⟶ 713:
end loop;
Put_Line (" A B C D E F G H");
end Queens;</langsyntaxhighlight>
{{out}}
<pre>
Line 545 ⟶ 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 593 ⟶ 780:
Put_Line (Long_Integer'Image (Queens (N)));
end loop;
end CountQueens;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 603 ⟶ 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 653 ⟶ 840:
FI
OD
)</langsyntaxhighlight>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
More or less copied from the "DFS" lesson on tryapl.org .
<syntaxhighlight lang="apl">
<lang APL>
⍝Solution
accm←{⍺,((⍴⍵)=⍴⊃⍺)↑⊂⍵}
Line 670 ⟶ 857:
⍝Example
printqueens 6
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 704 ⟶ 891:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">-- Finds all possible solutions and the unique patterns.
 
property Grid_Size : 8
Line 867 ⟶ 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 890 ⟶ 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 896 ⟶ 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 979 ⟶ 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,063 ⟶ 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,069 ⟶ 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,158 ⟶ 1,509:
 
GuiClose:
ExitApp</langsyntaxhighlight>
[[image:N-Queens_SolutionBrowserGUI.png]]
 
Line 1,167 ⟶ 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,227 ⟶ 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,260 ⟶ 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,266 ⟶ 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,401 ⟶ 1,752:
i, n, all)
}
</syntaxhighlight>
</lang>
 
=={{header|Befunge}}==
Line 1,407 ⟶ 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,435 ⟶ 1,786:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( printBoard
= board M L x y S R row line
. :?board
Line 1,495 ⟶ 1,846:
| out$(found !solutions solutions)
)
);</langsyntaxhighlight>
{{out}} (tail):
<pre>
Line 1,540 ⟶ 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,570 ⟶ 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,609 ⟶ 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,701 ⟶ 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,774 ⟶ 2,125:
printf("Number of solution for %d is %d\n",n,nqueens(n));
}
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
Line 1,787 ⟶ 2,138:
{{works with|C sharp|C#|7}}
<!-- By Martin Freedman, 13/02/2018 -->
<langsyntaxhighlight lang="csharp">using System.Collections.Generic;
using static System.Linq.Enumerable;
using static System.Console;
Line 1,829 ⟶ 2,180:
public static IEnumerable<T> ToSingleton<T>(this T item) { yield return item; }
}
}</langsyntaxhighlight>
Output
<pre>8-queens has 92 solutions
Line 1,837 ⟶ 2,188:
===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 ...)
<langsyntaxhighlight lang="csharp">
using System.Collections.Generic;
using static System.Linq.Enumerable;
Line 1,875 ⟶ 2,226:
public static IEnumerable<T> ToSingleton<T>(this T item) { yield return item; }
}
}</langsyntaxhighlight>
=== 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 -->
<langsyntaxhighlight lang="csharp">using static System.Linq.Enumerable;
using static System.Console;
 
Line 1,908 ⟶ 2,259:
}
}
}</langsyntaxhighlight>
 
=={{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 2,014 ⟶ 2,365:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}Output for N = 4:
<pre> a b c d
Line 2,028 ⟶ 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,286 ⟶ 2,637:
std::cout << queens( N ) << "\n";
}
</syntaxhighlight>
</lang>
{{out}} for N=4:
<pre>
Line 2,307 ⟶ 2,658:
=== Alternate version ===
Windows-only
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <iostream>
Line 2,408 ⟶ 2,759:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,437 ⟶ 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,526 ⟶ 2,877:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</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,552 ⟶ 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,674 ⟶ 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,695 ⟶ 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,738 ⟶ 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,762 ⟶ 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,825 ⟶ 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,864 ⟶ 4,303:
store = free
-- end
</syntaxhighlight>
</lang>
 
Yet another approach, also from the same source.
 
<langsyntaxhighlight lang="curry">
-- 8-queens implementation with both the Constrained Constructor
-- and the Fused Generate and Test patterns.
Line 2,928 ⟶ 4,367:
 
main = extend []
</syntaxhighlight>
</lang>
Mainly [http://www-ps.informatik.uni-kiel.de/~pakcs/webpakcs/main.cgi?queens webpakcs], uses constraint-solver.
<langsyntaxhighlight lang="curry">import CLPFD
import Findall
 
Line 2,948 ⟶ 4,387:
 
-- oneSolution = unpack $ queens 8
-- allSolutions = findall $ queens 8</langsyntaxhighlight>
 
=={{header|D}}==
===Short Version===
This high-level version uses the second solution of the Permutations task.
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm, std.range, permutations2;
 
Line 2,961 ⟶ 4,400:
n.iota.map!(i => p[i] - i).array.sort().uniq.count == n)
.count.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>92</pre>
Line 2,968 ⟶ 4,407:
This version shows all the solutions.
{{trans|C}}
<langsyntaxhighlight lang="d">enum side = 8;
__gshared int[side] board;
 
Line 3,011 ⟶ 4,450:
y--;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,048 ⟶ 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,137 ⟶ 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,146 ⟶ 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,210 ⟶ 4,649:
void main() {
enumerate(4);
}</langsyntaxhighlight>
{{out}}
<pre>* Q * *
Line 3,222 ⟶ 4,661:
* Q * *
</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program N_queens_problem;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils;
 
var
i: Integer;
q: boolean;
a: array[0..8] of boolean;
b: array[0..16] of boolean;
c: array[0..14] of boolean;
x: array[0..8] of Integer;
 
procedure TryMove(i: Integer);
begin
var j := 1;
while True do
begin
q := false;
if a[j] and b[i + j] and c[i - j + 7] then
begin
x[i] := j;
a[j] := false;
b[i + j] := false;
c[i - j + 7] := false;
 
if i < 8 then
begin
TryMove(i + 1);
if not q then
begin
a[j] := true;
b[i + j] := true;
c[i - j + 7] := true;
end;
end
else
q := true;
end;
if q or (j = 8) then
Break;
inc(j);
end;
end;
 
begin
for i := 1 to 8 do
a[i] := true;
 
for i := 2 to 16 do
b[i] := true;
 
for i := 0 to 14 do
c[i] := true;
 
TryMove(1);
 
if q then
for i := 1 to 8 do
writeln(i, ' ', x[i]);
readln;
end.</syntaxhighlight>
 
=={{header|Draco}}==
{{trans|C}}
<syntaxhighlight lang="draco">byte SIZE = 8;
word count;
 
proc solve([*] int hist; int col) void:
int i, j, n;
n := dim(hist, 1);
if col = n then
count := count + 1;
writeln();
writeln("No. ", count);
writeln("-----");
for i from 0 upto n-1 do
for j from 0 upto n-1 do
write(if j=hist[i] then 'Q'
elif (i+j)&1 /= 0 then ' '
else '.' fi)
od;
writeln()
od
else
for i from 0 upto n-1 do
j := 0;
while j<col and not (hist[j]=i or |(hist[j]-i) = col-j) do
j := j + 1
od;
if j >= col then
hist[col] := i;
solve(hist, col+1)
fi
od
fi
corp
 
proc nonrec main() void:
[SIZE] int hist;
count := 0;
solve(hist, 0)
corp</syntaxhighlight>
{{out}}
<pre>No. 1
-----
Q . . .
. .Q. .
. . . .Q
. . Q .
. Q . .
. . .Q.
.Q. . .
. Q . .</pre>
...
<pre>No. 92
-----
. . . .Q
. Q . .
Q . . .
.Q. . .
. . .Q.
Q . . .
. . . Q
. .Q. .</pre>
 
=={{header|EasyLang}}==
 
<syntaxhighlight lang="easylang">
<lang>intvars
subr show_sol
print "Solution " & n_sol
print ""
for i range= 1 to n
write " "
for j range= 1 to n
if j = x[i]
write "Q "
else
write ". "
.
.
. print ""
print "".
print ""
.
print ""
.
subr test
ok = 1
for i range= 1 to y - 1
if x[y] = x[i] or abs (x[i] - x[y]) = abs (y - i)
ok = 0
.
.
.
n = 8
len x[] n
y = 01
x[01] = 01
while y >= 01
call test
if ok = 1 and y + 1 <>= n
y += 1
x[y] = 01
else
if ok = 1
n_sol += 1
if n_sol <= 1
call show_sol
.
.
while y >= 1 and x[y] = n
.
while y >= 0 and x[y] -= n - 1
y -= 1.
. if y >= 1
if x[y] >+= 01
x[y] += 1.
.
.
.
print n_sol & " solutions"</lang>
</syntaxhighlight>
{{out}}
<pre>Solution 1
Line 3,290 ⟶ 4,861:
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; square num is i + j*N
(define-syntax-rule (sq i j) (+ i (* j N)))
Line 3,348 ⟶ 4,919:
(define (task up-to-n)
(for ((i up-to-n)) (writeln i ' ♕ (q-count i) 'solutions)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,367 ⟶ 4,938:
12 ♕ 14200 solutions
</pre>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="Ecstasy">/**
* A solver for the classic 8-queens problem.
*
* @see https://rosettacode.org/wiki/N-queens_problem
*/
module eightQueens {
void run() {
@Inject Console console;
Int count = new Board().solve(b -> console.print($"{b}\n"));
console.print($"{count} solutions found");
}
 
/**
* `Board` represents a chess board that holds only queens. The board
* is organized as columns 0 ("A") to 7 ("H"), and rows 0 (rank "1")
* to 7 (rank "8").
*/
const Board {
/**
* Construct an empty board.
*/
construct() {}
 
/**
* Internal: Construct a specifically-populated board.
*/
private construct(Int queens, Int claimed) {
this.queens = queens;
this.claimed = claimed;
}
 
/**
* Each bit of this 64-bit integer represents a queen.
*/
private Int queens;
/**
* Each bit of this 64-bit integer represents a queen or a threat.
*/
private Int claimed;
 
/**
* Translate a column and row to a bit-mask, used with the
* [queens] and [claimed] properties. Examples:
* * A1 is (0,0) => 0x0000000000000001
* * H8 is (7,7) => 0x8000000000000000
*/
private Int mask(Int col, Int row) = 1 << (row << 3) + col;
 
/**
* Determine if the specified square has a queen in it.
*/
Boolean occupied(Int col, Int row) {
return queens & mask(col, row) != 0;
}
 
/**
* Determine if the specified square is safe from the queens.
*/
Boolean safe(Int col, Int row) {
return claimed & mask(col, row) == 0;
}
 
/**
* Attempt to place a queen in a specified square.
*
* @return True iff a queen can be safely placed in the square
* @return (conditional) the new Board with the new queen on it
*/
conditional Board placeQueen(Int col, Int row) {
assert 0 <= col < 8 && 0 <= row < 8;
if (!safe(col, row)) {
return False;
}
 
Int newQueens = queens | mask(col, row);
Int newClaimed = claimed | queens;
// claim all threatened spaces
for (Int i : 0..7) {
newClaimed |= mask(i, row) | mask(col, i);
val diagDownRow = row + i - col;
if (0 <= diagDownRow < 8) {
newClaimed |= mask(i, diagDownRow);
}
val diagUpRow = row - i + col;
if (0 <= diagUpRow < 8) {
newClaimed |= mask(i, diagUpRow);
}
}
return True, new Board(newQueens, newClaimed);
}
 
/**
* Attempt to find all solutions to the n-queens problem.
*/
Int solve(function void(Board) yield) = solve(yield, 0);
 
/**
* Internal: Attempt to find all solutions to the n-queens problem,
* starting with the specified column and recursively solving by
* moving to the next column for each potential solution found in
* the specified column.
*/
private Int solve(function void(Board) yield, Int col) {
if (col == 8) {
// there is no column 8; we've found a solution
yield(this);
return 1;
}
 
Int count = 0;
for (Int rank : 8..1) {
val row = 8-rank;
if (Board afterPlacing := placeQueen(col, row)) {
count += afterPlacing.solve(yield, col + 1);
}
}
return count;
}
 
@Override String toString() {
val buf = new StringBuffer();
for (Int rank : 8..1) {
buf.append($"{rank} |");
val row = 8-rank;
for (Int col : 0..7) {
buf.add(occupied(col, row) ? 'q' : '_').add('|');
}
buf.add('\n');
}
return buf.append(" A B C D E F G H").toString();
}
}
}</syntaxhighlight>
<b>Output:</b>
<code><pre>
8 |q|_|_|_|_|_|_|_|
7 |_|_|_|_|_|_|q|_|
6 |_|_|_|_|q|_|_|_|
5 |_|_|_|_|_|_|_|q|
4 |_|q|_|_|_|_|_|_|
3 |_|_|_|q|_|_|_|_|
2 |_|_|_|_|_|q|_|_|
1 |_|_|q|_|_|_|_|_|
A B C D E F G H
 
8 |q|_|_|_|_|_|_|_|
7 |_|_|_|_|_|_|q|_|
6 |_|_|_|q|_|_|_|_|
5 |_|_|_|_|_|q|_|_|
4 |_|_|_|_|_|_|_|q|
3 |_|q|_|_|_|_|_|_|
2 |_|_|_|_|q|_|_|_|
1 |_|_|q|_|_|_|_|_|
A B C D E F G H
 
(...)
 
8 |_|_|q|_|_|_|_|_|
7 |_|_|_|_|_|q|_|_|
6 |_|_|_|q|_|_|_|_|
5 |_|q|_|_|_|_|_|_|
4 |_|_|_|_|_|_|_|q|
3 |_|_|_|_|q|_|_|_|
2 |_|_|_|_|_|_|q|_|
1 |q|_|_|_|_|_|_|_|
A B C D E F G H
 
92 solutions found
</pre></code>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
QUEENS
Line 3,438 ⟶ 5,180:
end
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,474 ⟶ 5,216:
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule RC do
def queen(n, display \\ true) do
solve(n, [], [], [], display)
Line 3,511 ⟶ 5,253:
Enum.each(7..12, fn n ->
IO.puts " #{n} Queen : #{RC.queen(n, false)}" # no display
end)</langsyntaxhighlight>
 
{{out}}
Line 3,644 ⟶ 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,732 ⟶ 5,564:
Board = solve( N ),
display( Board ).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,751 ⟶ 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,838 ⟶ 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 3,885 ⟶ 5,735:
 
printNumberOfSolutions()
</syntaxhighlight>
</lang>
 
The output:
 
<langpre>
| | | |X| | | | | |
| |X| | | | | | | |
Line 3,911 ⟶ 5,761:
10 724
11 2680
</langpre>
 
=={{header|Factor}}==
{{works with|Factor|0.98}}
<langsyntaxhighlight lang="factor">USING: kernel sequences math math.combinatorics formatting io locals ;
IN: queens
 
Line 3,941 ⟶ 5,791:
[
[ 1 + "%d " printf ] each nl
] each ;</langsyntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">variable solutions
variable nodes
 
Line 3,972 ⟶ 5,822:
solutions @ . ." solutions, " nodes @ . ." nodes" ;
 
8 queens \ 92 solutions, 1965 nodes</langsyntaxhighlight>
 
=== Alternate solution adapted from FD-V02N1.pdf ===
<syntaxhighlight lang="forth">
\ http://www.forth.org/fd/FD-V02N1.pdf
VOCABULARY nqueens ALSO nqueens DEFINITIONS
 
8 constant queens
 
\ Nqueen solution from FD-V02N1.pdf
: 1array CREATE 0 DO 1 , LOOP DOES> SWAP CELLS + ;
queens 1array a \ a,b & c: workspaces for solutions
queens 2* 1array b
queens 2* 1array c
queens 1array x \ trial solutions
 
: safe ( c i -- n )
SWAP
2DUP - queens 1- + c @ >R
2DUP + b @ >R
DROP a @ R> R> * * ;
 
: mark ( c i -- )
SWAP
2DUP - queens 1- + c 0 swap !
2DUP + b 0 swap !
DROP a 0 swap ! ;
 
: unmark ( c i -- )
SWAP
2DUP - queens 1- + c 1 swap !
2DUP + b 1 swap !
DROP a 1 swap ! ;
 
VARIABLE tries
VARIABLE sols
 
: .cols queens 0 DO I x @ 1+ 5 .r loop ;
: .sol ." Found on try " tries @ 6 .R .cols cr ;
 
: try
queens 0
DO 1 tries +!
DUP I safe
IF DUP I mark
DUP I SWAP x !
DUP queens 1- < IF DUP 1+ RECURSE ELSE sols ++ .sol THEN
DUP I unmark
THEN
LOOP DROP ;
 
: go 0 tries ! CR 0 try CR sols @ . ." solutions Found, for n = " queens . ;
go
</syntaxhighlight>
 
=={{header|Fortran}}==
Line 3,978 ⟶ 5,881:
 
Using a back tracking method to find one solution
<langsyntaxhighlight lang="fortran">program Nqueens
implicit none
 
Line 4,078 ⟶ 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,202 ⟶ 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,274 ⟶ 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,328 ⟶ 6,231:
print *, n, m
end do
end program</langsyntaxhighlight>
 
===Alternate Fortran 95 solution with OpenMP===
Line 4,341 ⟶ 6,244:
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,462 ⟶ 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,521 ⟶ 6,747:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> 1 3 5 2 4
Line 4,552 ⟶ 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,592 ⟶ 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,638 ⟶ 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.
<lang Frink>
<syntaxhighlight lang="frink">solution[board] :=
{
for q = 0 to length[board] - 1
Line 4,648 ⟶ 6,874:
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>
</lang>
 
=={{header|Fōrmulæ}}==
 
In [http{{FormulaeEntry|page=https://wiki.formulae.org/?script=examples/N-queens_problem this] page you can see the solution of this task.}}
 
'''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]]
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.
 
[[File:Fōrmulæ - N-queens problem 06.png]]
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|GAP}}==
Line 4,668 ⟶ 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,745 ⟶ 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)===
<langsyntaxhighlight 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,809 ⟶ 7,053:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,824 ⟶ 7,068:
 
=== Refactored Niklaus Wirth algorithm (clearer/Go friendly solution) ===
<langsyntaxhighlight lang="go">/*
* N-Queens Problem
*
Line 4,947 ⟶ 7,191:
trycol(0)
printresults()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,968 ⟶ 7,212:
[[N-queens_problem/dlx_go|dlx packge]].
 
<langsyntaxhighlight Golang="go">package main
 
import (
Line 5,103 ⟶ 7,347:
}
return nil
}</langsyntaxhighlight>
{{out}}
<pre>
Line 5,137 ⟶ 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,160 ⟶ 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,214 ⟶ 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,225 ⟶ 7,469:
else { println "first:${qus[0]}"; println "last:${qus[-1]}" }
println()
}</langsyntaxhighlight>
 
Interpreting the Results:
Line 5,295 ⟶ 7,539:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad
import Data.List
 
Line 5,324 ⟶ 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,339 ⟶ 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,354 ⟶ 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,366 ⟶ 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
map (unlines . map (intercalate " ") . transpose . map boardLines) $
. fmap (intercalate " ")
chunksOf nCols (queenPuzzle nSize nSize)
. transpose
. map boardLines
<$> chunksOf nCols (queenPuzzle nSize nSize)
where
go r x
| r == x = '♛'
| otherwise = '.'
boardLines rows =
[ mapgo (boolr '.' '♛' . (== r))<$> [1 .. (length rows)]
| r <- rows ]
]
 
chunksOf :: Int -> [a] -> [[a]]
Line 5,403 ⟶ 7,656:
 
main :: IO ()
main = (putStrLn . unlines) $ showSolutions 10 7</langsyntaxhighlight>
{{Out}}
<pre>......♛ ......♛ ......♛ ......♛ .....♛. .....♛. .....♛. .....♛. .....♛. .....♛.
Line 5,438 ⟶ 7,691:
 
===Breadth-first search and Depth-first search===
<langsyntaxhighlight lang="haskell">import Control.Monad
import System.Environment
 
Line 5,500 ⟶ 7,753:
let n = read narg :: Int
print (bfs n [emptySt])
print (head $ dfs n emptySt)</langsyntaxhighlight>
 
{{Out}}
Line 5,507 ⟶ 7,760:
 
=={{header|Heron}}==
<langsyntaxhighlight lang="heron">module NQueens {
inherits {
Heron.Windows.Console;
Line 5,601 ⟶ 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,620 ⟶ 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,631 ⟶ 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,643 ⟶ 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,677 ⟶ 7,930:
write()
return # Comment out to see all possible solutions
end</langsyntaxhighlight>
 
A sample run for <tt>N = 6</tt>:
Line 5,701 ⟶ 7,954:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "NQueens.bas"
110 TEXT 80
120 DO
Line 5,738 ⟶ 7,991:
450 LET T(I)=1
460 NEXT
470 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
Line 5,744 ⟶ 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,753 ⟶ 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,811 ⟶ 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,848 ⟶ 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 ------------------
 
// queenPuzzle :: Int -> Int -> [[Int]]
Line 5,865 ⟶ 8,118:
[]
] : go(nRows - 1).reduce(
(a, solution) => append(a)([
enumFromTo...a, ...(0)(intCols - 1)
.reduce enumFromTo(0)(b,intCols iCol)- =>1)
safe.reduce(nRows - 1(b, iCol, solution) ? (=>
b.concatsafe([solution.concat(iCol)])
) : b nRows - 1, iCol, [])solution
), []? (
[...b, [...solution, iCol]]
) : b, [])
)
], []
);
 
 
return go;
};
Line 5,878 ⟶ 8,137:
// safe : Int -> Int -> [Int] -> Bool
const safe = (iRow, iCol, solution) =>
!anyzip(solution)(
enumFromTo(0)(iRow - 1)
)
.some(
([sc, sr]) => (iCol === sc) || (
sc + sr === iCol + iRow
) || (sc - sr === iCol - iRow)
);
)(zip(solution)(enumFromTo(0)(iRow - 1)));
 
 
// ----------------------- TEST ------------------------
// Ten columns of solutions to the 7*7 board
 
// main :: IO ()
const main = () =>
// eslint-disable-next-line no-console
console.log(
showSolutions(10, )(7)
);
 
// ---------------------- DISPLAY ----------------------
 
// showSolutions :: Int -> Int -> String
const showSolutions = (nCols, nBoardSize) =>
// Display of solutions, in nCols columns
intercalate('\n\n')(
// for a board map(unlines)(of size N * N.
n => mapchunksOf(composenCols)(
mapqueenPuzzle(intercalate(' 'n)(n),
transpose,)
.map(rowsxs => transpose(
xs.map(r => concat(
rows => concatMaprows.map(
r => composeenumFromTo(bool1)('rows.')('♛'), eq(r)length)
).flatMap(enumFromTo(1)(rows.length))
)) x => r === x ? (rows)
"♛"
) : "."
)
.join("")
)
))(chunksOf(nCols)(
join(queenPuzzle)(nBoardSize)
))
)
.map(cells => cells.join(" "));
)
.map(x => x.join("\n"))
.join("\n\n");
 
// ----------------- GENERIC FUNCTIONS -----------------
 
// ---------------- GENERIC FUNCTIONS ----------------
// abs :: Num a => a -> a
const abs = Math.abs
 
// bool :: a -> a -> Bool -> a
const bool = f => t => p =>
p ? t : f;
 
// eq (==) :: Eq a => a -> a -> Bool
const eq = a => b => a === b;
 
// any :: (a -> Bool) -> [a] -> Bool
const any = p => xs => xs.some(p);
 
// (++) :: [a] -> [a] -> [a]
const append = xs => ys => xs.concat(ys);
 
// bindFn (>>=) :: (a -> b) -> (b -> a -> c) -> a -> c
const bindFn = f => bop =>
// Binary operator applied over f x and x.
x => bop(f(x))(x);
 
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = n => xs =>{
// xs.reduce((a, _,split i,into xs)sublists =>of length n.
// The last sublist iwill %be nshort ? a : a.concat([xs.slice(i, i +if n)]), []);
// does not evenly divide the length of xs .
const go = xs => {
const chunk = xs.slice(0, n);
 
// compose (<<<) :: (b -> c) -> return Boolean(a -> bchunk.length) -> a ->? c[
chunk, ...go(xs.slice(n))
const compose = (...fs) =>
x => fs.reduceRight((a, f) =>] f(a),: x)[];
};
 
// concat :: [[a]] ->return [a]go;
};
// concat :: [String] -> String
const concat = xs =>
0 < xs.length ? (() => {
const unit = 'string' !== typeof xs[0] ? (
[]
) : '';
return unit.concat.apply(unit, xs);
})() : [];
 
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = f => xs =>
xs.flatMap(f);
 
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m => n =>
n => Array.from({
length: Math.floor(1 + n - m) + 1
}, (_, i) => m + i);
 
// identity :: a -> a
const identity = x => x;
 
// intercalatetranspose_ :: String -> [[a]] -> String[[a]]
const intercalatetranspose = srows => xs => xs.join(s);
// 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]
)
) : [];
 
// join :: m (m a) -> m a
// Function instance
const join = f => bindFn(f)(
identity
);
 
// mapzip :: ([a] -> [b)] -> [(a], -> [b)]
const mapzip = f => xs => xs.map(f)
// 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]]);
 
// transpose :: [[a]]MAIN -> [[a]]--
return main();
const transpose = xs =>
})();</syntaxhighlight>
xs[0].map((_, iCol) => xs.map(row => row[iCol]));
{{Out}}
<pre>....... ....... ....... ....... ♛...... ♛...... ♛...... ♛...... ♛...... ♛......
.♛..... ..♛.... ...♛... ....♛.. ..♛.... ..♛.... ...♛... ...♛... ...♛... ....♛..
...♛... .....♛. ♛...... ..♛.... ....... ....♛.. ....... .♛..... .....♛. .♛.....
.....♛. .♛..... ....♛.. ♛...... .....♛. ....... ..♛.... ....... ..♛.... .....♛.
♛...... ....♛.. .♛..... .....♛. ...♛... .♛..... .....♛. .....♛. ....... ..♛....
..♛.... ♛...... .....♛. ...♛... .♛..... ...♛... .♛..... ..♛.... .♛..... .......
....♛.. ...♛... ..♛.... .♛..... ....♛.. .....♛. ....♛.. ....♛.. ....♛.. ...♛...
 
♛...... .♛..... .♛..... .♛..... .♛..... .♛..... .♛..... ..♛.... ..♛.... ..♛....
// unlines :: [String] -> String
.....♛. ....... ....... ...♛... ....♛.. .....♛. .....♛. ....... ....... ♛......
const unlines = xs => xs.join('\n');
...♛... ....♛.. ....♛.. .....♛. ♛...... ♛...... ..♛.... .♛..... ...♛... .....♛.
.♛..... ♛...... ..♛.... ♛...... ...♛... ..♛.... ....... ....♛.. ♛...... ...♛...
....... ...♛... ♛...... ..♛.... ....... ....♛.. ...♛... ♛...... ....♛.. .♛.....
....♛.. .....♛. .....♛. ....♛.. ..♛.... ....... ♛...... .....♛. .♛..... .......
..♛.... ..♛.... ...♛... ....... .....♛. ...♛... ....♛.. ...♛... .....♛. ....♛..
 
..♛.... ..♛.... ..♛.... ...♛... ...♛... ...♛... ...♛... ...♛... ...♛... ....♛..
// zip :: [a] -> [b] -> [(a,b)]
....♛.. .....♛. .....♛. ....... ....... ♛...... .♛..... .....♛. .....♛. .......
const zip = xs => ys =>
....... .♛..... ...♛... ..♛.... ....♛.. ....♛.. ....... ♛...... ♛...... .♛.....
xs.slice(0, Math.min(xs.length, ys.length))
.♛..... ....♛.. ♛...... .....♛. ..♛.... .♛..... ....♛.. ..♛.... ....♛.. ...♛...
.map((x, i) => [x, ys[i]]);
...♛... ♛...... ....♛.. .♛..... ♛...... .....♛. ..♛.... ....♛.. .♛..... .....♛.
.....♛. ...♛... ....... ....♛.. .....♛. ..♛.... ♛...... ....... ....... ♛......
♛...... ....... .♛..... ♛...... .♛..... ....... .....♛. .♛..... ..♛.... ..♛....
 
....♛.. ....♛.. ....♛.. ....♛.. ....♛.. ....♛.. .....♛. .....♛. .....♛. .....♛.
// MAIN ---
♛...... .♛..... .♛..... .♛..... ..♛.... ..♛.... ♛...... .♛..... ..♛.... ...♛...
return main();
...♛... ....... ...♛... .....♛. ♛...... .....♛. ..♛.... ....♛.. ....... .♛.....
})();</lang>
....... ..♛.... .....♛. ..♛.... .....♛. ....... ....♛.. ♛...... ...♛... .......
..♛.... .....♛. ....... ....... ...♛... .♛..... ....... ...♛... ♛...... ....♛..
.....♛. ...♛... ..♛.... ...♛... .♛..... ...♛... .♛..... ....... ....♛.. ..♛....
.♛..... ♛...... ♛...... ♛...... ....... ♛...... ...♛... ..♛.... .♛..... ♛......</pre>
 
=={{header|jq}}==
Line 6,004 ⟶ 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 6,028 ⟶ 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 6,038 ⟶ 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 6,054 ⟶ 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,072 ⟶ 8,340:
 
count( permutations(n) | select(allowable) );
</syntaxhighlight>
</lang>
'''Example''':
<syntaxhighlight lang ="jq">queens(8)</langsyntaxhighlight>
{{out}}
92
Line 6,080 ⟶ 8,348:
=={{header|Julia}}==
 
<langsyntaxhighlight lang="ruby">"""
#!/usr/bin/env julia
 
__precompile__(true)
 
"""
# EightQueensPuzzle
 
Line 6,093 ⟶ 8,356:
module EightQueensPuzzle
 
export mainBoard, solve!
 
typemutable struct Board
cols::Int
nodes::Int
Line 6,107 ⟶ 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,140 ⟶ 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,179 ⟶ 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,262 ⟶ 8,517:
println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 6,323 ⟶ 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,409 ⟶ 8,664:
End Function
 
</syntaxhighlight>
</lang>
 
=={{header|Locomotive Basic}}==
Line 6,415 ⟶ 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,466 ⟶ 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,472 ⟶ 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,488 ⟶ 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,525 ⟶ 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,588 ⟶ 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,600 ⟶ 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,624 ⟶ 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,631 ⟶ 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,680 ⟶ 9,102:
 
solveNqueens[8] // dispSol
</syntaxhighlight>
</lang>
<pre>- - - - Q - - -
- Q - - - - - -
Line 6,689 ⟶ 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,708 ⟶ 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}}==
<langsyntaxhighlight lang="minizinc">int: n;
array [1..n] of var 1..n: q; % queen in column i is in row q[i]
 
Line 6,724 ⟶ 9,296:
satisfy;
output [ if fix(q[j]) == i then "Q" else "." endif ++
if j == n then "\n" else "" endif | i,j in 1..n]</langsyntaxhighlight>
 
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,788 ⟶ 10,433:
Quit
Do Queens
</syntaxhighlight>
</lang>
<div style="overflow:scroll; height:400px;">
<syntaxhighlight lang="mumps">
<lang MUMPS>
+--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+
8 | |##| Q|##| |##| |##| | |##| Q|##| |##| |##| | |##| | Q| |##| |##|
Line 6,998 ⟶ 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 7,010 ⟶ 10,655:
return false
 
proc solve(n: int): autoseq[seq[int]] =
result = newSeq[seq[int]]()
result.add(@[])
Line 7,016 ⟶ 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 7,022 ⟶ 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,087 ⟶ 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,150 ⟶ 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,183 ⟶ 10,859:
else 8 in
 
solutions n</langsyntaxhighlight>
{{out}}
<pre>$ ocaml queens.ml 6
Line 7,217 ⟶ 10,893:
=={{header|Oz}}==
A pretty naive solution, using constraint programming:
<langsyntaxhighlight lang="oz">declare
fun {Queens N}
proc {$ Board}
Line 7,284 ⟶ 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,369 ⟶ 11,045:
14 365596
15 2279184
16 14772512 }</langsyntaxhighlight>
 
===Alternative===
Line 7,387 ⟶ 11,063:
Solution found</pre>
<langsyntaxhighlight lang="pascal">program NQueens;
{$IFDEF FPC}
{$MODE DELPHI}
Line 7,498 ⟶ 11,174:
end;
WriteLn('Fertig');
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 7,522 ⟶ 11,198:
 
=={{header|PDP-11 Assembly}}==
<syntaxhighlight lang="pdp-11 assembly">
<lang PDP-11 Assembly>
; "eight queens problem" benchmark test
 
Line 7,637 ⟶ 11,313:
 
scr: ;display RAM
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">my ($board_size, @occupied, @past, @solutions);
 
sub try_column {
Line 7,679 ⟶ 11,355:
 
#print for @solutions; # un-comment to see all solutions
print "total " . @solutions . " solutions\n";</langsyntaxhighlight>
{{out}}
<pre>total 14200 solutions</pre>
 
=={{header|Phix}}==
<!--(phixonline)-->
<lang Phix>--
<syntaxhighlight lang="phix">
with javascript_semantics
--
-- demo\rosetta\n_queens.exw
-- =========================
Line 7,693 ⟶ 11,372:
bd, -- backward diagonals
board
 
atom count
 
procedure solve(integer row, integer N, integer show)
for col=1 to N do
Line 7,704 ⟶ 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,717 ⟶ 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,734 ⟶ 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,767 ⟶ 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,785 ⟶ 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,850 ⟶ 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,857 ⟶ 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,887 ⟶ 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,896 ⟶ 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,919 ⟶ 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,946 ⟶ 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,986 ⟶ 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 8,014 ⟶ 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 8,030 ⟶ 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 8,050 ⟶ 11,828:
(mapcar - L R) )
(inc 'Cnt) ) ) )
Cnt ) )</langsyntaxhighlight>
{{out}} for both cases:
<pre>: (queens 8)
Line 8,057 ⟶ 11,835:
=={{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.
<langsyntaxhighlight lang="pli">
NQUEENS: PROC OPTIONS (MAIN);
DCL A(35) BIN FIXED(31) EXTERNAL;
Line 8,135 ⟶ 11,913:
END QUEEN;
END NQUEENS; </langsyntaxhighlight>
 
=={{header|PowerBASIC}}==
=== Recursive version ===
{{trans|Stata}}
<langsyntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
 
Line 8,185 ⟶ 11,963:
PRINT m
END IF
END FUNCTION</langsyntaxhighlight>
 
=== Iterative version ===
{{trans|Stata}}
<langsyntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
 
Line 8,235 ⟶ 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,299 ⟶ 12,077:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
Get-NQueensBoard 8
''
Line 8,308 ⟶ 12,086:
''
Get-NQueensBoard 14
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 8,342 ⟶ 12,120:
| | | | | | | | | | |Q| | | |
</pre>
 
=={{header|Processing}}==
{{trans|Java}}
<syntaxhighlight lang="java">
int n = 8;
int[] b = new int[n];
int s = 0;
int y = 0;
 
void setup() {
size(400, 400);
textAlign(CENTER, CENTER);
textFont(createFont("DejaVu Sans", 44));
b[0] = -1;
}
 
void draw() {
if (y >= 0) {
do {
b[y]++;
} while ((b[y] < n) && unsafe(y));
if (b[y] < n) {
if (y < (n-1)) {
b[++y] = -1;
} else {
drawBoard();
}
} else {
y--;
}
} else {
textSize(18);
text("Press any key to restart", width / 2, height - 20);
}
}
 
 
boolean unsafe(int y) {
int x = b[y];
for (int i = 1; i <= y; i++) {
int t = b[y - i];
if (t == x ||
t == x - i ||
t == x + i) {
return true;
}
}
return false;
}
 
void drawBoard() {
float w = width / n;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
fill(255 * ((x + y) % 2));
square(x * w, y * w, w);
if (b[y] == x) {
fill(255 - 255 * ((x + y) % 2));
textSize(42);
text("♕", w / 2 + x *w, w /2 + y * w);
}
}
}
fill(255, 0, 0);
textSize(18);
text("Solution " + (++s), width / 2, height / 90);
}
 
void keyPressed() {
b = new int[n];
s = 0;
y = 0;
b[0] = -1;
}
</syntaxhighlight>
 
==={{header|Processing Python mode}}===
{{trans|Python}}
 
This solution, originally by Raymond Hettinger for demonstrating the power of the itertools module, generates all solutions.
 
<syntaxhighlight lang="python">from itertools import permutations, product
 
n = 8
cols = range(n)
i = 0 # solution shown
 
solutions = [vec for vec in permutations(cols)
if n == len(set(vec[i] + i for i in cols))
== len(set(vec[i] - i for i in cols))]
 
def setup():
size(400, 400)
textAlign(CENTER, CENTER)
textFont(createFont("DejaVu Sans", 44))
 
def draw():
background(0)
w = width / n
for x, y in product(range(n), range(n)):
fill(255 * ((x + y) % 2))
square(x * w, y * w, w)
if solutions[i][y] == x:
fill(255 - 255 * ((x + y) % 2))
text(u'♕', w / 2 + x * w, w / 3 + y * w)
 
def keyPressed(): # show next solution
global i
i = (i + 1) % len(solutions)
</syntaxhighlight>
 
=={{header|Prolog}}==
Line 8,347 ⟶ 12,235:
 
Solution #1:
<langsyntaxhighlight Prologlang="prolog">solution([]).
solution([X/Y|Others]) :-
Line 8,367 ⟶ 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,397 ⟶ 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,419 ⟶ 12,307:
del(Item,[First|List],[First|List1]) :-
del(Item,List,List1).</langsyntaxhighlight>
 
[http://ideone.com/Y6olN Output]:
Line 8,427 ⟶ 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,442 ⟶ 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,448 ⟶ 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,470 ⟶ 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,487 ⟶ 12,375:
===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,499 ⟶ 12,387:
</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===
Line 8,521 ⟶ 12,422:
</ul>
<br/>
<langsyntaxhighlight Prologlang="prolog">:- use_module(library(clpfd)).
 
% DOC: http://www.pathwayslms.com/swipltuts/clpfd/clpfd.html
Line 8,593 ⟶ 12,494:
main :-
print_nqueens_all(8).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 8,621 ⟶ 12,522:
From the Pure (programming language) Wikipedia page
 
<langsyntaxhighlight lang="pure">/*
n-queens.pure
Tectonics:
Line 8,639 ⟶ 12,540:
 
compiling || (puts("queens 4: " + str(queens 4)) $$
puts("Solutions to queens 7: " + str(#queens 7)));</langsyntaxhighlight>
 
{{out}}
Line 8,657 ⟶ 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,736 ⟶ 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,857 ⟶ 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,866 ⟶ 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,884 ⟶ 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,901 ⟶ 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,925 ⟶ 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 9,018 ⟶ 12,931:
 
# queenPuzzle :: Int -> Int -> [[Int]]
def queenPuzzle(nRows, nCols):
'''All board patterns of this dimension
in which no two Queens share a row,
Line 9,035 ⟶ 12,948:
go(lessRows),
[]
) if nRows0 >< 0nRows else [[]]
return go(nRows)
 
 
Line 9,051 ⟶ 12,964:
 
 
# TEST --------------------------- TEST -------------------------
# main :: IO ()
def main():
Line 9,057 ⟶ 12,970:
 
n = 5
xs = queenPuzzle(n, )(n)
 
print(
Line 9,068 ⟶ 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 9,119 ⟶ 12,999:
return '\n\n'.join(map(
showBlock,
chunksOf(nCols)([
list(map(showBoard,(b) for b in bs))
])
))
return lambda boards: go(boards)
 
 
Line 9,133 ⟶ 13,013:
def showLine(n):
return ('.' * (n - 1)) + '♛' + ('.' * (lng - n))
return list(map(showLine, xs))
 
 
Line 9,152 ⟶ 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 9,178 ⟶ 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 9,220 ⟶ 13,252:
NEXT icol
END SUB
</syntaxhighlight>
</lang>
 
=={{header|R}}==
Line 9,228 ⟶ 13,260:
This solution uses recursive backtracking.
 
<langsyntaxhighlight lang="r">queens <- function(n) {
a <- seq(n)
u <- rep(T, 2 * n - 1)
Line 9,255 ⟶ 13,287:
aux(1)
m
}</langsyntaxhighlight>
 
Show the first solution found for size 8 as a permutation matrix.
 
<langsyntaxhighlight Rlang="r">library(Matrix)
a <- queens(8)
as(a[, 1], "pMatrix")</langsyntaxhighlight>
 
{{out}}
Line 9,278 ⟶ 13,310:
Count solutions for board size 4 to 12.
 
<langsyntaxhighlight Rlang="r">sapply(4:12, function(n) ncol(queens(n)))</langsyntaxhighlight>
 
{{out}}
Line 9,288 ⟶ 13,320:
Backtracking algorithm; returns one solution
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 9,319 ⟶ 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,333 ⟶ 13,365:
 
(show-nqueens 8)
</syntaxhighlight>
</lang>
 
[[image:Racket-nqueens.png]]
Line 9,345 ⟶ 13,377:
Computes all solutions.
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 9,391 ⟶ 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,420 ⟶ 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,480 ⟶ 13,512:
(define (print-queens n)
(for ([x (queens n)]) (displayln (string-join x))))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 9,487 ⟶ 13,519:
Neither pretty nor efficient, a simple backtracking solution
 
<syntaxhighlight lang="raku" perl6line>sub MAIN(\N = 8) {
sub collision(@field, $row) {
for ^$row -> $i {
Line 9,510 ⟶ 13,542:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[0 4 7 5 2 6 1 3]</pre>
Line 9,516 ⟶ 13,548:
=={{header|Rascal}}==
 
<langsyntaxhighlight Rascallang="rascal">import Prelude;
 
public set[list[int]] Nqueens(int n){
Line 9,525 ⟶ 13,557:
result += vector;}
return result;
}</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 9,534 ⟶ 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.*/
Line 9,583 ⟶ 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,654 ⟶ 13,686:
 
=={{header|Ring}}==
<langsyntaxhighlight 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"
Line 9,682 ⟶ 13,786:
Tiles = newlist(size,size)
TitleMoves = null
lineSize = null
LayoutButtonRow = list(size)
LayoutButtonMain = null
Line 9,727 ⟶ 13,832:
# Set the Window Icon
setWindowIcon(new qIcon(new qPixmap(WQueen)))
win.setminimumwidth(500700)
win.setminimumheight(500700)
 
Button = newList(size, size) ### Internal Array with Letters
Line 9,739 ⟶ 13,844:
move(moveX, moveY)
resize(500700,500700)
 
wwidth = win.width()
Line 9,762 ⟶ 13,867:
}
 
labelSizesizeBtn = new qLineEditQPushButton(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)")
Line 9,768 ⟶ 13,880:
setAlignment( Qt_AlignHCenter)
setAlignment( Qt_AlignVCenter)
setreturnPressedEvent("newBoardSize()")
setText(" Size: ")
setText(" 8 ")
}
 
cmbSize = new qComboBox(win)
{
setStyleSheet("background-color:rgb(255,255,204)")
setFont(new qFont("Calibri",fontsize,100,0))
aList = 4:8
for x in aList additem(string(x),0) next
setcurrentIndex(0)
setactivatedEvent("newSize()")
}
 
Line 9,820 ⟶ 13,923:
setSpacing(C_Spacing)
setContentsMargins(0,0,0,0)
}
LayoutTitleRow.AddWidget(TitleMoves)
LayoutTitleRow.AddWidget(labelSizesizeBtn)
LayoutTitleRow.AddWidget(cmbSizelineSize)
LayoutTitleRow.AddWidget(SolveGame)
LayoutTitleRow.AddWidget(NewGame)
Line 9,887 ⟶ 13,989:
 
return
 
###============================================================
 
func newSize()
nSize = cmbSize.currentText()
nSize = number(nSize)
count = 0
newWindow(nSize)
 
###============================================================
 
func newBoardSize()
nrSize = number(lineSize.text())
newWindow(nrSize)
 
###============================================================
Line 9,894 ⟶ 14,010:
for Row = 1 to size
for Col = 1 to size
Tiles[Row][Col] = 0
Button[Row][Col].delete()
next
Line 9,902 ⟶ 14,017:
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()
Line 9,960 ⟶ 14,082:
win.setLayout(LayoutButtonMain)
 
###============================================================
 
func newSize()
nSize = cmbSize.currentText()
nSize = number(nSize)
count = 0
newWindow(nSize)
 
###============================================================
Line 10,165 ⟶ 14,280:
func UserLeftClick Row,Col
 
sleep(0.3)
Tiles[Row][Col] = 1
 
Line 10,218 ⟶ 14,334:
setButtonImage(Button[Row][Col],oPicGreen,bwidth-8,bheight-8)
ok
Tiles[Row][Col] = 01
ok
 
Line 10,329 ⟶ 14,445:
msgBox("You Win!")
ok
 
###============================================================
</syntaxhighlight>
</lang>
[httphttps://www.mediafire.com/viewfile/lchwxwt4wcov3u053bxu7kpuc4tlx5/WQueenImages.pngzip/file Necessary imageimages]
 
=={{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 10,391 ⟶ 14,508:
end
(1 .. 15).each {|n| puts "n=#{n}"; puts n_queens(n); puts}</langsyntaxhighlight>
 
{{out}}
Line 10,545 ⟶ 14,662:
===Alternate solution===
If there is not specification, it outputs all solutions.
<langsyntaxhighlight lang="ruby">class Queen
attr_reader :count
Line 10,583 ⟶ 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 10,594 ⟶ 14,711:
puzzle = Queen.new(n, false) # do not display
puts " #{n} Queen : #{puzzle.count}"
end</langsyntaxhighlight>
 
{{out}}
Line 10,730 ⟶ 14,847:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">[loop]
input "How many queens (N>=4)";n
if n < 4 then
Line 10,841 ⟶ 14,958:
end if
end if
next</langsyntaxhighlight>
<pre>abcdefgh
* 8
Line 10,853 ⟶ 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 10,885 ⟶ 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 10,898 ⟶ 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 10,917 ⟶ 15,036:
 
pub struct NQueens {
iterator: Box<Iterator<dyn Item=NQueensSolution>>
}
 
Line 10,965 ⟶ 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 11,025 ⟶ 15,183:
put n m;
keep p1-p8;
run;</langsyntaxhighlight>
 
=={{header|Scala}}==
Line 11,033 ⟶ 15,191:
Lazily generates permutations with an <code>Iterator</code>.
 
<langsyntaxhighlight lang="scala">
object NQueens {
 
Line 11,083 ⟶ 15,241:
}
}
</syntaxhighlight>
</lang>
 
<pre>
Line 11,114 ⟶ 15,272:
This is a simple breadth-first technique to retrieve all solutions.
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme write)
Line 11,189 ⟶ 15,347:
(pretty-print (n-queens 5) 5)
(pretty-print (n-queens 8) 8)
</syntaxhighlight>
</lang>
 
{{out}}
Line 11,263 ⟶ 15,421:
=={{header|Scilab}}==
Naive brute-force search.
<syntaxhighlight lang="scilab">//Length of board side
Board_size = 8;
 
Line 11,404 ⟶ 15,562:
string(Board_size)+"x"+string(Board_size)+" board.");
//Time elapsed
disp("Time: "+string(toc())+"s.");</langsyntaxhighlight>
 
{{out}}
Line 11,413 ⟶ 15,571:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
var array integer: board is 8 times 0;
Line 11,463 ⟶ 15,621:
end if;
end while;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func N_queens_solution(N = 8) {
 
func collision(field, row) {
Line 11,497 ⟶ 15,655:
for n in (1..15) {
say "#{'%2d' % n}: #{N_queens_solution(n) || 'No solution'}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 11,518 ⟶ 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 11,542 ⟶ 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 11,600 ⟶ 15,758:
};
 
stdout.printf("%d solutions\n", n_queens(range(8), 0));</langsyntaxhighlight>
 
=={{header|SQL}}==
Line 11,606 ⟶ 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 11,637 ⟶ 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 11,681 ⟶ 16,158:
(* NONE *)
queens(2);
</syntaxhighlight>
</lang>
 
=={{header|Stata}}==
Line 11,687 ⟶ 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 11,744 ⟶ 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 11,757 ⟶ 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 11,793 ⟶ 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 11,860 ⟶ 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 11,901 ⟶ 16,378:
 
endprogram
</syntaxhighlight>
</lang>
 
=={{header|Tailspin}}==
A functional-ish solution utilising tailspin's data flows
<langsyntaxhighlight lang="tailspin">
templates queens
def n: $;
Line 11,915 ⟶ 16,392:
def major: $ + $prev::length + 1;
// If prev is not an array that contains row, send it on...
$prev -> \(when <~[<=$row>]> do $ !\)
-> \(when <?($ -> \[i]($ - $i !\) <~[<=$minor>]>)> do $ !\)
-> \(when <?($ -> \[i]($ + $i !\) <~[<=$major>]>)> do $ !\)
-> [ $..., $row] !
end addIfPossible
Line 11,923 ⟶ 16,400:
end addColumn
1..$n -> [$] -> #
when <[]($n)> do $ !
<>otherwise $ -> addColumn -> #
end queens
 
Line 11,937 ⟶ 16,414:
'For 3 queens there are $:[3 -> queens] -> $::length; solutions
' -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 11,946 ⟶ 16,423:
 
A solution using state to find one solution if any exist
<langsyntaxhighlight lang="tailspin">
templates queens
def n: $;
templates getRowColumn
when <?($@queens.freeRows($.r::raw) <=0>)> do 0 !
when <?($@queens.freeMaxs($.r::raw + $.c::raw) <=0>)> do 0 !
when <?($@queens.freeMins($.c::raw - $.r::raw + $n) <=0>)> do 0 !
<>otherwise 1!
end getRowColumn
 
sink setRowColumn
def p: $;
@queens.freeRows($p.r::raw): $p.val::raw;
@queens.freeMaxs($p.c::raw + $p.r::raw): $p.val::raw;
@queens.freeMins($p.c::raw - $p.r::raw + $n): $p.val::raw;
end setRowColumn
 
data done <=1>
 
templates placeQueen
def c: $;
row´1 -> #
when <done> do 1!
<=-1> 1! // Use -1 to signal successful completion
when <=row´($n+1)> do 0 !
when <?({r: $, c: $c} -> getRowColumn <=1>)> do
def r: $;
@queens.queenRows($r::raw): $c;
{r: $, c: $c, val: 0} -> !setRowColumn
$c -> \(<=col´$n> -done´1!
<?(col´($c::raw + 1) -> placeQueen <=1>)> -done´1!
<>
{r: $r, c: $c, val: 1} -> !setRowColumn
row´($r::raw + 1) !\) -> #
<>otherwise row´($::raw + 1) -> #
end placeQueen
 
Line 11,984 ⟶ 16,463:
freeMins: [1..$n*2 -> 1],
queenRows: [1..$n -> -1] };
col´1 -> placeQueen -> \(<=1> $@queens.queenRows ! <> 'non-existent'!\)!
end queens
 
Line 11,993 ⟶ 16,472:
'A solution to the 3 queens problem is $:3 -> queens;
' -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 12,005 ⟶ 16,484:
 
{{works with|Tcl|8.5}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
proc unsafe {y} {
Line 12,051 ⟶ 16,530:
}
 
main [expr {$argc ? int(0+[lindex $argv 0]) : 8}]</langsyntaxhighlight>
{{out}}
<pre>$ tclsh8.5 8queens.tcl 6
Line 12,094 ⟶ 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 12,171 ⟶ 16,650:
work
out
depose</langsyntaxhighlight>
 
=={{header|Ursala}}==
Line 12,177 ⟶ 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 12,194 ⟶ 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 12,212 ⟶ 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 12,269 ⟶ 16,748:
Debug.Print n, m 'number of queens, number of solutions
Next n
End Sub 'n_queens</langsyntaxhighlight>
{{out}}
<pre>
Line 12,292 ⟶ 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 12,337 ⟶ 16,816:
Loop Until i=0
wscript.echo n &":"& m
next 'n</langsyntaxhighlight>
{{out}}
<pre>
Line 12,360 ⟶ 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 12,417 ⟶ 16,896:
Debug.Print n, m 'number of queens, number of solutions
Next n
End Sub 'n_queens</langsyntaxhighlight>
{{out}}
<pre>
Line 12,439 ⟶ 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 12,499 ⟶ 16,978:
Next n
End Sub 'n_queens
End Module</langsyntaxhighlight>
{{out}}
<pre>
Line 12,520 ⟶ 16,999:
 
=={{header|Wart}}==
<langsyntaxhighlight Wartlang="wart">def (nqueens n queens)
prn "step: " queens # show progress
if (len.queens = n)
Line 12,543 ⟶ 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 12,617 ⟶ 17,195:
int main () {
return queen (8);
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
[[File:NQueensXPL0.GIF|right]]
<langsyntaxhighlight XPL0lang="xpl0">def N=8; \board size (NxN)
int R, C; \row and column of board
char B(N,N); \board
Line 12,677 ⟶ 17,255:
C:= 0; \start at left column
Try;
]</langsyntaxhighlight>
 
=={{header|XSLT}}==
Line 12,683 ⟶ 17,261:
(either by XSLT processors saxon-6.5.5, xsltproc, xalan,
or any of the big5 browsers):
<langpre>
15863724
16837425
Line 12,689 ⟶ 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 12,708 ⟶ 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 12,835 ⟶ 17,413:
 
</xsl:stylesheet>
</syntaxhighlight>
</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 12,902 ⟶ 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 12,915 ⟶ 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