Help:=proc(): print(`W(a,b), Wt(HorizA,VertA,w)`): print(`BPb(HorizA,VertA,a,b), BP1(HorizA,VertA,a,b) `): print(`BP(L1,L2,a,b)`): end: W:=proc(a,b) local S1,S2,i: option remember: if a=0 then RETURN({ [seq([0,i],i=0..b) ]}): elif b=0 then RETURN({ [seq([i,0],i=0..a) ]}): fi: S1:=W(a,b-1): S2:=W(a-1,b): {seq([op(S1[i]),[a,b]],i=1..nops(S1)), seq([op(S2[i]),[a,b]],i=1..nops(S2))}: end: #Wt(HorizA,VertB,w): Given the Horiz. and Vertical attraction #tables (given as lists of lists, #where HorizA[i][j] is #the number of attractions between [i-1,j-1] to [i-1,j] #where VertA[i][j] is #the number of attractions between [i-1,j-1] to [i,j-1] Wt:=proc(HorizA,VertA,w) local c,i,w1,pt1,pt2: if w=[[0,0]] then RETURN(0): fi: w1:=[op(1..nops(w)-1,w)]: c:=Wt(HorizA,VertA,w1): pt1:=w[nops(w)-1]: pt2:=w[nops(w)]: if pt1[2]=pt2[2] then RETURN(c+ HorizA[pt1[1]+1][pt1[2]+1]): elif pt1[1]=pt2[1] then RETURN(c+ VertA[pt1[1]+1][pt1[2]+1]): else print(`Something is wrong`): RETURN(FAIL): fi: end: #BPb(HorizA,VertA,a,b): The best path from [0,0] to [a,b] #given the Attraction Lists HorizA, VertA BPb:=proc(HorizA,VertA,a,b) local S,champ,rec,i,wt1,cand : S:=W(a,b): champ:=S[1]: rec:=Wt(HorizA,VertA,S[1]): for i from 2 to nops(S) do cand:=S[i]: wt1:=Wt(HorizA,VertA,S[i]): if wt1>rec then rec:=wt1: champ:=cand: fi: od: RETURN(champ,rec): end: #BP1(L1,L2,a,b): Best (highest weight) path from #[0,0] to [a,b] (with unit steps) with weights given #by L1 (number of attractions in the horiz. direction) # and L2(" vert. ") #followed by its score BP1:=proc(L1,L2,a,b) local Lchamp, Dchamp,Lrec, Drec,tem: option remember: if a=0 then RETURN(W(0,b)[1],Wt(L1,L2,W(0,b)[1])): elif b=0 then RETURN(W(a,0)[1],Wt(L1,L2,W(a,0)[1])): fi: tem:=BP1(L1,L2,a-1,b): Lchamp:=tem[1]: Lrec:=tem[2]: Lchamp:=[op(Lchamp),[a,b]]: Lrec:=Lrec+L1[a][b+1]: tem:=BP1(L1,L2,a,b-1): Dchamp:=tem[1]: Drec:=tem[2]: Dchamp:=[op(Dchamp),[a,b]]: Drec:=Drec+L2[a+1][b]: if Drec>Lrec then RETURN(Dchamp,Drec): else RETURN(Lchamp,Lrec): fi: end: BP:=proc(L1,L2,a,b) local i,j: for i from 0 to a do for j from 0 to b do BP1(L1,L2,i,j): od: od: BP1(L1,L2,a,b): end: