Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
110
Task/N-queens-problem/360-Assembly/n-queens-problem.360
Normal file
110
Task/N-queens-problem/360-Assembly/n-queens-problem.360
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
* N-queens problem 04/09/2015
|
||||
NQUEENS PROLOG
|
||||
LA R9,1 n=1
|
||||
LOOPN CH R9,L do n=1 to l
|
||||
BH ELOOPN if n>l then exit loop
|
||||
SR R8,R8 m=0
|
||||
LA R10,1 i=1
|
||||
LR R5,R9 n
|
||||
SLA R5,1 n*2
|
||||
BCTR R5,0 r=2*n-1
|
||||
E40 CR R10,R9 if i>n
|
||||
BH E80 then goto e80
|
||||
LR R11,R10 j=i
|
||||
E50 LR R1,R10 i
|
||||
SLA R1,1 i*2
|
||||
LA R6,A-2(R1) r6=@a(i)
|
||||
LR R1,R11 j
|
||||
SLA R1,1 j*2
|
||||
LA R7,A-2(R1) r7=@a(j)
|
||||
MVC Z,0(R6) z=a(i)
|
||||
MVC Y,0(R7) y=a(j)
|
||||
LR R3,R10 i
|
||||
SH R3,Y -y
|
||||
AR R3,R9 p=i-y+n
|
||||
LR R4,R10 i
|
||||
AH R4,Y +y
|
||||
BCTR R4,0 q=i+y-1
|
||||
MVC 0(2,R6),Y a(i)=y
|
||||
MVC 0(2,R7),Z a(j)=z
|
||||
LR R1,R3 p
|
||||
SLA R1,1 p*2
|
||||
LH R2,U-2(R1) u(p)
|
||||
LTR R2,R2 if u(p)<>0
|
||||
BNE E60 then goto e60
|
||||
LR R1,R4 q
|
||||
AR R1,R5 q+r
|
||||
SLA R1,1 (q+r)*2
|
||||
LH R2,U-2(R1) u(q+r)
|
||||
C R2,=F'0' if u(q+r)<>0
|
||||
BNE E60 then goto e60
|
||||
LR R1,R10 i
|
||||
SLA R1,1 i*2
|
||||
STH R11,S-2(R1) s(i)=j
|
||||
LA R0,1 r0=1
|
||||
LR R1,R3 p
|
||||
SLA R1,1 p*2
|
||||
STH R0,U-2(R1) u(p)=1
|
||||
LR R1,R4 q
|
||||
AR R1,R5 q+r
|
||||
SLA R1,1 (q+r)*2
|
||||
STH R0,U-2(R1) u(q+r)=1
|
||||
LA R10,1(R10) i=i+1
|
||||
B E40 goto e40
|
||||
E60 LA R11,1(R11) j=j+1
|
||||
CR R11,R9 if j<=n
|
||||
BNH E50 then goto e50
|
||||
E70 BCTR R11,0 j=j-1
|
||||
CR R11,R10 if j=i
|
||||
BE E90 goto e90
|
||||
LR R1,R10 i
|
||||
SLA R1,1 i*2
|
||||
LA R6,A-2(R1) r6=@a(i)
|
||||
LR R1,R11 j
|
||||
SLA R1,1 j*2
|
||||
LA R7,A-2(R1) r7=@a(j)
|
||||
MVC Z,0(R6) z=a(i)
|
||||
MVC 0(2,R6),0(R7) a(i)=a(j)
|
||||
MVC 0(2,R7),Z a(j)=z;
|
||||
B E70 goto e70
|
||||
E80 LA R8,1(R8) m=m+1
|
||||
E90 BCTR R10,0 i=i-1
|
||||
LTR R10,R10 if i=0
|
||||
BZ ZERO then goto zero
|
||||
LR R1,R10 i
|
||||
SLA R1,1 i*2
|
||||
LH R2,A-2(R1) r2=a(i)
|
||||
LR R3,R10 i
|
||||
SR R3,R2 -a(i)
|
||||
AR R3,R9 p=i-a(i)+n
|
||||
LR R4,R10 i
|
||||
AR R4,R2 +a(i)
|
||||
BCTR R4,0 q=i+a(i)-1
|
||||
LR R1,R10 i
|
||||
SLA R1,1 i*2
|
||||
LH R11,S-2(R1) j=s(i)
|
||||
LA R0,0 r0=0
|
||||
LR R1,R3 p
|
||||
SLA R1,1 p*2
|
||||
STH R0,U-2(R1) u(p)=0
|
||||
LR R1,R4 q
|
||||
AR R1,R5 q+r
|
||||
SLA R1,1 (q+r)*2
|
||||
STH R0,U-2(R1) u(q+r)=0
|
||||
B E60 goto e60
|
||||
ZERO XDECO R9,PG+0 edit n
|
||||
XDECO R8,PG+12 edit m
|
||||
XPRNT PG,24 print buffer
|
||||
LA R9,1(R9) n=n+1
|
||||
B LOOPN loop do n
|
||||
ELOOPN EPILOG
|
||||
L DC H'12' input value
|
||||
A DC H'01',H'02',H'03',H'04',H'05',H'06'
|
||||
DC H'07',H'08',H'09',H'10',H'11',H'12'
|
||||
U DC 46H'0'
|
||||
S DS 12H
|
||||
Z DS H
|
||||
Y DS H
|
||||
PG DS CL24 buffer
|
||||
YREGS
|
||||
END NQUEENS
|
||||
80
Task/N-queens-problem/ATS/n-queens-problem.ats
Normal file
80
Task/N-queens-problem/ATS/n-queens-problem.ats
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
(* ****** ****** *)
|
||||
//
|
||||
// Solving N-queen puzzle
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
// How to test:
|
||||
// ./queens
|
||||
// How to compile:
|
||||
// patscc -DATS_MEMALLOC_LIBC -o queens queens.dats
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
#include
|
||||
"share/atspre_staload.hats"
|
||||
//
|
||||
#include
|
||||
"share/HATS/atspre_staload_libats_ML.hats"
|
||||
//
|
||||
(* ****** ****** *)
|
||||
|
||||
fun
|
||||
solutions(N:int) = let
|
||||
//
|
||||
fun
|
||||
show
|
||||
(
|
||||
board: list0(int)
|
||||
) : void =
|
||||
(
|
||||
list0_foreach<int>
|
||||
( list0_reverse(board)
|
||||
, lam(n) => ((N).foreach()(lam(i) => print_string(if i = n then " Q" else " _")); print_newline())
|
||||
) ;
|
||||
print_newline()
|
||||
)
|
||||
//
|
||||
fun
|
||||
safe
|
||||
(
|
||||
i: int, j: int, k: int, xs: list0(int)
|
||||
) : bool =
|
||||
(
|
||||
case+ xs of
|
||||
| nil0() => true
|
||||
| cons0(x, xs) => x != i && x != j && x != k && safe(i, j+1, k-1, xs)
|
||||
)
|
||||
//
|
||||
fun
|
||||
loop
|
||||
(
|
||||
col: int, xs: list0(int)
|
||||
) : void =
|
||||
(N).foreach()
|
||||
(
|
||||
lam(i) =>
|
||||
if
|
||||
safe(i, i+1, i-1, xs)
|
||||
then let
|
||||
val xs = cons0(i, xs)
|
||||
in
|
||||
if col = N then show(xs) else loop(col+1, xs)
|
||||
end // end of [then]
|
||||
)
|
||||
//
|
||||
in
|
||||
loop(1, nil0())
|
||||
end // end of [solutions]
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
val () = solutions(8)
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
implement main0() = ()
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
(* end of [queens.dats] *)
|
||||
49
Task/N-queens-problem/Ada/n-queens-problem-2.ada
Normal file
49
Task/N-queens-problem/Ada/n-queens-problem-2.ada
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
with Ada.Text_IO;
|
||||
use Ada.Text_IO;
|
||||
|
||||
procedure CountQueens is
|
||||
function Queens (N : Integer) return Long_Integer is
|
||||
A : array (0 .. N) of Integer;
|
||||
U : array (0 .. 2 * N - 1) of Boolean := (others => true);
|
||||
V : array (0 .. 2 * N - 1) of Boolean := (others => true);
|
||||
M : Long_Integer := 0;
|
||||
|
||||
procedure Sub (I: Integer) is
|
||||
K, P, Q: Integer;
|
||||
begin
|
||||
if N = I then
|
||||
M := M + 1;
|
||||
else
|
||||
for J in I .. N - 1 loop
|
||||
P := I + A (J);
|
||||
Q := I + N - 1 - A (J);
|
||||
if U (P) and then V (Q) then
|
||||
U (P) := false;
|
||||
V (Q) := false;
|
||||
K := A (I);
|
||||
A (I) := A (J);
|
||||
A (J) := K;
|
||||
Sub (I + 1);
|
||||
U (P) := true;
|
||||
V (Q) := true;
|
||||
K := A (I);
|
||||
A (I) := A (J);
|
||||
A (J) := K;
|
||||
end if;
|
||||
end loop;
|
||||
end if;
|
||||
end Sub;
|
||||
begin
|
||||
for I in 0 .. N - 1 loop
|
||||
A (I) := I;
|
||||
end loop;
|
||||
Sub (0);
|
||||
return M;
|
||||
end Queens;
|
||||
begin
|
||||
for N in 1 .. 16 loop
|
||||
Put (Integer'Image (N));
|
||||
Put (" ");
|
||||
Put_Line (Long_Integer'Image (Queens (N)));
|
||||
end loop;
|
||||
end CountQueens;
|
||||
69
Task/N-queens-problem/C/n-queens-problem-4.c
Normal file
69
Task/N-queens-problem/C/n-queens-problem-4.c
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#include <stdio.h>
|
||||
#define MAXN 31
|
||||
|
||||
int nqueens(int n)
|
||||
{
|
||||
int q0,q1;
|
||||
int cols[MAXN], diagl[MAXN], diagr[MAXN], posibs[MAXN]; // Our backtracking 'stack'
|
||||
int num=0;
|
||||
//
|
||||
// The top level is two fors, to save one bit of symmetry in the enumeration by forcing second queen to
|
||||
// be AFTER the first queen.
|
||||
//
|
||||
for (q0=0; q0<n-2; q0++) {
|
||||
for (q1=q0+2; q1<n; q1++){
|
||||
int bit0 = 1<<q0;
|
||||
int bit1 = 1<<q1;
|
||||
int d=0; // d is our depth in the backtrack stack
|
||||
cols[0] = bit0 | bit1 | (-1<<n); // The -1 here is used to fill all 'coloumn' bits after n ...
|
||||
diagl[0]= (bit0<<1 | bit1)<<1;
|
||||
diagr[0]= (bit0>>1 | bit1)>>1;
|
||||
|
||||
// The variable posib contains the bitmask of possibilities we still have to try in a given row ...
|
||||
int posib = ~(cols[0] | diagl[0] | diagr[0]);
|
||||
|
||||
while (d >= 0) {
|
||||
while(posib) {
|
||||
int bit = posib & -posib; // The standard trick for getting the rightmost bit in the mask
|
||||
int ncols= cols[d] | bit;
|
||||
int ndiagl = (diagl[d] | bit) << 1;
|
||||
int ndiagr = (diagr[d] | bit) >> 1;
|
||||
int nposib = ~(ncols | ndiagl | ndiagr);
|
||||
posib^=bit; // Eliminate the tried possibility.
|
||||
|
||||
// The following is the main additional trick here, as recognizing solution can not be done using stack level (d),
|
||||
// since we save the depth+backtrack time at the end of the enumeration loop. However by noticing all coloumns are
|
||||
// filled (comparison to -1) we know a solution was reached ...
|
||||
// Notice also that avoiding an if on the ncols==-1 comparison is more efficient!
|
||||
num += ncols==-1;
|
||||
|
||||
if (nposib) {
|
||||
if (posib) { // This if saves stack depth + backtrack operations when we passed the last possibility in a row.
|
||||
posibs[d++] = posib; // Go lower in stack ..
|
||||
}
|
||||
cols[d] = ncols;
|
||||
diagl[d] = ndiagl;
|
||||
diagr[d] = ndiagr;
|
||||
posib = nposib;
|
||||
}
|
||||
}
|
||||
posib = posibs[--d]; // backtrack ...
|
||||
}
|
||||
}
|
||||
}
|
||||
return num*2;
|
||||
}
|
||||
|
||||
|
||||
main(int ac , char **av)
|
||||
{
|
||||
if(ac != 2) {
|
||||
printf("usage: nq n\n");
|
||||
return 1;
|
||||
}
|
||||
int n = atoi(av[1]);
|
||||
if(n<1 || n > MAXN) {
|
||||
printf("n must be between 2 and 31!\n");
|
||||
}
|
||||
printf("Number of solution for %d is %d\n",n,nqueens(n));
|
||||
}
|
||||
|
|
@ -1,45 +1,40 @@
|
|||
(defun queens (nmax)
|
||||
(let ((a (make-array `(,nmax)))
|
||||
(s (make-array `(,nmax)))
|
||||
(u (make-array `(,(- (* 4 nmax) 2)) :initial-element 0))
|
||||
y z i j p q r m (v nil))
|
||||
(dotimes (i nmax) (setf (aref a i) i))
|
||||
(loop for n from 1 to nmax do
|
||||
(tagbody
|
||||
(setf m 0 i 0 r (1- (* 2 n)))
|
||||
(go L40)
|
||||
L30
|
||||
(setf (aref s i) j (aref u p) 1 (aref u (+ q r)) 1)
|
||||
(incf i)
|
||||
L40
|
||||
(if (>= i n) (go L80))
|
||||
(defun queens1 (n)
|
||||
(let ((a (make-array n))
|
||||
(s (make-array n))
|
||||
(u (make-array (list (- (* 4 n) 2)) :initial-element t))
|
||||
y z (i 0) j p q (r (1- (* 2 n))) (m 0))
|
||||
(dotimes (i n) (setf (aref a i) i))
|
||||
(tagbody
|
||||
L1
|
||||
(if (>= i n) (go L5))
|
||||
(setf j i)
|
||||
L50
|
||||
L2
|
||||
(setf y (aref a j) z (aref a i))
|
||||
(setf p (+ (- i y) (1- n)) q (+ i y))
|
||||
(setf p (+ (- i y) n -1) q (+ i y))
|
||||
(setf (aref a i) y (aref a j) z)
|
||||
(if (and (zerop (aref u p)) (zerop (aref u (+ q r)))) (go L30))
|
||||
L60
|
||||
(when (and (aref u p) (aref u (+ q r)))
|
||||
(setf (aref s i) j (aref u p) nil (aref u (+ q r)) nil)
|
||||
(incf i)
|
||||
(go L1))
|
||||
L3
|
||||
(incf j)
|
||||
(if (< j n) (go L50))
|
||||
L70
|
||||
(if (< j n) (go L2))
|
||||
L4
|
||||
(decf j)
|
||||
(if (= j i) (go L90))
|
||||
(if (= j i) (go L6))
|
||||
(rotatef (aref a i) (aref a j))
|
||||
(go L70)
|
||||
L80
|
||||
(go L4)
|
||||
L5
|
||||
(incf m)
|
||||
L90
|
||||
L6
|
||||
(decf i)
|
||||
(if (minusp i) (go L100))
|
||||
(setf p (+ (- i (aref a i)) (1- n)) q (+ i (aref a i)) j (aref s i))
|
||||
(setf (aref u p) 0 (aref u (+ q r)) 0)
|
||||
(go L60)
|
||||
L100
|
||||
;(princ n) (princ " ") (princ m) (terpri)
|
||||
(push (cons n m) v)
|
||||
)) (reverse v)))
|
||||
(if (minusp i) (go L7))
|
||||
(setf p (+ (- i (aref a i)) n -1) q (+ i (aref a i)) j (aref s i))
|
||||
(setf (aref u p) t (aref u (+ q r)) t)
|
||||
(go L3)
|
||||
L7)
|
||||
m))
|
||||
|
||||
> (queens 14)
|
||||
> (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))
|
||||
|
|
|
|||
21
Task/N-queens-problem/Common-Lisp/n-queens-problem-3.lisp
Normal file
21
Task/N-queens-problem/Common-Lisp/n-queens-problem-3.lisp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
(defun queens2 (n)
|
||||
(let ((a (make-array n))
|
||||
(u (make-array (+ n n -1) :initial-element t))
|
||||
(v (make-array (+ n n -1) :initial-element t))
|
||||
(m 0))
|
||||
(dotimes (i n) (setf (aref a i) i))
|
||||
(labels ((sub (i)
|
||||
(if (= i n)
|
||||
;(push (copy-seq a) s)
|
||||
(incf m)
|
||||
(loop for k from i below n do
|
||||
(let ((p (+ i (aref a k)))
|
||||
(q (+ (- i (aref a k)) n -1)))
|
||||
(when (and (aref u p) (aref v q))
|
||||
(setf (aref u p) nil (aref v q) nil)
|
||||
(rotatef (aref a i) (aref a k))
|
||||
(sub (1+ i))
|
||||
(setf (aref u p) t (aref v q) t)
|
||||
(rotatef (aref a i) (aref a k))))))))
|
||||
(sub 0))
|
||||
m))
|
||||
|
|
@ -29,9 +29,9 @@ printQueens(List q) {
|
|||
StringBuffer sb = new StringBuffer();
|
||||
for (int j=0; j<N; j++) {
|
||||
if (q[i] == j) {
|
||||
sb.add("Q ");
|
||||
sb.write("Q ");
|
||||
} else {
|
||||
sb.add("* ");
|
||||
sb.write("* ");
|
||||
}
|
||||
}
|
||||
print(sb.toString());
|
||||
|
|
|
|||
36
Task/N-queens-problem/Elixir/n-queens-problem.elixir
Normal file
36
Task/N-queens-problem/Elixir/n-queens-problem.elixir
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
defmodule RC do
|
||||
def queen(n) do
|
||||
add = Tuple.duplicate(true, 2*n-1)
|
||||
sub = Tuple.duplicate(true, 2*n-1)
|
||||
solve(n, [], add, sub)
|
||||
end
|
||||
|
||||
def solve(n, row, _, _) when n <= length(row) do
|
||||
print(n,row)
|
||||
1
|
||||
end
|
||||
def solve(n, row, add, sub) do
|
||||
Enum.map(Enum.to_list(0..n-1) -- row, fn x ->
|
||||
iadd = x + (len = length(row))
|
||||
isub = if (y = x-len) < 0, do: y + 2*n - 1, else: y
|
||||
if elem(add, iadd) and elem(sub, isub) do
|
||||
solve(n, [x|row], put_elem(add,iadd,false), put_elem(sub,isub,false))
|
||||
else
|
||||
0
|
||||
end
|
||||
end) |> Enum.sum
|
||||
end
|
||||
|
||||
def print(n, row) do
|
||||
IO.puts frame = "+-" <> String.duplicate("--", n) <> "+"
|
||||
Enum.each(row, fn x ->
|
||||
line = Enum.map_join(0..n-1, fn i -> if x==i, do: "Q ", else: ". " end)
|
||||
IO.puts "| #{line}|"
|
||||
end)
|
||||
IO.puts frame
|
||||
end
|
||||
end
|
||||
|
||||
Enum.each(1..6, fn n ->
|
||||
IO.puts " #{n} Queen : #{RC.queen(n)}"
|
||||
end)
|
||||
2
Task/N-queens-problem/J/n-queens-problem-2.j
Normal file
2
Task/N-queens-problem/J/n-queens-problem-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
$queenst 8
|
||||
92 8
|
||||
2
Task/N-queens-problem/J/n-queens-problem-3.j
Normal file
2
Task/N-queens-problem/J/n-queens-problem-3.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
{.queenst 8
|
||||
0 4 7 5 2 6 1 3
|
||||
|
|
@ -65,6 +65,7 @@ return $solutions;
|
|||
|
||||
// This is a function which will render the board
|
||||
function renderBoard($p,$boardX) {
|
||||
$img = 'data:image/png;base64,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';
|
||||
echo "<table border=1 cellspacing=0 style='text-align:center;display:inline'>";
|
||||
for ($y = 0; $y < $boardX; ++$y) {
|
||||
echo '<tr>';
|
||||
|
|
@ -72,7 +73,7 @@ for ($y = 0; $y < $boardX; ++$y) {
|
|||
if (($x+$y) & 1) { $cellCol = '#9C661F';}
|
||||
else {$cellCol = '#FCE6C9';}
|
||||
|
||||
if ($p[$y] == 1 << $x) { echo "<td bgcolor=".$cellCol."><img width=30 height=30 src='./images/blackqueen.png'></td>";}
|
||||
if ($p[$y] == 1 << $x) { echo "<td bgcolor=".$cellCol."><img width=30 height=30 src='".$img."'></td>";}
|
||||
else { echo "<td bgcolor=".$cellCol."> </td>";}
|
||||
}
|
||||
echo '<tr>';
|
||||
|
|
|
|||
|
|
@ -1,30 +1,24 @@
|
|||
sub MAIN($N = 8) {
|
||||
sub MAIN(\N = 8) {
|
||||
sub collision(@field, $row) {
|
||||
for ^$row -> $i {
|
||||
my $distance = @field[$i] - @field[$row];
|
||||
return 1 if $distance == any(0, $row - $i, $i - $row);
|
||||
return True if $distance == any(0, $row - $i, $i - $row);
|
||||
}
|
||||
0;
|
||||
False;
|
||||
}
|
||||
sub search(@field is rw, $row) {
|
||||
if $row == $N {
|
||||
return @field;
|
||||
} else {
|
||||
for ^$N -> $i {
|
||||
@field[$row] = $i;
|
||||
if !collision(@field, $row) {
|
||||
my @r = search(@field, $row + 1) and return @r;
|
||||
}
|
||||
}
|
||||
return @field if $row == N;
|
||||
for ^N -> $i {
|
||||
@field[$row] = $i;
|
||||
return search(@field, $row + 1) || next
|
||||
unless collision(@field, $row);
|
||||
}
|
||||
Nil;
|
||||
()
|
||||
}
|
||||
for 0 .. $N / 2 {
|
||||
if my @f = search [$_], 1 {
|
||||
say ~@f;
|
||||
for 0 .. N / 2 {
|
||||
if search [$_], 1 -> @f {
|
||||
say @f;
|
||||
last;
|
||||
}
|
||||
}
|
||||
}
|
||||
# output:
|
||||
0 4 7 5 2 6 1 3
|
||||
|
|
|
|||
|
|
@ -3,18 +3,21 @@ def queens(n):
|
|||
up = [True]*(2*n - 1)
|
||||
down = [True]*(2*n - 1)
|
||||
def sub(i):
|
||||
nonlocal a, up, down
|
||||
for k in range(i, n):
|
||||
j = a[k]
|
||||
p = i + j
|
||||
q = i - j + n - 1
|
||||
if up[p] and down[q]:
|
||||
if i == n - 1:
|
||||
yield tuple(a)
|
||||
else:
|
||||
if i == n:
|
||||
yield tuple(a)
|
||||
else:
|
||||
for k in range(i, n):
|
||||
j = a[k]
|
||||
p = 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]
|
||||
yield from sub(i + 1)
|
||||
up[p] = down[q] = True
|
||||
a[i], a[k] = a[k], a[i]
|
||||
yield from sub(0)
|
||||
|
||||
#Count solutions for n=8:
|
||||
sum(1 for p in queens(8))
|
||||
92
|
||||
|
|
|
|||
31
Task/N-queens-problem/Python/n-queens-problem-6.py
Normal file
31
Task/N-queens-problem/Python/n-queens-problem-6.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
def queens_lex(n):
|
||||
a = list(range(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):
|
||||
a[i], a[k] = a[k], a[i]
|
||||
j = a[i]
|
||||
p = i + j
|
||||
q = i - j + n - 1
|
||||
if up[p] and down[q]:
|
||||
up[p] = down[q] = False
|
||||
yield from sub(i + 1)
|
||||
up[p] = down[q] = True
|
||||
x = a[i]
|
||||
for k in range(i + 1, n):
|
||||
a[k - 1] = a[k]
|
||||
a[n - 1] = x
|
||||
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, ...
|
||||
40
Task/N-queens-problem/Ruby/n-queens-problem-2.rb
Normal file
40
Task/N-queens-problem/Ruby/n-queens-problem-2.rb
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
class Queen
|
||||
def initialize(num=8)
|
||||
@num = num
|
||||
end
|
||||
|
||||
def solve(out=true)
|
||||
@out = out
|
||||
@row = *0...@num
|
||||
@frame = "+-" + "--" * @num + "+"
|
||||
@count = 0
|
||||
add = Array.new(2 * @num - 1, true)
|
||||
sub = Array.new(2 * @num - 1, true)
|
||||
_solve([], add, sub)
|
||||
@count
|
||||
end
|
||||
|
||||
def _solve(row, add, sub)
|
||||
y = row.size
|
||||
if y == @num
|
||||
print_out(row) if @out
|
||||
@count += 1
|
||||
else
|
||||
(@row-row).each do |x|
|
||||
next unless add[x+y] and sub[x-y]
|
||||
add[x+y] = sub[x-y] = false
|
||||
_solve(row+[x], add, sub)
|
||||
add[x+y] = sub[x-y] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def print_out(row)
|
||||
puts @frame
|
||||
row.each do |i|
|
||||
line = @num.times.map {|j| j==i ? "Q " : ". "}.join
|
||||
puts "| #{line}|"
|
||||
end
|
||||
puts @frame
|
||||
end
|
||||
end
|
||||
9
Task/N-queens-problem/Ruby/n-queens-problem-3.rb
Normal file
9
Task/N-queens-problem/Ruby/n-queens-problem-3.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(1..6).each do |n|
|
||||
puzzle = Queen.new(n)
|
||||
puts " #{n} Queen : #{puzzle.solve}"
|
||||
end
|
||||
|
||||
(7..12).each do |n|
|
||||
puzzle = Queen.new(n)
|
||||
puts " #{n} Queen : #{puzzle.solve(false)}" # no display
|
||||
end
|
||||
|
|
@ -1,44 +1,33 @@
|
|||
// rustc 0.10-pre, 24th Feb
|
||||
static side: i8 = 8;
|
||||
static queens: i8 = 8; //change side and queens to modify parameters
|
||||
const N: usize = 8;
|
||||
|
||||
fn place(mut board: [i8,..side*side], ix:i8) -> Option<[i8,..side*side]> {
|
||||
if board[ix] == 0 {
|
||||
return None
|
||||
};
|
||||
board[ix] = -1;
|
||||
let i1 = ix/side;
|
||||
let j1 = ix % side;
|
||||
for k in range(1,side) {
|
||||
let mut loc :i8 = i1 * side + k;
|
||||
board[loc] = 0;
|
||||
loc = k * side + j1;
|
||||
board[loc] = 0;
|
||||
loc = (i1-k) * side + (j1-k);
|
||||
if loc / side == i1 -k && loc % side == j1 - k && loc != ix && loc >= 0 { board[loc] = 0 };
|
||||
loc = loc + 2 * k;
|
||||
if loc / side == i1 - k && loc % side == j1 + k && loc != ix && loc >= 0 { board[loc] = 0};
|
||||
loc = loc + 2 * side * k;
|
||||
if loc / side == i1 + k && loc % side == j1 + k && loc != ix && loc < side*side { board[loc] = 0};
|
||||
loc = loc - 2 * k;
|
||||
if loc / side == i1 + k && loc % side == j1 - k && loc != ix && loc < side*side { board[loc] = 0};
|
||||
}
|
||||
Some(board)
|
||||
}
|
||||
|
||||
fn tryplace(b : [i8,..side*side],ix:i8, nq: i8, mut score: u32) -> u32 {
|
||||
if nq == queens { return score + 1 }
|
||||
for ind in range(ix, side*side) {
|
||||
score = match place(b, ind) {
|
||||
Some(b2) => tryplace(b2, ind+1, nq+1, score),
|
||||
None() => score
|
||||
};
|
||||
}
|
||||
return score
|
||||
fn try(mut board: &mut [[bool; N]; N], row: usize, mut count: &mut i64) {
|
||||
if row == N {
|
||||
*count += 1;
|
||||
for r in board.iter() {
|
||||
println!("{}", r.iter().map(|&x| if x {"x"} else {"."}.to_string()).collect::<Vec<String>>().join(" "))
|
||||
}
|
||||
println!("");
|
||||
return
|
||||
}
|
||||
for i in 0..N {
|
||||
let mut ok: bool = true;
|
||||
for j in 0..row {
|
||||
if board[j][i]
|
||||
|| i+j >= row && board[j][i+j-row]
|
||||
|| i+row < N+j && board[j][i+row-j]
|
||||
{ ok = false }
|
||||
}
|
||||
if ok {
|
||||
board[row][i] = true;
|
||||
try(&mut board, row+1, &mut count);
|
||||
board[row][i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let b : [i8, ..side*side] = [1,..side*side];
|
||||
let score = tryplace(b, 0, 0, 0);
|
||||
println!("{}", score)
|
||||
let mut board: [[bool; N]; N] = [[false; N]; N];
|
||||
let mut count: i64 = 0;
|
||||
try (&mut board, 0, &mut count);
|
||||
println!("Found {} solutions", count)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue