Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
37
Task/N-queens-problem/GAP/n-queens-problem.gap
Normal file
37
Task/N-queens-problem/GAP/n-queens-problem.gap
Normal 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 ] ]
|
||||
Loading…
Add table
Add a link
Reference in a new issue