4本ハノイの塔

 投稿者:しばっち  投稿日:2020年 7月 1日(水)20時17分3秒
  4本ハノイの塔

PUBLIC NUMERIC COUNT,S(8,4),NN
PUBLIC STRING D$(0 TO 8)
MAT READ D$
INPUT NN
PRINT "No. 0"
FOR K=1 TO NN
   LET S(K,1)=1
   PRINT D$(K)
NEXT K
CALL HANOI(NN,1,2,3,4)
DATA "                                  "
DATA "                ■                "
DATA "              ■■■              "
DATA "            ■■■■■            "
DATA "          ■■■■■■■          "
DATA "        ■■■■■■■■■        "
DATA "      ■■■■■■■■■■■      "
DATA "    ■■■■■■■■■■■■■    "
DATA "  ■■■■■■■■■■■■■■■  "
END

EXTERNAL  SUB HANOI(N,A,B,C,D)
IF N=1 THEN
   CALL DISPLAY(1,A,D)
ELSEIF N=2 THEN
   CALL DISPLAY(1,A,B)
   CALL DISPLAY(2,A,D)
   CALL DISPLAY(1,B,D)
ELSE
   CALL HANOI(N-2,A,C,D,B)
   CALL DISPLAY(N-1,A,C)
   CALL DISPLAY(N,A,D)
   CALL DISPLAY(N-1,C,D)
   CALL HANOI(N-2,B,A,C,D)
END IF
END SUB

EXTERNAL  SUB DISPLAY(N,A,B)
LET S(N,A)=0
LET S(N,B)=1
PRINT REPEAT$("-",LEN(D$(0))*4)
LET COUNT=COUNT+1
PRINT "No.";COUNT
FOR J=1 TO NN
   FOR K=1 TO 4
      IF S(J,K)=1 THEN
         PRINT D$(J)
         EXIT FOR
      END IF
      PRINT D$(0);
   NEXT K
NEXT J
END SUB

                   実行結果

? 5
No. 0
                ■
              ■■■
            ■■■■■
          ■■■■■■■
        ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 1
                                                                                    ■
              ■■■
            ■■■■■
          ■■■■■■■
        ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 2
                                                                                    ■
                                                                                                                    ■■■
            ■■■■■
          ■■■■■■■
        ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 3
                                                                                    ■
                                                                                                                    ■■■
                                              ■■■■■
          ■■■■■■■
        ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 4
                                                                                    ■
                                                ■■■
                                              ■■■■■
          ■■■■■■■
        ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 5
                                                  ■
                                                ■■■
                                              ■■■■■
          ■■■■■■■
        ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 6
                                                  ■
                                                ■■■
                                              ■■■■■
                                                                              ■■■■■■■
        ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 7
                                                  ■
                                                ■■■
                                              ■■■■■
                                                                              ■■■■■■■
                                                                                                              ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 8
                                                  ■
                                                ■■■
                                              ■■■■■
                                                                                                                ■■■■■■■
                                                                                                              ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 9
                ■
                                                ■■■
                                              ■■■■■
                                                                                                                ■■■■■■■
                                                                                                              ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 10
                ■
                                                                                  ■■■
                                              ■■■■■
                                                                                                                ■■■■■■■
                                                                                                              ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 11
                ■
                                                                                  ■■■
                                                                                                                  ■■■■■
                                                                                                                ■■■■■■■
                                                                                                              ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 12
                ■
                                                                                                                    ■■■
                                                                                                                  ■■■■■
                                                                                                                ■■■■■■■
                                                                                                              ■■■■■■■■■
----------------------------------------------------------------------------------------------------------------------------------------
No. 13
                                                                                                                      ■
                                                                                                                    ■■■
                                                                                                                  ■■■■■
                                                                                                                ■■■■■■■
                                                                                                              ■■■■■■■■■
 

戻る