トップページに戻る    次のC#のサンプルへ    前のC#のサンプルへ

Cマガ電脳クラブ(第073回) 詰められ升?

問題

4×4×4の立方体が入るふた付きの箱(Fig.1)があり、
これに2×2×1のピース(Fig.2)を詰めることを考える。

このピース16個をきっちり詰めてふたをすれば、
箱のどの面を上にしても内部のピースは移動しない
(工作上の誤差による隙間は考えない)。

では、15個をうまく詰めて、箱のどの面を上にしてもピースがなかで移動しないようにすることは可能だろうか。
さらに14個,13個・・・のときはどうだろうか。

このように、どのピースも移動しないように詰めることが可能なピースの数を
全て答えて下さい。

Fig1. 4×4×4の箱(ふたは省略)


Fig2. 2×2×1のピース


ソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    //配置候補のList
    static List<bool[, ,]> HaitiKouhoList;

    static System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();

    static void Main()
    {
        HaitiKouhoList = DeriveHaitiKouhoList();

        for (int Depth = 16; Depth >= 1; Depth--) {
            Console.WriteLine("ピースの数={0}の解を探索中", Depth);
            if (ExecDES(Depth) == false) {
                Console.WriteLine("ピースの数={0}の解は、ありません。", Depth);
                break;
            }
        }
        Console.WriteLine("経過時間={0}", sw.Elapsed);
    }

    static int UB = 4 - 1;

    struct JyoutaiDef
    {
        internal char[, ,] BanArr;
        internal int CurrX;
        internal int CurrY;
        internal int CurrZ;
        internal int PieceCnt;
        internal int SpaceCnt;
    }

    //深さを指定してDFSを行い、解の有無を返す
    static bool ExecDES(int pDepth)
    {
        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.BanArr = new char[UB + 1, UB + 1, UB + 1];
        for (int X = 0; X <= UB; X++)
            for (int Y = 0; Y <= UB; Y++)
                for (int Z = 0; Z <= UB; Z++)
                    WillPush.BanArr[X, Y, Z] = ' ';

        WillPush.CurrX = WillPush.CurrY = WillPush.CurrZ = 0;
        WillPush.PieceCnt = 0;
        WillPush.SpaceCnt = 0;
        stk.Push(WillPush);

        while (stk.Count > 0) {
            JyoutaiDef Popped = stk.Pop();

            //X座標の繰上げ処理
            if (Popped.CurrX > UB) {
                Popped.CurrX = 0;
                Popped.CurrY++;
            }

            //最終行を超えた場合
            if (Popped.CurrY > UB) {
                //スライド可能なピースがあれば枝切り
                if (ExistCanSlidePiece(Popped.BanArr, Popped.PieceCnt)) continue;

                Popped.CurrY = 0;

                //最終高さを超えた場合
                if (++Popped.CurrZ > UB) {
                    Console.WriteLine("解を発見。経過時間={0}", sw.Elapsed);
                    PrintAnswer(Popped.BanArr);
                    return true;
                }
            }

            for (int I = 0; I <= HaitiKouhoList.Count - 1; I++) {
                //ピース上限ならピースをセットできない
                if (Popped.PieceCnt == pDepth) break;

                //1個目のピースは回転させない
                if (Popped.PieceCnt == 0 && 0 < I) break;

                //マス目にピースを埋めれない場合
                if (CanFillPiece(HaitiKouhoList[I], Popped.CurrX, Popped.CurrY, Popped.CurrZ,
                    Popped.BanArr) == false) continue;

                //ピースを配置する経路のPush処理
                WillPush.BanArr = (char[, ,])Popped.BanArr.Clone();
                WillPush.CurrX = Popped.CurrX + 1;
                WillPush.CurrY = Popped.CurrY;
                WillPush.CurrZ = Popped.CurrZ;
                WillPush.PieceCnt = Popped.PieceCnt + 1;
                WillPush.SpaceCnt = Popped.SpaceCnt;

                bool[, ,] PieceMap = HaitiKouhoList[I];

                for (int X = 0; X <= PieceMap.GetUpperBound(0); X++) {
                    for (int Y = 0; Y <= PieceMap.GetUpperBound(1); Y++) {
                        for (int Z = 0; Z <= PieceMap.GetUpperBound(2); Z++) {
                            if (PieceMap[X, Y, Z] == false) continue;
                            WillPush.BanArr[Popped.CurrX + X, Popped.CurrY + Y, Popped.CurrZ + Z]
                                = (Char)('A' + WillPush.PieceCnt - 1);
                        }
                    }
                }
                stk.Push(WillPush);
            }

            //ピースを配置しない経路のPush
            WillPush.CurrX = Popped.CurrX + 1;
            WillPush.CurrY = Popped.CurrY;
            WillPush.CurrZ = Popped.CurrZ;

            //ピースを配置済の場合
            if (Popped.BanArr[Popped.CurrX, Popped.CurrY, Popped.CurrZ] != ' ') {
                WillPush.BanArr = Popped.BanArr;
                WillPush.SpaceCnt = Popped.SpaceCnt;
            }
            else { //確定空白の場合
                //確定空白の上限を超えたら枝切り
                WillPush.SpaceCnt = Popped.SpaceCnt + 1;
                if (64 < WillPush.SpaceCnt + 4 * pDepth) continue;

                WillPush.BanArr = (char[, ,])Popped.BanArr.Clone();
                WillPush.BanArr[Popped.CurrX, Popped.CurrY, Popped.CurrZ] = '/';
            }
            WillPush.PieceCnt = Popped.PieceCnt;
            stk.Push(WillPush);
        }
        return false;
    }

    //スライド可能なピースの有無を返す
    static bool ExistCanSlidePiece(char[, ,] pBanArr, int pPieceCnt)
    {
        //ピースのループ
        for (char LoopChar = 'A'; LoopChar <= 'A' + pPieceCnt - 1; LoopChar++) {
            //各軸の移動可否
            bool CanMoveXMinus = true;
            bool CanMoveXPlus = true;
            bool CanMoveYMinus = true;
            bool CanMoveYPlus = true;
            bool CanMoveZMinus = true;
            bool CanMoveZPlus = true;

            for (int X = 0; X <= UB; X++) {
                for (int Y = 0; Y <= UB; Y++) {
                    for (int Z = 0; Z <= UB; Z++) {
                        if (pBanArr[X, Y, Z] != LoopChar) continue;

                        Predicate<char> wkPred = (pChar) =>
                            (pChar != LoopChar && pChar != '/');

                        if (X == 0 || wkPred(pBanArr[X - 1, Y, Z])) CanMoveXMinus = false;
                        if (X == UB || wkPred(pBanArr[X + 1, Y, Z])) CanMoveXPlus = false;
                        if (Y == 0 || wkPred(pBanArr[X, Y - 1, Z])) CanMoveYMinus = false;
                        if (Y == UB || wkPred(pBanArr[X, Y + 1, Z])) CanMoveYPlus = false;
                        if (Z == 0 || wkPred(pBanArr[X, Y, Z - 1])) CanMoveZMinus = false;
                        if (Z == UB || wkPred(pBanArr[X, Y, Z + 1])) CanMoveZPlus = false;
                    }
                }
            }

            if (CanMoveXMinus || CanMoveXPlus) return true;
            if (CanMoveYMinus || CanMoveYPlus) return true;
            if (CanMoveZMinus || CanMoveZPlus) return true;
        }
        return false;
    }

    //回転させた配置のListを返す
    static List<bool[, ,]> DeriveHaitiKouhoList()
    {
        bool[, ,] wkArr = null;

        wkArr = new bool[2, 2, 1];
        wkArr[0, 0, 0] = wkArr[1, 0, 0] = true;
        wkArr[0, 1, 0] = wkArr[1, 1, 0] = true;

        return DeriveKaitenArrList(wkArr);
    }

    //配列を引数として、回転させた配列のリストをDistinctして返す
    static List<bool[, ,]> DeriveKaitenArrList(bool[, ,] pBaseArr)
    {
        var KaitenArrList = new List<bool[, ,]>();

        int BaseUB_X = pBaseArr.GetUpperBound(0);
        int BaseUB_Y = pBaseArr.GetUpperBound(1);
        int BaseUB_Z = pBaseArr.GetUpperBound(2);

        for (int I = 1; I <= 48; I++) KaitenArrList.Add(null);
        for (int P = 0; P <= 7; P++) KaitenArrList[P] = new bool[BaseUB_X + 1, BaseUB_Y + 1, BaseUB_Z + 1];
        for (int P = 8; P <= 15; P++) KaitenArrList[P] = new bool[BaseUB_X + 1, BaseUB_Z + 1, BaseUB_Y + 1];
        for (int P = 16; P <= 23; P++) KaitenArrList[P] = new bool[BaseUB_Y + 1, BaseUB_X + 1, BaseUB_Z + 1];
        for (int P = 24; P <= 31; P++) KaitenArrList[P] = new bool[BaseUB_Y + 1, BaseUB_Z + 1, BaseUB_X + 1];
        for (int P = 32; P <= 39; P++) KaitenArrList[P] = new bool[BaseUB_Z + 1, BaseUB_X + 1, BaseUB_Y + 1];
        for (int P = 40; P <= 47; P++) KaitenArrList[P] = new bool[BaseUB_Z + 1, BaseUB_Y + 1, BaseUB_X + 1];

        for (int X = 0; X <= BaseUB_X; X++) {
            for (int Y = 0; Y <= BaseUB_Y; Y++) {
                for (int Z = 0; Z <= BaseUB_Z; Z++) {
                    bool SetVal = pBaseArr[X, Y, Z];
                    KaitenArrList[0][X, Y, Z] = SetVal;
                    KaitenArrList[1][X, Y, BaseUB_Z - Z] = SetVal;
                    KaitenArrList[2][X, BaseUB_Y - Y, Z] = SetVal;
                    KaitenArrList[3][X, BaseUB_Y - Y, BaseUB_Z - Z] = SetVal;
                    KaitenArrList[4][BaseUB_X - X, Y, Z] = SetVal;
                    KaitenArrList[5][BaseUB_X - X, Y, BaseUB_Z - Z] = SetVal;
                    KaitenArrList[6][BaseUB_X - X, BaseUB_Y - Y, Z] = SetVal;
                    KaitenArrList[7][BaseUB_X - X, BaseUB_Y - Y, BaseUB_Z - Z] = SetVal;

                    KaitenArrList[8][X, Z, Y] = SetVal;
                    KaitenArrList[9][X, Z, BaseUB_Y - Y] = SetVal;
                    KaitenArrList[10][X, BaseUB_Z - Z, Y] = SetVal;
                    KaitenArrList[11][X, BaseUB_Z - Z, BaseUB_Y - Y] = SetVal;
                    KaitenArrList[12][BaseUB_X - X, Z, Y] = SetVal;
                    KaitenArrList[13][BaseUB_X - X, Z, BaseUB_Y - Y] = SetVal;
                    KaitenArrList[14][BaseUB_X - X, BaseUB_Z - Z, Y] = SetVal;
                    KaitenArrList[15][BaseUB_X - X, BaseUB_Z - Z, BaseUB_Y - Y] = SetVal;

                    KaitenArrList[16][Y, X, Z] = SetVal;
                    KaitenArrList[17][Y, X, BaseUB_Z - Z] = SetVal;
                    KaitenArrList[18][Y, BaseUB_X - X, Z] = SetVal;
                    KaitenArrList[19][Y, BaseUB_X - X, BaseUB_Z - Z] = SetVal;
                    KaitenArrList[20][BaseUB_Y - Y, X, Z] = SetVal;
                    KaitenArrList[21][BaseUB_Y - Y, X, BaseUB_Z - Z] = SetVal;
                    KaitenArrList[22][BaseUB_Y - Y, BaseUB_X - X, Z] = SetVal;
                    KaitenArrList[23][BaseUB_Y - Y, BaseUB_X - X, BaseUB_Z - Z] = SetVal;

                    KaitenArrList[24][Y, Z, X] = SetVal;
                    KaitenArrList[25][Y, Z, BaseUB_X - X] = SetVal;
                    KaitenArrList[26][Y, BaseUB_Z - Z, X] = SetVal;
                    KaitenArrList[27][Y, BaseUB_Z - Z, BaseUB_X - X] = SetVal;
                    KaitenArrList[28][BaseUB_Y - Y, Z, X] = SetVal;
                    KaitenArrList[29][BaseUB_Y - Y, Z, BaseUB_X - X] = SetVal;
                    KaitenArrList[30][BaseUB_Y - Y, BaseUB_Z - Z, X] = SetVal;
                    KaitenArrList[31][BaseUB_Y - Y, BaseUB_Z - Z, BaseUB_X - X] = SetVal;

                    KaitenArrList[32][Z, X, Y] = SetVal;
                    KaitenArrList[33][Z, X, BaseUB_Y - Y] = SetVal;
                    KaitenArrList[34][Z, BaseUB_X - X, Y] = SetVal;
                    KaitenArrList[35][Z, BaseUB_X - X, BaseUB_Y - Y] = SetVal;
                    KaitenArrList[36][BaseUB_Z - Z, X, Y] = SetVal;
                    KaitenArrList[37][BaseUB_Z - Z, X, BaseUB_Y - Y] = SetVal;
                    KaitenArrList[38][BaseUB_Z - Z, BaseUB_X - X, Y] = SetVal;
                    KaitenArrList[39][BaseUB_Z - Z, BaseUB_X - X, BaseUB_Y - Y] = SetVal;

                    KaitenArrList[40][Z, Y, X] = SetVal;
                    KaitenArrList[41][Z, Y, BaseUB_X - X] = SetVal;
                    KaitenArrList[42][Z, BaseUB_Y - Y, X] = SetVal;
                    KaitenArrList[43][Z, BaseUB_Y - Y, BaseUB_X - X] = SetVal;
                    KaitenArrList[44][BaseUB_Z - Z, Y, X] = SetVal;
                    KaitenArrList[45][BaseUB_Z - Z, Y, BaseUB_X - X] = SetVal;
                    KaitenArrList[46][BaseUB_Z - Z, BaseUB_Y - Y, X] = SetVal;
                    KaitenArrList[47][BaseUB_Z - Z, BaseUB_Y - Y, BaseUB_X - X] = SetVal;
                }
            }
        }

        //Distinctする
        for (int I = KaitenArrList.Count - 1; 0 <= I; I--) {
            for (int J = 0; J <= I - 1; J++) {
                //UBが違う場合は、同一でない
                if (KaitenArrList[I].GetUpperBound(0) != KaitenArrList[J].GetUpperBound(0)) continue;
                if (KaitenArrList[I].GetUpperBound(1) != KaitenArrList[J].GetUpperBound(1)) continue;
                if (KaitenArrList[I].GetUpperBound(2) != KaitenArrList[J].GetUpperBound(2)) continue;

                IEnumerable<bool> wkEnum1 = KaitenArrList[I].Cast<bool>();
                IEnumerable<bool> wkEnum2 = KaitenArrList[J].Cast<bool>();
                if (wkEnum1.SequenceEqual(wkEnum2) == false) continue;

                KaitenArrList.RemoveAt(I);
                break;
            }
        }
        return KaitenArrList;
    }

    //マス目にピースを埋めれるか
    static bool CanFillPiece(bool[, ,] pPieceMap,
        int pTargetX, int pTargetY, int pTargetZ, char[, ,] pBanArr)
    {
        for (int X = 0; X <= pPieceMap.GetUpperBound(0); X++) {
            if (pTargetX + X > UB) return false;
            for (int Y = 0; Y <= pPieceMap.GetUpperBound(1); Y++) {
                if (pTargetY + Y > UB) return false;
                for (int Z = 0; Z <= pPieceMap.GetUpperBound(2); Z++) {
                    if (pTargetZ + Z > UB) return false;
                    if (pPieceMap[X, Y, Z]
                     && pBanArr[pTargetX + X, pTargetY + Y, pTargetZ + Z] != ' ')
                        return false;
                }
            }
        }
        return true;
    }

    //解を出力
    static void PrintAnswer(char[, ,] pBanArr)
    {
        var sb = new System.Text.StringBuilder();

        for (int Z = 0; Z <= UB; Z++) {
            sb.AppendFormat("Z={0}の平面", Z);
            sb.AppendLine();
            for (int Y = 0; Y <= UB; Y++) {
                for (int X = 0; X <= UB; X++) {
                    sb.Append(pBanArr[X, Y, Z]);
                }
                sb.AppendLine();
            }
        }
        Console.WriteLine(sb.ToString());
    }
}


実行結果

省略
ピースの数=12の解を探索中
解を発見。経過時間=00:00:03.1342757
Z=0の平面
/AA/
BAAC
BDDC
/DD/
Z=1の平面
EEFF
B//C
B//C
GGHH
Z=2の平面
EEFF
I//J
I//J
GGHH
Z=3の平面
/KK/
IKKJ
ILLJ
/LL/

ピースの数=11の解を探索中
ピースの数=11の解は、ありません。
経過時間=00:00:36.7100692


解説

参考までに回転解と対称解を除くと
12ピースなら   3通り
13ピースなら   1通り
14ピースなら  72通り
15ピースなら  21通り
16ピースなら1073通り
だそうです。

正解例