RosettaCodeData/Task/Topswops/XPL0/topswops-2.xpl0
Ingy döt Net 68f8f3e56b all tasks
2013-04-11 01:07:29 -07:00

36 lines
813 B
Text

code CrLf=9, IntOut=11, Text=12;
int N, D, Best(16);
proc TrySwaps(A, F, S);
int A, F, S;
int B(16), I, J, K;
[if D > Best(N) then Best(N):= D;
loop [if A(S)=-1 ! A(S)=S then quit;
if D+Best(S) <= Best(N) then return;
if S = 0 then quit;
S:= S-1;
];
D:= D+1;
for I:= 0 to S do B(I):= A(I);
K:= 1;
for I:= 1 to S do
[K:= K<<1;
if B(I)=-1 & (F&K)=0 ! B(I)=I then
[J:= I; B(0):= J;
while J do [J:= J-1; B(I-J):= A(J)];
TrySwaps(B, F!K, S);
];
];
D:= D-1;
];
int I, X(16);
[for I:= 0 to 16-1 do
[X(I):= -1; Best(I):= 0];
X(0):= 0;
for N:= 1 to 13 do
[D:= 0;
TrySwaps(X, 1, N-1);
IntOut(0, N); Text(0, ": "); IntOut(0, Best(N)); CrLf(0);
];
]