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

20-024 イージーキューブの合体問題(立体)(Q12)

問題

イージーキューブ1と2の合体問題の立体問題のQ12を解きます。



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





ソース

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 = { 'P', 'l', 'n', 'o', 't', 'a', 'b',
                                   'F', 'U', 'd' };

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

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

        List<int[, ,]> QuestionArrList = new List<int[, ,]>();
        int[, ,] wkArr1 = new int[3, 3, 1];
        int[, ,] wkArr2 = new int[3, 3, 1];
        int[, ,] wkArr3 = new int[3, 3, 2];
        int[, ,] wkArr4 = new int[3, 3, 2];
        Action<int[, ,]> wkAct = (pTargetArr) =>
        {
            for (int X = 0; X <= pTargetArr.GetUpperBound(0); X++) {
                for (int Y = 0; Y <= pTargetArr.GetUpperBound(1); Y++) {
                    for (int Z = 0; Z <= pTargetArr.GetUpperBound(2); Z++) {
                        pTargetArr[X, Y, Z] = 1;
                    }
                }
            }
            QuestionArrList.Add(pTargetArr);
        };
        wkAct(wkArr1); wkAct(wkArr2); wkAct(wkArr3); wkAct(wkArr4);
        var CanUsePieceDict = new Dictionary<char, int>() {{ 'P', 1 },{ 'l', 1 },
                                                           { 'n', 2 },{ 'o', 2 },
                                                           { 't', 2 },{ 'a', 1 },
                                                           { 'b', 2 },{ 'F', 1 },
                                                           { 'U', 1 },{ 'd', 1 }};

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

        //Z軸のUBが2以上ならRemove
        foreach (char EachKey in HaitiKouhoListDict.Keys) {
            HaitiKouhoListDict[EachKey].RemoveAll(X => X.GetUpperBound(2) >= 2);
        }
        //aは回転させない
        HaitiKouhoListDict['a'].RemoveRange(1, HaitiKouhoListDict['a'].Count - 1);

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.BanArrList = new List<char[, ,]>();
        for (int I = 0; I <= QuestionArrList.Count - 1; I++) {
            int Curr_UB_X = QuestionArrList[I].GetUpperBound(0);
            int Curr_UB_Y = QuestionArrList[I].GetUpperBound(1);
            int Curr_UB_Z = QuestionArrList[I].GetUpperBound(2);

            WillPush.BanArrList.Add(new char[Curr_UB_X + 1, Curr_UB_Y + 1, Curr_UB_Z + 1]);
            for (int X = 0; X <= Curr_UB_X; X++)
                for (int Y = 0; Y <= Curr_UB_Y; Y++)
                    for (int Z = 0; Z <= Curr_UB_Z; Z++)
                        WillPush.BanArrList[I][X, Y, Z] = (QuestionArrList[I][X, Y, Z] == 1 ? ' ' : '*');
        }

        WillPush.CurrBanInd = 0;
        WillPush.CurrX = WillPush.CurrY = WillPush.CurrZ = 0;
        WillPush.UsePieceDict = new Dictionary<char, int>();
        stk.Push(WillPush);

        int AnwserCnt = 0;

        //降順にソートしておく
        PieceNameArr = PieceNameArr.OrderByDescending(X => X).ToArray();

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

            //クリア判定
            if (Popped.BanArrList[Popped.CurrBanInd].Cast<char>().All(X => X != ' ')) {
                if (++Popped.CurrBanInd > QuestionArrList.Count - 1) {

                    Console.WriteLine("解{0}を発見。経過時間={1}", ++AnwserCnt, sw.Elapsed);
                    PrintAnswer(Popped.BanArrList);
                    return;
                }
                Popped.CurrX = Popped.CurrY = Popped.CurrZ = 0;
            }

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

            //最終行を超えた場合
            if (Popped.CurrY > Popped.BanArrList[Popped.CurrBanInd].GetUpperBound(1)) {
                Popped.CurrY = 0;

                //最終高さを超えた場合
                if (++Popped.CurrZ > Popped.BanArrList[Popped.CurrBanInd].GetUpperBound(2))
                    continue;
            }

            foreach (char AnyPiece in PieceNameArr) {
                //1個目の立体の[0,0,0] <= 2個目の立体の[0,0,0]
                if (Popped.CurrBanInd == 1 &&
                    Popped.CurrX == 0 && Popped.CurrY == 0 && Popped.CurrZ == 0) {
                    if (Popped.BanArrList[0][0, 0, 0] > AnyPiece) break;
                }
                //3個目の立体の[0,0,0] <= 3個目の立体の[0,0,1]
                if (Popped.CurrBanInd == 2 &&
                    Popped.CurrX == 0 && Popped.CurrY == 0 && Popped.CurrZ == 1) {
                    if (Popped.BanArrList[2][0, 0, 0] > AnyPiece) break;
                }
                //4個目の立体の[0,0,0] <= 4個目の立体の[0,0,1]
                if (Popped.CurrBanInd == 3 &&
                    Popped.CurrX == 0 && Popped.CurrY == 0 && Popped.CurrZ == 1) {
                    if (Popped.BanArrList[3][0, 0, 0] > AnyPiece) break;
                }
                //3個目の立体の[0,0,0] <= 4個目の立体の[0,0,0]
                if (Popped.CurrBanInd == 3 &&
                    Popped.CurrX == 0 && Popped.CurrY == 0 && Popped.CurrZ == 0) {
                    if (Popped.BanArrList[2][0, 0, 0] > AnyPiece) break;
                }

                if (CanUsePieceDict.ContainsKey(AnyPiece) == false) continue;
                if (Popped.UsePieceDict.ContainsKey(AnyPiece)) {
                    //使用上限に到達している場合は、不可
                    if (Popped.UsePieceDict[AnyPiece] >= CanUsePieceDict[AnyPiece])
                        continue;
                }

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

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

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

                WillPush.UsePieceDict = new Dictionary<char, int>(Popped.UsePieceDict);
                if (WillPush.UsePieceDict.ContainsKey(AnyPiece))
                    WillPush.UsePieceDict[AnyPiece]++;
                else WillPush.UsePieceDict[AnyPiece] = 1;

                //ピースを配置する経路のPush処理
                foreach (bool[, ,] AnyPieceMap 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 <= AnyPieceMap.GetUpperBound(0); X++) {
                        for (int Y = 0; Y <= AnyPieceMap.GetUpperBound(1); Y++) {
                            for (int Z = 0; Z <= AnyPieceMap.GetUpperBound(2); Z++) {
                                if (AnyPieceMap[X, Y, Z] == false) continue;
                                WillPush.BanArrList[WillPush.CurrBanInd]
                                    [Popped.CurrX + X, Popped.CurrY + Y, Popped.CurrZ + Z]
                                    = AnyPiece;
                            }
                        }
                    }
                    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.UsePieceDict = Popped.UsePieceDict;
                stk.Push(WillPush);
            }
        }
    }

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

        //■■
        //■■
        //■
        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; wkArr[1, 2, 0] = false;
        }

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

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

        //■■
        //■■
        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[0, 1, 0] = false; wkArr[1, 1, 0] = true; wkArr[2, 1, 0] = false;
        }

        //■
        //■
        //■
        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[1, 0, 0] = false;
            wkArr[0, 1, 0] = wkArr[1, 1, 0] = true;
        }

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

        //■ ■
        //■■■
        if (pPieceName == 'U') {
            wkArr = new bool[3, 2, 1];
            wkArr[0, 0, 0] = true; wkArr[1, 0, 0] = false; wkArr[2, 0, 0] = true;
            wkArr[0, 1, 0] = wkArr[1, 1, 0] = wkArr[2, 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)
    {
        for (int X = 0; X <= pPieceMap.GetUpperBound(0); X++) {
            if (pTargetX + X > pBanArr.GetUpperBound(0)) return false;
            for (int Y = 0; Y <= pPieceMap.GetUpperBound(1); Y++) {
                if (pTargetY + Y > pBanArr.GetUpperBound(1)) return false;
                for (int Z = 0; Z <= pPieceMap.GetUpperBound(2); Z++) {
                    if (pTargetZ + Z > pBanArr.GetUpperBound(2)) return false;
                    if (pPieceMap[X, Y, Z]
                     && pBanArr[pTargetX + X, pTargetY + Y, pTargetZ + Z] != ' ')
                        return false;
                }
            }
        }
        return true;
    }

    //解を出力
    static void PrintAnswer(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());
    }
}


実行結果

解1を発見。経過時間=00:00:00.1951694
1個目の立体
Z=0の平面
PPP
lPP
lll
2個目の立体
Z=0の平面
UUt
Utt
UUt
3個目の立体
Z=0の平面
FFd
oFF
oFb
Z=1の平面
nnd
onn
obb
4個目の立体
Z=0の平面
abb
abn
atn
Z=1の平面
oon
oon
ttt


解説

20-023 イージーキューブの合体問題(立体)(Q4,Q6,Q8,Q10)の
問題を変更しただけのソースだと、処理時間がかかり過ぎたので、
問題をchar型の3次元配列のListジェネリックで持つようにしました。

さらに、Q12限定の枝切りロジックとして、下記を実装してます。
●Z軸のUBが2以上になるピースの配置を候補にしない
●ピースaは回転させない
●1個目の立体の[0,0,0] <= 2個目の立体の[0,0,0]
●3個目の立体の[0,0,0] <= 3個目の立体の[0,0,1]
●4個目の立体の[0,0,0] <= 4個目の立体の[0,0,1]
●3個目の立体の[0,0,0] <= 4個目の立体の[0,0,0]