#Fixed by Paul Raff, March 1, 2005 Help:=proc():print(`Ini(n), Hanoi(n,i,j),H(n,i,j)`):end: #Ini(n): the initial state Ini:=proc(n,j) local i: if j=1 then [[seq(i,i=1..n)],[],[]]: elif j=2 then [[],[seq(i,i=1..n)],[]]: else [[],[],[seq(i,i=1..n)]]: fi: end: #Pref(S,n,i) - places n in the beginning of the ith index of every element of S #S should be a list of Hanoi states - i.e. a list of 3 lists #So S = [[[list 1],[list 2],[list 3]], etc. etc.] Pref:=proc(S,n,j) local S1,n1: n1:=nops(S); if j=1 then S1:=[seq([[op(S[i][1]),n],S[i][2],S[i][3]],i=1..n1)]; elif j=2 then S1:=[seq([S[i][1],[op(S[i][2]),n],S[i][3]],i=1..n1)]; else S1:=[seq([S[i][1],S[i][2],[op(S[i][3]),n]],i=1..n1)]; fi: return(S1); end: #H(n,i,j): the list of states to save the world #for moving n rings from Tower i to Tower j H:=proc(n,i,j) local k,S1,S2,i1,S: if n=1 then return([Ini(n,i),Ini(n,j)]): fi: k:=op(1,{1,2,3} minus {i,j}): S1:=H(n-1,i,k): S2:=H(n-1,k,j): S:=[op(Pref(S1,n,i)),op(Pref(S2,n,j))]; return S; end: #Hanoi(n,i,j) - a pretty format for the Towers of Hanoi Hanoi := proc(n,i,j) local Srow,a,n_Srow,Scolumn: Srow := H(n,i,j); n_Srow:=nops(Srow); Scolumn:=[]; for a from 1 to n_Srow do Scolumn:=[op(Scolumn),[convert(Srow[a][1],Vector),convert(Srow[a][2],Vector),convert(Srow[a][3],Vector)]]: od: Scolumn: end: