|
不自由なハノイの塔(再帰版)
●条件
m枚目は真ん中に置けない(n>=m)
移動回数 2^m*3^(n-m)-1
PUBLIC NUMERIC COUNT,INIT,NN
CALL GINIT(800,400)
INPUT PROMPT "枚数=":NN
INPUT PROMPT "真ん中に置けない円盤 No.=":M
FOR I=1 TO NN
CALL DISPLAY(I,2,1)
NEXT I
LET INIT=1
WAIT DELAY .5
CALL HANOI(NN,M,1,2,3)
END
EXTERNAL SUB HANOI(N,M,A,B,C) !' N>=M
IF N<=M THEN
CALL HANOI_SUB(N,A,B,C)
ELSE
CALL HANOI(N-1,M,A,B,C)
CALL DISPLAY(N,A,B)
CALL HANOI(N-1,M,C,B,A)
CALL DISPLAY(N,B,C)
CALL HANOI(N-1,M,A,B,C)
END IF
END SUB
EXTERNAL SUB HANOI_SUB(N,A,B,C)
IF N>0 THEN
CALL HANOI_SUB(N-1,A,C,B)
CALL DISPLAY(N,A,C)
CALL HANOI_SUB(N-1,B,A,C)
END IF
END SUB
EXTERNAL SUB DISPLAY(N,A,B)
IF INIT=1 THEN
LET COUNT=COUNT+1
PLOT TEXT,AT 30,0:"No."&STR$(COUNT)
WAIT DELAY 1/10
END IF
LET HEIGHT=INT(330/NN)
LET WIDTH=INT(180/NN)
CALL BOXFULL(200*A-WIDTH/2*N,50+HEIGHT*(N-1),200*A+WIDTH/2*N,50+HEIGHT*N,0)
CALL BOXFULL(200*B-WIDTH/2*N,50+HEIGHT*(N-1),200*B+WIDTH/2*N,50+HEIGHT*N,7)
END SUB
EXTERNAL SUB GINIT(XSIZE,YSIZE)
SET BITMAP SIZE XSIZE,YSIZE
SET WINDOW 0,XSIZE-1,YSIZE-1,0
SET POINT STYLE 1
SET COLOR MODE "REGULAR"
SET COLOR MIX(0) 0,0,0
SET COLOR MIX(1) 0,0,1
SET COLOR MIX(2) 1,0,0
SET COLOR MIX(3) 1,0,1
SET COLOR MIX(4) 0,1,0
SET COLOR MIX(5) 0,1,1
SET COLOR MIX(6) 1,1,0
SET COLOR MIX(7) 1,1,1
SET TEXT HEIGHT 20
SET TEXT JUSTIFY "LEFT" , "TOP"
SET TEXT BACKGROUND "OPAQUE"
SET TEXT COLOR 7
CLEAR
END SUB
EXTERNAL SUB BOXFULL(X1,Y1,X2,Y2,C)
SET COLOR C
PLOT AREA:X1,Y1;X2,Y1;X2,Y2;X1,Y2;X1,Y1
END SUB
|
|