关于汉诺塔问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于汉诺塔问题相关的知识,希望对你有一定的参考价值。
怎样修改程序使得能够控制汉诺塔移动快慢
我是新手啊,请大家帮帮我
Dim pan(10) As Shape
Dim l(3) As Integer
Dim p(3) As Integer
Dim k As Integer
Dim h(3, 10) As Integer
Dim num As Integer
Private Sub Command1_Click()
For i = 1 To num
h(1, i) = i
Next
p(1) = num
k = 0
Call han(num, 1, 2, 3)
s = l(1): l(1) = l(3): l(3) = s
For i = 1 To 3
p(i) = 0
Next
End Sub
Private Sub Form_Activate()
num = Val(Text1.Text)
For i = 1 To num
pan(i).Left = pan(i).Tag
pan(i).Visible = True
Next
For i = num + 1 To 10
pan(i).Visible = False
Next
For i = 1 To 3
l(i) = Line1(i - 1).X1
Next
End Sub
Private Sub Form_Load()
For i = 1 To 10
Set pan(i) = Shape1(i - 1)
pan(i).Tag = pan(i).Left
Next
End Sub
Private Sub han(m As Integer, u As Integer, v As Integer, w As Integer)
If m = 1 Then
Call yidong(u, w)
Else
Call han(m - 1, u, w, v)
Call yidong(u, w)
Call han(m - 1, v, u, w)
End If
End Sub
Private Sub yidong(X As Integer, Y As Integer)
a = p(X)
b = p(Y) + 1
h(Y, b) = h(X, a)
pan(h(Y, b)).Top = 200
Call yianshi(0.1)
pan(h(Y, b)).Left = l(Y) - pan(h(Y, b)).Width / 2
Call yianshi(0.2)
pan(h(Y, b)).Top = 1700 - p(Y) * 100
Call yianshi(0.1)
p(X) = a - 1
p(Y) = b
k = k + 1
Label3.Caption = "移动了" + LTrim(Str(k)) + "次"
Form1.Refresh
End Sub
Private Sub yianshi(pausetime)
Dim start
start = Timer
Do While Timer < start + pausetime
Loop
End Sub
Private Sub Text1_Change()
n = Val(Text1.Text)
If n > 2 And n < 10 Then
Form_Activate
End If
End Sub
并能将移动过程写入txt
Program Hanoi(input,output);
uses
crt;
type
a3=array[1..3] of integer;
var
n :integer;
Col,Flag:a3;
(* print a disk size is n on (x,y) *)
Procedure Disk(x,y,n:integer);
var
i:integer;
Begin
for i:=0 to n do
begin
gotoxy(x+i,y);write(\'o\');
gotoxy(x-i,y);write(\'o\');
gotoxy(80,25);
end;
end;
(* a sound procedure when move a disk *)
Procedure Sing;
var
i,freq:integer;
Begin
Randomize;
for i:=1 to 10 do
begin
freq:=Random(29)+1;
(* Sound(freq); *)
Delay(20);
(* NoSound; *)
end;
end;
(* clear a disk size is n on (x,y) *)
Procedure ClrDisk(x,y,n:integer);
var
i:integer;
Begin
for i:=n downto 0 do
begin
gotoxy(x+i,y);write(\' \');
gotoxy(x-i,y);write(\' \');
end;
end;
(* initiate procedure *)
Procedure Initiate;
var
i:integer;
Begin
clrscr;
repeat input n
gotoxy(5,5);
write(\'Enter the number of disks ( 1<= n <= 11): \');
ClrEol;read(n);
until (0<n) and (n<12);
clrscr; version information
gotoxy(28,1);
write(\'** \',n,\'-Hanoi Problem **\');
gotoxy(32,2);
write(\'2010.06 aWei\');
for i:=8 to 19 do three pointers
begin
gotoxy(15,i);write(\'|\');
gotoxy(40,i);write(\'|\');
gotoxy(65,i);write(\'|\');
end;
for i:=1 to 80 do the bottom
begin
gotoxy(i,20);write(\'_\');
end;
print A,B,C
gotoxy(15,21);write(\'A\');
gotoxy(40,21);write(\'B\');
gotoxy(65,21);write(\'C\');
for i:=n downto 1 do n disks
Disk(15,19-n+i,i-1);
initiate array Col[3],Flag[3]
Col[1]:=15; Col[2]:=40; Col[3]:=65;
Flag[1]:=n+1; Flag[2]:=1; Flag[3]:=1;
some informations
gotoxy(5,24);
write(\'Press spacebar to begin...\');
repeat until ReadKey<>\'\';
gotoxy(5,24);ClrEol;
gotoxy(5,24);ClrEol;
write(\'Press any key to break.\');
end;
(* move a disk FROM from TO too *)
Procedure Move(m,from,too:integer);
var
x1,y1,x2,y2,n,step:integer;
Begin
x1:=Col[from]; y1:=20-Flag[from]+1;
x2:=Col[too] ; y2:=20-Flag[too];
step:=too-from; n:=m-1;
repeat up
ClrDisk(x1,y1,n);
if y1>=8 then begin gotoxy(x1,y1);write(\'|\'); end;
dec(y1);
Disk(x1,y1,n);Delay(50);
until y1=5;
dec(Flag[from]);
repeat shift
ClrDisk(x1,5,n);
inc(x1,step);
Disk(x1,5,n);Delay(50);
until x1=x2;
repeat down
ClrDisk(x2,y1,n);
if y1>=8 then begin gotoxy(x1,y1);write(\'|\'); end;
inc(y1);
Disk(x2,y1,n);Delay(50);
until y1=y2;
inc(Flag[too]);
Sing;
if KeyPressed then Halt(0);
end;
(* n Hanoi Problem *)
procedure N_Hanoi(n,a,b,c:integer);
Begin
if n=1 then Move(1,a,c)
else begin
N_Hanoi(n-1,a,c,b);
Move(n,a,c);
N_Hanoi(n-1,b,a,c);
end;
end;
(* print end information of this program *)
Procedure Final;
Begin
gotoxy(5,24);ClrEol;
write(\'I have moved all disks !\');
gotoxy(5,25);
write(\'Press spacebar to end.\');
repeat until ReadKey<>\'\';
end;
Begin
Initiate;
N_Hanoi(n,1,2,3);
Final;
End. 参考技术A 你太有才了 一个汉诺塔,你写了这个多代码,我最多5行主要代码就能实现呵呵 这道题你的盘中数量不要太多,不然你会后悔,上次我计算当有60层盘子的时候,假如一个人每秒能移动一次。那么全中国所以人一起24小时不停的移动都要600(好像是6000,我忘了,好久之前弄的)亿年,呵呵
以上是关于关于汉诺塔问题的主要内容,如果未能解决你的问题,请参考以下文章