[Problem description]
From left to right, 4 fine column a,b,c,d are placed sequentially. A column with n (n<=20) of the same diameter, from the top to the next with a continuous lowercase letter a,b,c,... Numbers that move these discs through the b,c one-way to the D post (that is, it is not allowed to move from right to left.) Discs can be staged in b,c). It is required to find the moving process from the initial state of a column to the target state of the D column.
Input: The first row is the total number of disks on the D column, and the second row is the sequence of the lower-to-upper disk on the D-column.
Output: is a file. Each action of the file is a sequence of letters such as "K M L", where k is the disc number, M is the original bar of the K disk, and L is the target bar number. Output "No solution!" if no arrangement is generated
[Sample input]
3 a b c
[Sample Output]
c A b b a c a a D b c d C B D
[Problem analysis]
Too lazy to write (DAI) solution (MA) ..... Directly on the code
Here are the standard procedures:
ProgramP1_7(input,output);varGA,GB,GC,GD,FD:Array[0.. the] ofchar; topa,topb,topc,topf:byte; Resl:Array[1.. -] of RecordCode:char; Source:char; Target:charEnd; Order:Array['0'..'Z'] ofbyte; i,n,kz,ks:byte; J:char ;procedurePrint;varI:byte;beginI:=1; Repeatwriteln (resl[i].code:5, Resl[i].source:5, Resl[i].target:5); Inc (I)untilResl[i].code='0';End;procedureMove1 (vartopa,topb,topc,ks:byte);varT:byte;begin Repeat if(topa=0) and(topb=0) and(topc=0) Then beginPrint;halt (1); kz:=1 End; T:=0; ifGa[topa]=fd[topf] Then begin{a-d}T:=t+1; KS:=ks+1; Resl[ks].code:=Ga[topa]; Resl[ks].source:='A'; Resl[ks].target:='D'; Topf:=topf+1; topa:=topa-1; End; ifGb[topb]=fd[topf] Then begin{b-d}T:=t+1; KS:=ks+1; Resl[ks].code:=GB[TOPB]; Resl[ks].source:='B'; Resl[ks].target:='D'; Topf:=topf+1; topb:=topb-1; End; ifGc[topc]=fd[topf] Then begin {c-d}T:=t+1; KS:=ks+1; Resl[ks].code:=GC[TOPC]; Resl[ks].source:='C'; Resl[ks].target:='D'; Topf:=topf+1; topc:=topc-1; End; untilt=0;End;procedureMove2 (vartopa,topb,topc,ks:byte);varTp,ks1:byte;beginmove1 (TOPA,TOPB,TOPC,KS); KS1:=ks+1; fortp:=1 to 3 Do CaseTp of 1:{A--b} ifTopa>0 Then beginResl[ks1].code:=Ga[topa]; Resl[ks1].source:='A'; Resl[ks1].target:='B'; TOPB:=topb+1; gb[topb]:=ga[topa];topa:=topa-1; Move2 (TOPA,TOPB,TOPC,KS1); Topa:=topa+1;d EC (TOPB); End; 2:{a--c} if(Order[ga[topa]]<order[gc[topc]]) and(topa>0) Then beginResl[ks1].code:=Ga[topa]; Resl[ks1].source:='A'; Resl[ks1].target:='C'; TOPC:=topc+1; gc[topc]:=ga[topa];topa:=topa-1; Move2 (TOPA,TOPB,TOPC,KS1); Topa:=topa+1;d EC (TOPC); End; 3:{b--c} if(Order[gb[topb]]<order[gc[topc]]) and(topb>0) Then beginResl[ks1].code:=GB[TOPB]; Resl[ks1].source:='B'; Resl[ks1].target:='C'; TOPC:=topc+1; gc[topc]:=GB[TOPB];d EC (TOPB); Move2 (TOPA,TOPB,TOPC,KS1); TOPB:=topb+1;d EC (TOPC)End; End{ case and for};End;begin{Main}assign (input,'word.in'); Reset (input); Assign (output,'Word.out'); Rewrite (output); READLN (n); fori:=1 toN Do Read(Fd[i]); I:=1;Repeat forj:='a' toChr (Ord ('a') +n-1) Do ifJ=fd[i] Thenorder[j]:=i;i:=i+1untilI>n;order['0']:=0; order['1']:= -; fori:=1 to $ Doresl[i].code:='0'; fori:=1 toN DoGA[I]:=CHR (Ord ('a') +i-1); Topa:=n;topb:=0; topc:=0; topf:=1; kz:=0; KS:=0; ga[0]:='0'; gb[0]:='0'; gc[0]:='1'; move2 (topa,topb,topc,ks);ifkz=0 ThenWriteln ('No solution!');End.