汉诺塔问题 实例
procedure TForm1.move(x,y:char);
begin
listbox1.Items.add(x+'-'+'-'+'>'+y);
inc(i);
end;
procedure TForm1.hanoi(n:integer;one,two,three:char);
{将n个盘从one座借助two座,移动到three座}
begin
if n=1 then move(one,three) //one座上最后一个盘移到three座//
else
begin
hanoi(n-1,one,three,two); //将n-1个盘借助three座完成one-->two//
move(one,three); //one座上最后一个盘移到three座//
hanoi(n-1,two,one,three); //将n-1个盘借助one座完成two-->three//
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
n:integer;
begin
n :=strtoint(edit1.Text);
hanoi(n,'A','B','C');
showmessage('一共用了i='+inttostr(i)+'步');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Application.Terminate;
end;