Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,9 @@
|
|||
safe[q_List, n_] :=
|
||||
With[{l = Length@q},
|
||||
Length@Union@q == Length@Union[q + Range@l] ==
|
||||
Length@Union[q - Range@l] == l]
|
||||
nQueen[q_List: {}, n_] :=
|
||||
If[safe[q, n],
|
||||
If[Length[q] == n, {q},
|
||||
Cases[nQueen[Append[q, #], n] & /@ Range[n],
|
||||
Except[{Null} | {}], {2}]], Null]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
matrixView[n_] :=
|
||||
Grid[Normal@
|
||||
SparseArray[MapIndexed[{#, First@#2} -> "Q" &, #], {n, n}, "."],
|
||||
Frame -> All] & /@ nQueen[n]
|
||||
matrixView[6] // OutputForm
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
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 *)
|
||||
tot=0;
|
||||
Do[y=Abs[w[[q,1,1]]-w[[q,2,1]]];x=Abs[w[[q,1,2]]-w[[q,2,2]]];If[x==y,tot++],{q,1,Length[w]}];(* x and y are the abs values of x1-y1 and x2-y2 if equal they are on same diagonal *)
|
||||
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]}]
|
||||
44
Task/N-queens-problem/Mathematica/n-queens-problem-4.math
Normal file
44
Task/N-queens-problem/Mathematica/n-queens-problem-4.math
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
dispSol[sol_] := sol /. {1 -> "Q" , 0 -> "-"} // Grid
|
||||
|
||||
solveNqueens[n_] :=
|
||||
Module[{c, m, b, vars}, c = cqueens[n]; m = mqueens[n];
|
||||
vars = mqueens2[n]; b = bqueens[Length[m]];
|
||||
Partition[LinearProgramming[c, m, b, vars, Integers], n]]
|
||||
|
||||
cqueens[n_] := Table[-1, {i, n^2}]
|
||||
|
||||
bqueens[l_] := Table[{1, -1}, {i, l}]
|
||||
|
||||
mqueens2[n_] := Table[{0, 1}, {i, n^2}]
|
||||
|
||||
mqueens[n_] :=
|
||||
Module[{t, t2, t3, t4}, t = mqueensh[n]; t2 = Append[t, mqueensv[n]];
|
||||
t3 = Append[t2, mqueensd[n]]; t4 = Append[t3, mqueensdm[n]];
|
||||
Partition[Flatten[t4], n^2]]
|
||||
|
||||
mqueensh[n_] :=
|
||||
Module[{t}, t = Table[0, {i, n}, {j, n^2}];
|
||||
For[i = 1, i <= n, i++,
|
||||
For[j = 1, j <= n, j++, t[[i, ((i - 1)*n) + j]] = 1]]; t]
|
||||
|
||||
mqueensv[n_] :=
|
||||
Module[{t}, t = Table[0, {i, n}, {j, n^2}];
|
||||
For[i = 1, i <= n, i++,
|
||||
For[j = 1, j <= n, j++, t[[j, ((i - 1)*n) + j]] = 1]]; t]
|
||||
|
||||
mqueensd[n_] :=
|
||||
Module[{t}, t = Table[0, {i, (2*n) - 1}, {j, n^2}];
|
||||
For[k = 2, k <= 2 n, k++,
|
||||
For[i = 1, i <= n, i++,
|
||||
For[j = 1, j <= n, j++,
|
||||
If[i + j == k, t[[k - 1, ((i - 1)*n) + j]] = 1]]]]; t]
|
||||
|
||||
mqueensdm[n_] :=
|
||||
Module[{t}, t = Table[0, {i, Sum[1, {i, 1 - n, n - 1}]}, {j, n^2}];
|
||||
For[k = 1 - n, k <= n - 1, k++,
|
||||
For[i = 1, i <= n, i++,
|
||||
For[j = 1, j <= n, j++,
|
||||
If[i == j - k, t[[k + n, ((i - 1)*n) + j]] = 1]]]]; t]
|
||||
|
||||
|
||||
solveNqueens[8] // dispSol
|
||||
Loading…
Add table
Add a link
Reference in a new issue