Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -0,0 +1,37 @@
# Quick and dirty solution, checking all permutations without backtracking (thus it's slow)
IsSafe := function(a)
local n, i, j;
n := Length(a);
for i in [1 .. n - 1] do
for j in [i + 1 .. n] do
if AbsInt(a[j] - a[i]) = j - i then
return false;
fi;
od;
od;
return true;
end;
Queens := function(n)
local p, a, v;
v := [];
for p in SymmetricGroup(n) do
a := List([1 .. n], i -> i^p);
if IsSafe(a) then
Add(v, a);
fi;
od;
return v;
end;
v := Queens(8);;
Length(v);
PrintArray(PermutationMat(PermListList([1 .. 8], v[1]), 8));
[ [ 0, 0, 1, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 1, 0, 0 ],
[ 0, 1, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 1, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 1, 0 ],
[ 0, 0, 0, 1, 0, 0, 0, 0 ] ]