不自由なハノイの塔

 投稿者:しばっち  投稿日:2020年 6月 3日(水)20時44分45秒
  不自由なハノイの塔

不自由なハノイの塔ではハノイの塔のルールに次の条件を加えます。

●条件
1番上の1枚目の円盤は3本ある棒のうち、真ん中(の棒)には置いてはいけない。


この条件を適用すると1枚目の円盤は奇数回毎に棒Aと棒Cを繰り返し移動します。
すると偶数回で移動できる円盤は1通りしかありません。

移動回数は

   1枚の時、 1回
   2枚の時、 5回
   3枚の時、17回
   4枚の時、53回
                  となり、

よって円盤がn枚の時は
     2*3^(n-1)-1回となります。

円盤が3枚の場合ですが、下記の実行結果を見ても1枚目の円盤は真ん中(の棒)にはありません。
ちなみにNo.1までが円盤1枚の時の No.5までが円盤2枚の時の解答となります。

その他棒の数を4本、5本と増やす流儀もあるようです。

LET N=3
DIM S(N,3),A$(N)
MAT READ A$
PRINT "円盤";N;"枚"
PRINT "No. 0"
FOR K=1 TO N
   LET S(K,1)=1
   PRINT A$(K)
NEXT K
FOR I=1 TO 2*3^(N-1)-1
   IF MOD(I,2)=1 THEN
      LET J=1
      SWAP S(1,1),S(1,3)
   ELSE
      FOR J=2 TO N
         IF S(1,1)=0 AND S(J-1,1)=0 AND S(J,1)=1 AND S(J,2)=0 OR S(1,1)=0 AND S(J-1,2)=0 AND S(J,2)=1 AND S(J,1)=0 THEN
            SWAP S(J,1),S(J,2)
            EXIT FOR
         END IF
         IF S(1,1)=0 AND S(J-1,1)=0 AND S(J,1)=1 AND S(J,3)=0 OR S(1,3)=0 AND S(1,1)=0 AND S(J-1,3)=0 AND S(J,3)=1 AND S(J,1)=0 THEN
            SWAP S(J,1),S(J,3)
            EXIT FOR
         END IF
         IF S(1,3)=0 AND S(J-1,3)=0 AND S(J,3)=1 AND S(J,2)=0 OR S(J,2)=1 AND S(J,3)=0 AND S(J-1,2)=0 THEN
            SWAP S(J,3),S(J,2)
            EXIT FOR
         END IF
      NEXT J
   END IF
   PRINT REPEAT$("-",78)
   PRINT "No.";I
   FOR J=1 TO N
      FOR K=1 TO 3
         IF S(J,K)=1 THEN
            PRINT A$(J)
            EXIT FOR
         END IF
         PRINT REPEAT$(" ",26);
      NEXT K
   NEXT J
NEXT I
DATA "            ■            "
DATA "          ■■■          "
DATA "        ■■■■■        "
DATA "      ■■■■■■■      "
DATA "    ■■■■■■■■■    "
DATA "  ■■■■■■■■■■■  "
END


                実行結果

円盤 3 枚
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
                                                                ■
          ■■■
                                                            ■■■■■
------------------------------------------------------------------------------
No. 14
                                                                ■
                                    ■■■
                                                            ■■■■■
------------------------------------------------------------------------------
No. 15
            ■
                                    ■■■
                                                            ■■■■■
------------------------------------------------------------------------------
No. 16
            ■
                                                              ■■■
                                                            ■■■■■
------------------------------------------------------------------------------
No. 17
                                                                ■
                                                              ■■■
                                                            ■■■■■
 

不自由なハノイの塔

 投稿者:kikiriri  投稿日:2020年 6月 4日(木)06時53分30秒
  しばっち様へ
いいですね。
 

Re: 不自由なハノイの塔

 投稿者:しばっち  投稿日:2020年 6月 7日(日)14時28分53秒
  > No.4835[元記事へ]

不自由なハノイの塔

●条件
2枚目は真ん中(の棒)には置けない (N>=2)


LET N=3 !' N>=2
DIM S(N,3),A$(0 TO N)
MAT READ A$
PRINT "円盤";N;"枚"
PRINT "No. 0"
FOR K=1 TO N
   LET S(K,1)=1
   PRINT A$(K)
NEXT K
FOR I=1 TO 4*3^(N-2)-1
   IF MOD(I,2)=1 THEN
      LET S(1,1)=0
      LET S(1,2)=0
      LET S(1,3)=0
      LET L=MOD(L,4)+1
      SELECT CASE L
      CASE 1
         LET S(1,2)=1
      CASE 2
         LET S(1,3)=1
      CASE 3
         LET S(1,2)=1
      CASE 4
         LET S(1,1)=1
      END SELECT
   ELSEIF MOD(I,4)=2 THEN
      SWAP S(2,1),S(2,3)
   ELSE
      FOR J=3 TO N
         IF S(J-1,1)=0 AND S(J,1)=1 AND S(J-1,2)=0 OR S(J-1,2)=0 AND S(J,2)=1 AND S(J-1,1)=0 THEN
            SWAP S(J,1),S(J,2)
            EXIT FOR
         END IF
         IF S(J-1,1)=0 AND S(J,1)=1 AND S(J-1,3)=0 OR S(J-1,3)=0 AND S(J,3)=1 AND S(J-1,1)=0 THEN
            SWAP S(J,1),S(J,3)
            EXIT FOR
         END IF
         IF S(J-1,3)=0 AND S(J,3)=1 AND S(J-1,2)=0 OR S(J-1,3)=0 AND S(J-1,2)=0 AND S(J,2)=1 THEN
            SWAP S(J,3),S(J,2)
            EXIT FOR
         END IF
      NEXT J
   END IF
   PRINT REPEAT$("-",LEN(A$(0))*3)
   PRINT "No.";I
   FOR J=1 TO N
      FOR K=1 TO 3
         IF S(J,K)=1 THEN
            PRINT A$(J)
            EXIT FOR
         END IF
         PRINT A$(0);
      NEXT K
   NEXT J
NEXT I
DATA "                              "
DATA "              ■              "
DATA "            ■■■            "
DATA "          ■■■■■          "
DATA "        ■■■■■■■        "
DATA "      ■■■■■■■■■      "
DATA "    ■■■■■■■■■■■    "
DATA "  ■■■■■■■■■■■■■  "
END

              実行結果

円盤 3 枚
No. 0
              ■
            ■■■
          ■■■■■
------------------------------------------------------------------------------------------
No. 1
                                            ■
            ■■■
          ■■■■■
------------------------------------------------------------------------------------------
No. 2
                                            ■
                                                                        ■■■
          ■■■■■
------------------------------------------------------------------------------------------
No. 3
                                                                          ■
                                                                        ■■■
          ■■■■■
------------------------------------------------------------------------------------------
No. 4
                                                                          ■
                                                                        ■■■
                                        ■■■■■
------------------------------------------------------------------------------------------
No. 5
                                            ■
                                                                        ■■■
                                        ■■■■■
------------------------------------------------------------------------------------------
No. 6
                                            ■
            ■■■
                                        ■■■■■
------------------------------------------------------------------------------------------
No. 7
              ■
            ■■■
                                        ■■■■■
------------------------------------------------------------------------------------------
No. 8
              ■
            ■■■
                                                                      ■■■■■
------------------------------------------------------------------------------------------
No. 9
                                            ■
            ■■■
                                                                      ■■■■■
------------------------------------------------------------------------------------------
No. 10
                                            ■
                                                                        ■■■
                                                                      ■■■■■
------------------------------------------------------------------------------------------
No. 11
                                                                          ■
                                                                        ■■■
                                                                      ■■■■■
 

Re: 不自由なハノイの塔

 投稿者:しばっち  投稿日:2020年 6月 7日(日)14時29分41秒
  > No.4835[元記事へ]

不自由なハノイの塔

●条件
3枚目は真ん中(の棒)には置けない (N>=3)


LET N=4 !'N>=3
DIM S(N,3),A$(0 TO N)
MAT READ A$
PRINT "円盤";N;"枚"
PRINT "No. 0"
FOR K=1 TO N
   LET S(K,1)=1
   PRINT A$(K)
NEXT K
FOR I=1 TO 8*3^(N-3)-1
   IF MOD(I,2)=1 THEN
      IF MOD(I,16)<=8 THEN
         LET L2=0
         LET S(1,1)=0
         LET S(1,2)=0
         LET S(1,3)=0
         LET L1=MOD(L1,3)+1
         SELECT CASE L1
         CASE 1
            LET S(1,3)=1
         CASE 2
            LET S(1,2)=1
         CASE 3
            LET S(1,1)=1
         END SELECT
      ELSE
         LET L1=0
         LET S(1,1)=0
         LET S(1,2)=0
         LET S(1,3)=0
         LET L2=MOD(L2,3)+1
         SELECT CASE L2
         CASE 1
            LET S(1,1)=1
         CASE 2
            LET S(1,2)=1
         CASE 3
            LET S(1,3)=1
         END SELECT
      END IF
   ELSEIF MOD(I,8)=4 THEN
      SWAP S(3,1),S(3,3)
   ELSEIF MOD(I,4)=2 THEN
      LET S(2,1)=0
      LET S(2,2)=0
      LET S(2,3)=0
      LET L3=MOD(L3,4)+1
      SELECT CASE L3
      CASE 1
         LET S(2,2)=1
      CASE 2
         LET S(2,3)=1
      CASE 3
         LET S(2,2)=1
      CASE 4
         LET S(2,1)=1
      END SELECT
   ELSE
      FOR J=4 TO N
         IF S(J-1,1)=0 AND S(J,1)=1 AND S(J-1,2)=0 OR S(J-1,2)=0 AND S(J,2)=1 AND S(J-1,1)=0 THEN
            SWAP S(J,1),S(J,2)
            EXIT FOR
         END IF
         IF S(J-1,1)=0 AND S(J,1)=1 AND S(J-1,3)=0 OR S(J-1,3)=0 AND S(J,3)=1 AND S(J-1,1)=0 THEN
            SWAP S(J,1),S(J,3)
            EXIT FOR
         END IF
         IF S(J-1,3)=0 AND S(J,3)=1 AND S(J-1,2)=0 OR S(J-1,3)=0 AND S(J-1,2)=0 AND S(J,2)=1 THEN
            SWAP S(J,3),S(J,2)
            EXIT FOR
         END IF
      NEXT J
   END IF
   PRINT REPEAT$("-",LEN(A$(0))*3)
   PRINT "No.";I
   FOR J=1 TO N
      FOR K=1 TO 3
         IF S(J,K)=1 THEN
            PRINT A$(J)
            EXIT FOR
         END IF
         PRINT A$(0);
      NEXT K
   NEXT J
NEXT I
DATA "                              "
DATA "              ■              "
DATA "            ■■■            "
DATA "          ■■■■■          "
DATA "        ■■■■■■■        "
DATA "      ■■■■■■■■■      "
DATA "    ■■■■■■■■■■■    "
DATA "  ■■■■■■■■■■■■■  "
END

              実行結果

円盤 4 枚
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
                                                                          ■
                                          ■■■
          ■■■■■
                                      ■■■■■■■
------------------------------------------------------------------------------------------
No. 14
                                                                          ■
            ■■■
          ■■■■■
                                      ■■■■■■■
------------------------------------------------------------------------------------------
No. 15
              ■
            ■■■
          ■■■■■
                                      ■■■■■■■
------------------------------------------------------------------------------------------
No. 16
              ■
            ■■■
          ■■■■■
                                                                    ■■■■■■■
------------------------------------------------------------------------------------------
No. 17
                                                                          ■
            ■■■
          ■■■■■
                                                                    ■■■■■■■
------------------------------------------------------------------------------------------
No. 18
                                                                          ■
                                          ■■■
          ■■■■■
                                                                    ■■■■■■■
------------------------------------------------------------------------------------------
No. 19
                                            ■
                                          ■■■
          ■■■■■
                                                                    ■■■■■■■
------------------------------------------------------------------------------------------
No. 20
                                            ■
                                          ■■■
                                                                      ■■■■■
                                                                    ■■■■■■■
------------------------------------------------------------------------------------------
No. 21
              ■
                                          ■■■
                                                                      ■■■■■
                                                                    ■■■■■■■
------------------------------------------------------------------------------------------
No. 22
              ■
                                                                        ■■■
                                                                      ■■■■■
                                                                    ■■■■■■■
------------------------------------------------------------------------------------------
No. 23
                                                                          ■
                                                                        ■■■
                                                                      ■■■■■
                                                                    ■■■■■■■
 

戻る