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

20-009 Y-25(立体)

問題

小田原充宏さんのY-25の立体問題を解きます。



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


Q2 2×5×6の直方体


Q3 3×4×5の直方体


Q4 2×4×10の直方体


Q5 2×5×8の直方体


Q6 4×4×5の直方体


Q7 4×5×5の直方体


Q8 2×5×11の直方体


Q9 2×4×15の直方体


Q10 5×5×5の立方体


Q11 5×5×5−5


ソース

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

class Program
{
    //Yペントミノの回転リスト
    static List<bool[, ,]> PentominoKaitenList = new List<bool[, ,]>();

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

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

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

        //直方体を作成する
        Func<int, int, int, int[, ,]> wkFunc = (pLen_X, pLen_Y, pLen_Z) =>
        {
            int[, ,] WillReturnArr = new int[pLen_X, pLen_Y, pLen_Z];
            for (int X = 0; X <= WillReturnArr.GetUpperBound(0); X++)
                for (int Y = 0; Y <= WillReturnArr.GetUpperBound(1); Y++)
                    for (int Z = 0; Z <= WillReturnArr.GetUpperBound(2); Z++)
                        WillReturnArr[X, Y, Z] = 1;
            return WillReturnArr;
        };

        int[, ,] Q02Arr = wkFunc(2, 5, 6);
        int[, ,] Q03Arr = wkFunc(3, 4, 5);
        int[, ,] Q04Arr = wkFunc(2, 4, 10);
        int[, ,] Q05Arr = wkFunc(2, 5, 8);
        int[, ,] Q06Arr = wkFunc(4, 4, 5);
        int[, ,] Q07Arr = wkFunc(4, 5, 5);
        int[, ,] Q08Arr = wkFunc(2, 5, 11);
        int[, ,] Q09Arr = wkFunc(2, 4, 15);
        int[, ,] Q10Arr = wkFunc(5, 5, 5);
        int[, ,] Q11Arr = wkFunc(5, 5, 5);
        for (int Z = 0; Z <= 4; Z++) Q11Arr[2, 2, Z] = 0;

        int[, ,] QuestionArr = Q02Arr;

        UB_X = QuestionArr.GetUpperBound(0);
        UB_Y = QuestionArr.GetUpperBound(1);
        UB_Z = QuestionArr.GetUpperBound(2);

        //Yペントミノを回転させた配置のListを返す
        PentominoKaitenList = DerivePentominoKaitenList();

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.BanArr = new char[UB_X + 1, UB_Y + 1, UB_Z + 1];
        for (int X = 0; X <= UB_X; X++)
            for (int Y = 0; Y <= UB_Y; Y++)
                for (int Z = 0; Z <= UB_Z; Z++)
                    WillPush.BanArr[X, Y, Z] = (QuestionArr[X, Y, Z] == 1 ? ' ' : '*');

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

        int PopCnt = 0;

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

            if (++PopCnt % 100000 == 0) {
                Console.WriteLine("{0}回目のPop。経過時間={1}", PopCnt, sw.Elapsed);
            }

            //クリア判定
            if (Popped.BanArr.Cast<char>().All(X => X != ' ')) {
                Console.WriteLine("解を発見。経過時間={0}", sw.Elapsed);
                PrintAnswer(Popped.BanArr);
                return;
            }

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

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

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

            //Yペントミノの配置候補リスト
            List<bool[, ,]> HaitiKouhoList = new List<bool[, ,]>();
            HaitiKouhoList.AddRange(PentominoKaitenList);

            //Push後にX座標をインクリメントできるか?
            bool CanIncrement = true;

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

            //マス目にYペントミノを埋めれない候補をRemove
            HaitiKouhoList.RemoveAll(X =>
                CanFillPiece(X, Popped.CurrX, Popped.CurrY, Popped.CurrZ, Popped.BanArr) == false);

            //10進数をchar型に変換
            Func<int, char> DecToStr = (pInt) =>
            {
                if (pInt < 10) return (char)((int)'1' + pInt - 1);
                return (char)((int)'A' + pInt - 10);
            };

            //Yペントミノを配置する経路のPush
            foreach (bool[, ,] AnyPieceMap in HaitiKouhoList) {
                WillPush.BanArr = (char[, ,])Popped.BanArr.Clone();
                WillPush.PentominoCnt = Popped.PentominoCnt + 1;
                WillPush.CurrX = Popped.CurrX;
                if (CanIncrement) WillPush.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.BanArr[Popped.CurrX + X, Popped.CurrY + Y, Popped.CurrZ + Z]
                                = DecToStr(WillPush.PentominoCnt);
                        }
                    }
                }

                stk.Push(WillPush);
            }

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

    //Yペントミノを回転させた配置のListを返す
    static List<bool[, ,]> DerivePentominoKaitenList()
    {
        var WillReturn = new List<bool[, ,]>();
        bool[, ,] wkArr = null;

        // ■
        //■■■■
        wkArr = new bool[4, 2, 1];
        wkArr[0, 0, 0] = false; wkArr[1, 0, 0] = true; wkArr[2, 0, 0] = wkArr[3, 0, 0] = false;
        wkArr[0, 1, 0] = wkArr[1, 1, 0] = wkArr[2, 1, 0] = wkArr[3, 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) > UB_X) return false;
        if (pTargetY + pPieceMap.GetUpperBound(1) > UB_Y) return false;
        if (pTargetZ + pPieceMap.GetUpperBound(2) > UB_Z) 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 PrintAnswer(char[, ,] pBanArr)
    {
        char[, ,] wkBanArr = ArrBoundAscSort(pBanArr);

        var sb = new System.Text.StringBuilder();

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

    //配列の次元の長さがZ軸、X軸、Y軸で昇順になるように変換
    static char[, ,] ArrBoundAscSort(char[, ,] pBanArr)
    {
        var LengthDict = new Dictionary<char, int>();
        LengthDict['X'] = pBanArr.GetUpperBound(0);
        LengthDict['Y'] = pBanArr.GetUpperBound(1);
        LengthDict['Z'] = pBanArr.GetUpperBound(2);

        IEnumerable<int> wkValues = LengthDict.Values.OrderByDescending(X => X);
        char[, ,] WillReturnArr = new char[wkValues.ElementAt(0) + 1,
                                           wkValues.ElementAt(1) + 1,
                                           wkValues.ElementAt(2) + 1];

        IEnumerable<char> wkKeys = LengthDict.OrderByDescending(X => X.Value).Select(X => X.Key);

        for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
            for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
                for (int Z = 0; Z <= pBanArr.GetUpperBound(2); Z++) {
                    char SetVal = pBanArr[X, Y, Z];

                    if (wkKeys.SequenceEqual(new char[] { 'Z', 'Y', 'X' }))
                        WillReturnArr[Z, Y, X] = SetVal;
                    if (wkKeys.SequenceEqual(new char[] { 'Z', 'X', 'Y' }))
                        WillReturnArr[Z, X, Y] = SetVal;

                    if (wkKeys.SequenceEqual(new char[] { 'Y', 'Z', 'X' }))
                        WillReturnArr[Y, Z, X] = SetVal;
                    if (wkKeys.SequenceEqual(new char[] { 'Y', 'X', 'Z' }))
                        WillReturnArr[Y, X, Z] = SetVal;

                    if (wkKeys.SequenceEqual(new char[] { 'X', 'Y', 'Z' }))
                        WillReturnArr[X, Y, Z] = SetVal;
                    if (wkKeys.SequenceEqual(new char[] { 'X', 'Z', 'Y' }))
                        WillReturnArr[X, Z, Y] = SetVal;
                }
            }
        }
        return WillReturnArr;
    }
}


実行結果

解を発見。経過時間=00:00:00.1975823
Z=0の平面
129999
15555C
1175CC
17777C
4444AC
Z=1の平面
22229B
36666B
3386BB
38888B
34AAAA


解説

枝切りが早くなるので、問題は
配列の次元の長さがX軸、Y軸、Z軸で昇順なるように定義して、
解の出力時に、ArrBoundAscSortメソッドで
配列の次元の長さがZ軸、X軸、Y軸で昇順になるように変換してます。

CanIncrementという変数で、現在のマス目が空白でない場合でピースを配置したら
X座標をインクリメントしてからPushするようにしてます。