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

20-017 アソートキューブ(立体)

問題

小田原充宏さんのアソートキューブの立体問題を解きます。



15個のピースは下記となります。
各ピースは回転や裏返しても使えます。


Q1 4×4×4を作る。


Q2 VFWlntのピースを使って、3×3×3を作る。解数は1


Q3 UPZWVdのピースを使って、3×3×3を作る。解数は1


Q4 XZTVtlnoのピースを使って、3×3×4を作る。解数は1


Q5 Zbdを除いたピースで、3×3×3を2つ作る。解数は1


Q6 2×7と2×5×5を作る。


ソース

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

class Program
{
    //ピースごとの配置候補
    static Dictionary<char, List<bool[, ,]>> HaitiKouhoListDict =
        new Dictionary<char, List<bool[, ,]>>();

    static char[] PieceNameArr = "FPTUVWXZlnotabd".ToCharArray();

    static int mQuestionNo = 1;
    static List<char[, ,]> mQuestionArrList = new List<char[, ,]>(); //問題の初期盤面
    static HashSet<char> mCanUsePieceSet = new HashSet<char>(); //使用可能なピースのSet

    static int UB_X;
    static int UB_Y;
    static int UB_Z;

    struct JyoutaiDef
    {
        internal List<char[, ,]> BanArrList;
        internal int CurrBanInd;
        internal int CurrX;
        internal int CurrY;
        internal int CurrZ;
        internal HashSet<char> UsedPieceSet;
    }

    //問題を定義
    static void QuestionDef()
    {
        Action<int, int, int> AddAct = (pX, pY, pZ) =>
        {
            char[, ,] wkArr = new char[pX + 1, pY + 1, pZ + 1];
            for (int X = 0; X <= pX; X++)
                for (int Y = 0; Y <= pY; Y++)
                    for (int Z = 0; Z <= pZ; Z++)
                        wkArr[X, Y, Z] = ' ';
            mQuestionArrList.Add(wkArr);
        };

        if (mQuestionNo == 1) {
            UB_X = UB_Y = UB_Z = 4 - 1;
            mCanUsePieceSet = new HashSet<char>(PieceNameArr);
            AddAct(UB_X, UB_Y, UB_Z);
        }
        if (mQuestionNo == 2) {
            UB_X = UB_Y = UB_Z = 3 - 1;
            mCanUsePieceSet = new HashSet<char>("VFWlnt");
            AddAct(UB_X, UB_Y, UB_Z);
        }
        if (mQuestionNo == 3) {
            UB_X = UB_Y = UB_Z = 3 - 1;
            mCanUsePieceSet = new HashSet<char>("UPZWVd");
            AddAct(UB_X, UB_Y, UB_Z);
        }
        if (mQuestionNo == 4) {
            UB_X = 4 - 1; UB_Y = 3 - 1; UB_Z = 3 - 1;
            mCanUsePieceSet = new HashSet<char>("XZTVtlno");
            AddAct(UB_X, UB_Y, UB_Z);
        }
        if (mQuestionNo == 5) {
            UB_X = UB_Y = UB_Z = 3 - 1;
            mCanUsePieceSet = new HashSet<char>(PieceNameArr.Except("Zbd"));
            AddAct(UB_X, UB_Y, UB_Z);
            AddAct(UB_X, UB_Y, UB_Z);
        }
        if (mQuestionNo == 6) {
            UB_X = 2 - 1; UB_Y = 7 - 1; UB_Z = 1 - 1;
            mCanUsePieceSet = new HashSet<char>(PieceNameArr);
            AddAct(UB_X, UB_Y, UB_Z);
            AddAct(5 - 1, 5 - 1, 2 - 1);
        }
    }

    //1個目の立体のピースセット
    static HashSet<string> m1KomePieceSet = new HashSet<string>();

    static void Main()
    {
        var sw = System.Diagnostics.Stopwatch.StartNew();

        //問題を定義
        QuestionDef();

        foreach (char EachPiece in PieceNameArr) {
            HaitiKouhoListDict[EachPiece] = DeriveHaitiKouhoList(EachPiece);
        }

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.BanArrList = mQuestionArrList;
        WillPush.CurrBanInd = 0;
        WillPush.CurrX = WillPush.CurrY = WillPush.CurrZ = 0;
        WillPush.UsedPieceSet = new HashSet<char>();
        Stk.Push(WillPush);

        int PopCnt = 0;
        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();
            if (++PopCnt % 1000000 == 0) {
                Console.WriteLine("{0}回目のPopを行いました。", PopCnt);
            }

            //クリア判定
            if (Popped.BanArrList[Popped.CurrBanInd].Cast<char>().All(X => X != ' ')) {
                if (++Popped.CurrBanInd > mQuestionArrList.Count - 1) {
                    Console.WriteLine("解を発見。経過時間={0}", sw.Elapsed);
                    PrintBan(Popped.BanArrList);
                    return;
                }
                Popped.CurrX = Popped.CurrY = Popped.CurrZ = 0;

                //特殊枝切り 1個目の立体のピースを入れ替えた解の除外
                var sb = new System.Text.StringBuilder();
                foreach (char EachChar in Popped.UsedPieceSet.OrderBy(A => A)) {
                    sb.Append(EachChar);
                }
                if (m1KomePieceSet.Add(sb.ToString()) == false) continue;

                if (mQuestionNo == 6) {
                    UB_X = UB_Y = 5 - 1;
                    UB_Z = 2 - 1;
                }
            }

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

                //特殊枝切り 対称解の除外
                if (Popped.CurrY == 1) {
                    if (Popped.BanArrList[Popped.CurrBanInd][0, 0, 0] >
                       Popped.BanArrList[Popped.CurrBanInd][UB_X, 0, 0])
                        continue;
                }
            }

            //最終行を超えた場合
            if (Popped.CurrY > UB_Y) {
                Popped.CurrY = 0;

                //最終高さを超えた場合
                if (++Popped.CurrZ > UB_Z)
                    continue;
            }

            foreach (char EachPiece in PieceNameArr) {
                if (mCanUsePieceSet.Contains(EachPiece) == false) continue;
                if (Popped.UsedPieceSet.Contains(EachPiece)) continue;

                //特殊枝切り
                if (mQuestionNo == 5) {
                    if (Popped.CurrBanInd == 1 &&
                        Popped.CurrX == 0 && Popped.CurrY == 0 && Popped.CurrZ == 0) {
                        if (Popped.BanArrList[0][0, 0, 0] < EachPiece) continue;
                    }
                }

                //ピースの配置候補リスト
                List<bool[, ,]> HaitiKouhoList = new List<bool[, ,]>();
                HaitiKouhoList.AddRange(HaitiKouhoListDict[EachPiece]);

                //現在のマス目が空白の場合は、マス目を埋める必要あり
                if (Popped.BanArrList[Popped.CurrBanInd][Popped.CurrX, Popped.CurrY, Popped.CurrZ] == ' ')
                    HaitiKouhoList.RemoveAll(A => A[0, 0, 0] == false);

                //マス目にピースを埋めれない候補をRemove
                HaitiKouhoList.RemoveAll(X =>
                    CanFillPiece(X, Popped.CurrX, Popped.CurrY, Popped.CurrZ,
                                 Popped.BanArrList[Popped.CurrBanInd]) == false);

                WillPush.UsedPieceSet = new HashSet<char>(Popped.UsedPieceSet);
                WillPush.UsedPieceSet.Add(EachPiece);

                //ピースを配置する経路のPush処理
                foreach (bool[, ,] EachPieceMap in HaitiKouhoList) {
                    WillPush.CurrBanInd = Popped.CurrBanInd;
                    WillPush.BanArrList = new List<char[, ,]>(Popped.BanArrList);
                    WillPush.BanArrList[WillPush.CurrBanInd] =
                        (char[, ,])Popped.BanArrList[Popped.CurrBanInd].Clone();
                    WillPush.CurrX = Popped.CurrX;
                    WillPush.CurrY = Popped.CurrY;
                    WillPush.CurrZ = Popped.CurrZ;

                    for (int X = 0; X <= EachPieceMap.GetUpperBound(0); X++) {
                        for (int Y = 0; Y <= EachPieceMap.GetUpperBound(1); Y++) {
                            for (int Z = 0; Z <= EachPieceMap.GetUpperBound(2); Z++) {
                                if (EachPieceMap[X, Y, Z] == false) continue;
                                WillPush.BanArrList[WillPush.CurrBanInd]
                                    [Popped.CurrX + X, Popped.CurrY + Y, Popped.CurrZ + Z]
                                    = EachPiece;
                            }
                        }
                    }
                    Stk.Push(WillPush);
                }
            }

            //現在のマス目が空白でない場合は、ピースを配置しない経路のPush
            if (Popped.BanArrList[Popped.CurrBanInd][Popped.CurrX, Popped.CurrY, Popped.CurrZ] != ' ') {
                WillPush.CurrBanInd = Popped.CurrBanInd;
                WillPush.BanArrList = Popped.BanArrList;
                WillPush.CurrX = Popped.CurrX + 1;
                WillPush.CurrY = Popped.CurrY;
                WillPush.CurrZ = Popped.CurrZ;
                WillPush.UsedPieceSet = Popped.UsedPieceSet;
                Stk.Push(WillPush);
            }
        }
    }

    //ピース名を引数として、回転させた配置のListを返す
    static List<bool[, ,]> DeriveHaitiKouhoList(char pPieceName)
    {
        bool[, ,] wkArr = null;

        // ■■
        //■■
        // ■
        if (pPieceName == 'F') {
            wkArr = new bool[3, 3, 1];
            wkArr[1, 0, 0] = wkArr[2, 0, 0] = true;
            wkArr[0, 1, 0] = wkArr[1, 1, 0] = true;
            wkArr[1, 2, 0] = true;
        }

        //■■
        //■■
        //■
        if (pPieceName == 'P') {
            wkArr = new bool[2, 3, 1];
            wkArr[0, 0, 0] = wkArr[1, 0, 0] = true;
            wkArr[0, 1, 0] = wkArr[1, 1, 0] = true;
            wkArr[0, 2, 0] = true;
        }

        //■■■
        // ■
        // ■
        if (pPieceName == 'T') {
            wkArr = new bool[3, 3, 1];
            wkArr[0, 0, 0] = wkArr[1, 0, 0] = wkArr[2, 0, 0] = true;
            wkArr[1, 1, 0] = true;
            wkArr[1, 2, 0] = true;
        }

        //■ ■
        //■■■
        if (pPieceName == 'U') {
            wkArr = new bool[3, 2, 1];
            wkArr[0, 0, 0] = wkArr[2, 0, 0] = true;
            wkArr[0, 1, 0] = wkArr[1, 1, 0] = wkArr[2, 1, 0] = true;
        }

        //■
        //■
        //■■■
        if (pPieceName == 'V') {
            wkArr = new bool[3, 3, 1];
            wkArr[0, 0, 0] = true;
            wkArr[0, 1, 0] = true;
            wkArr[0, 2, 0] = wkArr[1, 2, 0] = wkArr[2, 2, 0] = true;
        }

        //■
        //■■
        // ■■
        if (pPieceName == 'W') {
            wkArr = new bool[3, 3, 1];
            wkArr[0, 0, 0] = true;
            wkArr[0, 1, 0] = wkArr[1, 1, 0] = true;
            wkArr[1, 2, 0] = wkArr[2, 2, 0] = true;
        }

        // ■
        //■■■
        // ■
        if (pPieceName == 'X') {
            wkArr = new bool[3, 3, 1];
            wkArr[1, 0, 0] = true;
            wkArr[0, 1, 0] = wkArr[1, 1, 0] = wkArr[2, 1, 0] = true;
            wkArr[1, 2, 0] = true;
        }

        //■■
        // ■
        // ■■
        if (pPieceName == 'Z') {
            wkArr = new bool[3, 3, 1];
            wkArr[0, 0, 0] = wkArr[1, 0, 0] = true;
            wkArr[1, 1, 0] = true;
            wkArr[1, 2, 0] = wkArr[2, 2, 0] = true;
        }

        //■
        //■
        //■■
        if (pPieceName == 'l') {
            wkArr = new bool[2, 3, 1];
            wkArr[0, 0, 0] = true;
            wkArr[0, 1, 0] = true;
            wkArr[0, 2, 0] = wkArr[1, 2, 0] = true;
        }

        // ■
        //■■
        //■
        if (pPieceName == 'n') {
            wkArr = new bool[2, 3, 1];
            wkArr[1, 0, 0] = true;
            wkArr[0, 1, 0] = wkArr[1, 1, 0] = true;
            wkArr[0, 2, 0] = true;
        }

        //■■
        //■■
        if (pPieceName == 'o') {
            wkArr = new bool[2, 2, 1];
            wkArr[0, 0, 0] = wkArr[1, 0, 0] = true;
            wkArr[0, 1, 0] = wkArr[1, 1, 0] = true;
        }

        //■■■
        // ■
        if (pPieceName == 't') {
            wkArr = new bool[3, 2, 1];
            wkArr[0, 0, 0] = wkArr[1, 0, 0] = wkArr[2, 0, 0] = true;
            wkArr[1, 1, 0] = true;
        }

        //■
        //■
        //■
        if (pPieceName == 'a') {
            wkArr = new bool[1, 3, 1];
            wkArr[0, 0, 0] = true;
            wkArr[0, 1, 0] = true;
            wkArr[0, 2, 0] = true;
        }

        //■
        //■■
        if (pPieceName == 'b') {
            wkArr = new bool[2, 2, 1];
            wkArr[0, 0, 0] = true;
            wkArr[0, 1, 0] = wkArr[1, 1, 0] = true;
        }

        //■
        //■
        if (pPieceName == 'd') {
            wkArr = new bool[1, 2, 1];
            wkArr[0, 0, 0] = true;
            wkArr[0, 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)
    {
        if (pTargetX + pPieceMap.GetUpperBound(0) > pBanArr.GetUpperBound(0)) return false;
        if (pTargetY + pPieceMap.GetUpperBound(1) > pBanArr.GetUpperBound(1)) return false;
        if (pTargetZ + pPieceMap.GetUpperBound(2) > pBanArr.GetUpperBound(2)) return false;

        for (int X = 0; X <= pPieceMap.GetUpperBound(0); X++) {
            for (int Y = 0; Y <= pPieceMap.GetUpperBound(1); Y++) {
                for (int Z = 0; Z <= pPieceMap.GetUpperBound(2); Z++) {
                    if (pPieceMap[X, Y, Z]
                     && pBanArr[pTargetX + X, pTargetY + Y, pTargetZ + Z] != ' ')
                        return false;
                }
            }
        }
        return true;
    }

    //盤面を出力
    static void PrintBan(List<char[, ,]> pBanArrList)
    {
        var sb = new System.Text.StringBuilder();

        for (int I = 0; I <= pBanArrList.Count - 1; I++) {
            sb.AppendFormat("{0}個目の立体", I + 1);
            sb.AppendLine();
            for (int Z = 0; Z <= pBanArrList[I].GetUpperBound(2); Z++) {
                sb.AppendFormat("Z={0}の平面", Z);
                sb.AppendLine();
                for (int Y = 0; Y <= pBanArrList[I].GetUpperBound(1); Y++) {
                    for (int X = 0; X <= pBanArrList[I].GetUpperBound(0); X++) {
                        sb.Append(pBanArrList[I][X, Y, Z]);
                    }
                    sb.AppendLine();
                }
            }
        }
        Console.WriteLine(sb.ToString());
    }
}


実行結果

1000000回目のPopを行いました。
2000000回目のPopを行いました。
解を発見。経過時間=00:01:01.6985616
1個目の立体
Z=0の平面
ddbt
aobP
loZP
lnZP
Z=1の平面
VFTt
aobt
loZP
nnWP
Z=2の平面
VFTt
aFZU
lFZU
nWWU
Z=3の平面
VTTT
VFXU
VXXX
WWXU


解説

ピースの数が多いので、こまめに枝切りしてます。