Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -1,50 +0,0 @@
with Ada.Text_IO; use Ada.Text_IO;
procedure Queens is
Board : array (1..8, 1..8) of Boolean := (others => (others => False));
function Test (Row, Column : Integer) return Boolean is
begin
for J in 1..Column - 1 loop
if ( Board (Row, J)
or else
(Row > J and then Board (Row - J, Column - J))
or else
(Row + J <= 8 and then Board (Row + J, Column - J))
) then
return False;
end if;
end loop;
return True;
end Test;
function Fill (Column : Integer) return Boolean is
begin
for Row in Board'Range (1) loop
if Test (Row, Column) then
Board (Row, Column) := True;
if Column = 8 or else Fill (Column + 1) then
return True;
end if;
Board (Row, Column) := False;
end if;
end loop;
return False;
end Fill;
begin
if not Fill (1) then
raise Program_Error;
end if;
for I in Board'Range (1) loop
Put (Integer'Image (9 - I));
for J in Board'Range (2) loop
if Board (I, J) then
Put ("|Q");
elsif (I + J) mod 2 = 1 then
Put ("|/");
else
Put ("| ");
end if;
end loop;
Put_Line ("|");
end loop;
Put_Line (" A B C D E F G H");
end Queens;

View file

@ -1,49 +0,0 @@
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;

View file

@ -1,7 +1,7 @@
result: new []
result: []
queens: function [n, i, a, b, c][
if? i < n [
switch i < n [
loop 1..n 'j [
if all? @[
not? contains? a j
@ -11,8 +11,7 @@ queens: function [n, i, a, b, c][
queens n, i+1, a ++ @[j], b ++ @[i+j], c ++ @[i-j]
]
]
else [
][
if n = size a ->
'result ++ @[a]
]
@ -24,8 +23,8 @@ queens BoardSize, 0, [], [], []
loop result 'solution [
loop solution 'col [
line: new repeat "-" BoardSize
line\[col-1]: `Q`
line: repeat "-" BoardSize
line\[col-1]: 'Q'
print line
]
print ""

View file

@ -1,80 +0,0 @@
(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)
)

View file

@ -1,55 +1,57 @@
with javascript_semantics
--
-- demo\rosetta\n_queens.exw
-- =========================
--
sequence co, -- columns occupied
-- (ro is implicit)
fd, -- forward diagonals
bd, -- backward diagonals
board
(phixonline)(not pygments)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\n_queens.exw
-- =========================
--</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">co</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- columns occupied
-- (ro is implicit)</span>
<span style="color: #000000;">fd</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- forward diagonals</span>
<span style="color: #000000;">bd</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- backward diagonals</span>
<span style="color: #000000;">board</span>
atom count
<span style="color: #004080;">atom</span> <span style="color: #000000;">count</span>
procedure solve(integer row, integer N, integer show)
for col=1 to N do
if not co[col] then
integer fdi = col+row-1,
bdi = row-col+N
if not fd[fdi]
and not bd[bdi] then
board[row][col] = 'Q'
co[col] = true
fd[fdi] = true
bd[bdi] = true
if row=N then
if show then
puts(1,join(board,"\n")&"\n")
puts(1,repeat('=',N)&"\n")
end if
count += 1
else
solve(row+1,N,show)
end if
board[row][col] = '.'
co[col] = false
fd[fdi] = false
bd[bdi] = false
end if
end if
end for
end procedure
<span style="color: #008080;">procedure</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">col</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">co</span><span style="color: #0000FF;">[</span><span style="color: #000000;">col</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">fdi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">col</span><span style="color: #0000FF;">+</span><span style="color: #000000;">row</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">bdi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">-</span><span style="color: #000000;">col</span><span style="color: #0000FF;">+</span><span style="color: #000000;">N</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">fd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">fdi</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">and</span> <span style="color: #008080;">not</span> <span style="color: #000000;">bd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">bdi</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">row</span><span style="color: #0000FF;">][</span><span style="color: #000000;">col</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'Q'</span>
<span style="color: #000000;">co</span><span style="color: #0000FF;">[</span><span style="color: #000000;">col</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #000000;">fd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">fdi</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #000000;">bd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">bdi</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">=</span><span style="color: #000000;">N</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">show</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'='</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">row</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">show</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">row</span><span style="color: #0000FF;">][</span><span style="color: #000000;">col</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'.'</span>
<span style="color: #000000;">co</span><span style="color: #0000FF;">[</span><span style="color: #000000;">col</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #000000;">fd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">fdi</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #000000;">bd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">bdi</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
procedure n_queens(integer N=8, integer show=1)
co = repeat(false,N)
fd = repeat(false,N*2-1)
bd = repeat(false,N*2-1)
board = repeat(repeat('.',N),N)
count = 0
solve(1,N,show)
printf(1,"%d queens: %d solutions\n",{N,count})
end procedure
<span style="color: #008080;">procedure</span> <span style="color: #000000;">n_queens</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">=</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">co</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">fd</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">bd</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">board</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">),</span><span style="color: #000000;">N</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">show</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d queens: %d solutions\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
for N=1 to iff(platform()=JS?12:14) do
n_queens(N,N<5)
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">12</span><span style="color: #0000FF;">:</span><span style="color: #000000;">14</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">n_queens</span><span style="color: #0000FF;">(</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;"><</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--

View file

@ -1,59 +0,0 @@
function PlaceQueen ( [ref]$Board, $Row, $N )
{
# For the current row, start with the first column
$Board.Value[$Row] = 0
# While haven't exhausted all columns in the current row...
While ( $Board.Value[$Row] -lt $N )
{
# If not the first row, check for conflicts
$Conflict = $Row -and
( (0..($Row-1)).Where{ $Board.Value[$_] -eq $Board.Value[$Row] }.Count -or
(0..($Row-1)).Where{ $Board.Value[$_] -eq $Board.Value[$Row] - $Row + $_ }.Count -or
(0..($Row-1)).Where{ $Board.Value[$_] -eq $Board.Value[$Row] + $Row - $_ }.Count )
# If no conflicts and the current column is a valid column...
If ( -not $Conflict -and $Board.Value[$Row] -lt $N )
{
# If this is the last row
# Board completed successfully
If ( $Row -eq ( $N - 1 ) )
{
return $True
}
# Recurse
# If all nested recursions were successful
# Board completed successfully
If ( PlaceQueen $Board ( $Row + 1 ) $N )
{
return $True
}
}
# Try the next column
$Board.Value[$Row]++
}
# Everything was tried, nothing worked
Return $False
}
function Get-NQueensBoard ( $N )
{
# Start with a default board (array of column positions for each row)
$Board = @( 0 ) * $N
# Place queens on board
# If successful...
If ( PlaceQueen -Board ([ref]$Board) -Row 0 -N $N )
{
# Convert board to strings for display
$Board | ForEach { ( @( "" ) + @(" ") * $_ + "Q" + @(" ") * ( $N - $_ ) ) -join "|" }
}
Else
{
"There is no solution for N = $N"
}
}

View file

@ -1,7 +0,0 @@
Get-NQueensBoard 8
''
Get-NQueensBoard 3
''
Get-NQueensBoard 4
''
Get-NQueensBoard 14

View file

@ -1,4 +1,5 @@
N ← 8
N ← 8
Good ← =1⧻◴[⧻◴⟜(∩(⧻◴)⊃+-⇡⧻.)⟜⧻]
⊙◌⍥(⊏⊚≡Good.☇1⊞⊂⇡,),[[]]N
≡(≡(/(⊂⊂)@|/⊂⍜(⊡|@Q◌):↯:@_)⊸⧻)
⊙◌⍥(⊏⊚≡Good.♭₂⊞⊂⇡:⊙.):⊙.[[]]N
↙2 &p$"_ solutions, two examples are:\n"⊸⧻
≡(&s≡(/(⊂⊂)@|/⊂⍜(⊡|@Q◌):↯:@_)⊸⧻)

View file

@ -1,46 +0,0 @@
'N-queens problem - non recursive & structured - vbs - 24/02/2017
const l=15
dim a(),s(),u(): redim a(l),s(l),u(4*l-2)
for i=1 to l: a(i)=i: next
for n=1 to l
m=0
i=1
j=0
r=2*n-1
Do
i=i-1
j=j+1
p=0
q=-r
Do
i=i+1
u(p)=1
u(q+r)=1
z=a(j): a(j)=a(i): a(i)=z 'swap a(i),a(j)
p=i-a(i)+n
q=i+a(i)-1
s(i)=j
j=i+1
Loop Until j>n Or u(p)<>0 Or u(q+r)<>0
If u(p)=0 Then
If u(q+r)=0 Then
m=m+1 'm: number of solutions
'x="": for k=1 to n: x=x&" "&a(k): next: msgbox x,,m
End If
End If
j=s(i)
Do While j>=n And i<>0
Do
z=a(j): a(j)=a(i): a(i)=z 'swap a(i),a(j)
j=j-1
Loop Until j<i
i=i-1
p=i-a(i)+n
q=i+a(i)-1
j=s(i)
u(p)=0
u(q+r)=0
Loop
Loop Until i=0
wscript.echo n &":"& m
next 'n

View file

@ -1,10 +1,11 @@
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
{ ( not qs.filter1(isAttacked,r,c) ) }
// ( (r,c), x,y ) : is queen at r,c attacked by q@(x,y)?
fcn isAttacked(q, x,y){ r,c := q; (r==x or c==y or r+c==x+y or r-c==x-y) }
// queen safe at (r,c)?, qs=( (r,c),(r,c)..) solution so far
fcn isSafe(r,c,qs){ ( not qs.filter1(isAttacked,r,c) ) }
fcn queensN(N=8,row=1,queens=T){
qs:=[1..N].filter(isSafe.fpM("101",row,queens)) #isSafe(row,?,( (r,c),(r,c).. )
.apply(fcn(c,r,qs){ qs.append(T(r,c)) },row,queens);
if (row == N) return(qs);
return(qs.apply(self.fcn.fp(N,row+1)).flatten());
// isSafe(row,?,( (r,c),(r,c).. )
qs := [1..N].filter(isSafe.fpM("101",row,queens))
.apply(fcn(c,r,qs){ qs.append(T(r,c)) },row,queens);
if(row == N) return(qs);
return(qs.apply( self.fcn.fp(N,row+1) ).flatten());
}

View file

@ -1,3 +1,8 @@
queens := queensN(4);
N, queens := 4, queensN(N);
println(queens.len()," solution(s):");
queens.apply2(Console.println);
foreach s in (queens){
println(s,":");
b = (b := List.createLong(N,"-")).apply(b.copy); // NxN array
s.apply2('wrap([(r,c)]){ b[r - 1][c - 1] = "Q" });
b.apply("concat","").concat("\n").println();
}