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

20-075 Fペンタキューブ(立体)

問題

小田原充宏さんのFペンタキューブの立体問題を解きます。

ピースはFペンタキューブが複数個となります。
Fペンタキューブは回転や裏返しても使えます。


問題1 4×4×4の立方形 (すき間9単位)


ソース

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

class Program
{
    //ピースごとの配置候補
    static List<bool[, ,]> HaitiKouhoList;

    const int UB = 3;

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

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

        HaitiKouhoList = DeriveHaitiKouhoList();

        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.PassCnt = 0;
        Stk.Push(WillPush);

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

            //クリア判定
            if (Popped.PieceCnt == 11) {
                Console.WriteLine("解を発見。経過時間={0}", sw.Elapsed);
                PrintBan(Popped.BanArr);
                break;
            }

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

                //特殊枝切り1
                if (Popped.CurrY == 1 && Popped.CurrZ == 0) {
                    int LeftCnt = 0;
                    for (int X = 0; X <= 1; X++)
                        if (Popped.BanArr[X, 0, 0] == ' ')
                            LeftCnt++;

                    int RightCnt = 0;
                    for (int X = 2; X <= 3; X++)
                        if (Popped.BanArr[X, 0, 0] == ' ')
                            RightCnt++;

                    if (LeftCnt > RightCnt) continue;
                }

                //解に到達しない場合は枝切り
                if (Popped.CurrX >= 0 && Popped.CurrY == 3 && Popped.CurrZ == UB) {
                    continue;
                }
            }

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

                Popped.CurrZ++;

                //特殊枝切り2
                if (Popped.CurrX == 0 && Popped.CurrY == 0 && Popped.CurrZ == 2)
                    if (Popped.PieceCnt < 6) continue;
            }

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

            //マス目にピースを埋めれない候補をRemove
            wkHaitiKouhoList.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);
            };

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

                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.BanArr[Popped.CurrX + X, Popped.CurrY + Y, Popped.CurrZ + Z]
                                = DecToStr(WillPush.PieceCnt);
                        }
                    }
                }
                if (WillEdakiri(WillPush.BanArr, WillPush.PieceCnt) == false)
                    Stk.Push(WillPush);
            }

            //ピースを配置しない経路のPush
            WillPush.BanArr = Popped.BanArr;
            WillPush.CurrX = Popped.CurrX + 1;
            WillPush.CurrY = Popped.CurrY;
            WillPush.CurrZ = Popped.CurrZ;
            WillPush.PieceCnt = Popped.PieceCnt;
            WillPush.PassCnt = Popped.PassCnt;
            if (WillPush.BanArr[Popped.CurrX, Popped.CurrY, Popped.CurrZ] == ' ')
                WillPush.PassCnt++;

            if (WillPush.PassCnt <= 9)
                Stk.Push(WillPush);
        }
    }

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

        Action<int, int> SetAct = (pX, pY) => wkArr[pX, pY, 0] = true;

        // ■■
        //■■
        // ■
        wkArr = new bool[3, 3, 1];
        SetAct(1, 0); SetAct(2, 0);
        SetAct(0, 1); SetAct(1, 1);
        SetAct(1, 2);

        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) return false;
        if (pTargetY + pPieceMap.GetUpperBound(1) > UB) return false;
        if (pTargetZ + pPieceMap.GetUpperBound(2) > UB) 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;
    }

    //空白マスからUnionFindして、枝切り判定
    static bool WillEdakiri(char[, ,] pBanArr, int pPieceCnt)
    {
        var SpaceSetList = new List<HashSet<int>>();
        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] != ' ') continue;

                    int CurrHash = GetHash(X, Y, Z);
                    bool IsExist = false;
                    foreach (var EachSet in SpaceSetList) {
                        if (EachSet.Contains(CurrHash)) {
                            IsExist = true;
                            break;
                        }
                    }
                    if (IsExist) continue;

                    //始点を引数として、連結した空白マスのHashSetを返す
                    HashSet<int> wkSet = ExecUnionFind(pBanArr, X, Y, Z);

                    SpaceSetList.Add(wkSet);
                }
            }
        }
        int RestSum = SpaceSetList.Sum(A => A.Count / 5);
        return pPieceCnt + RestSum < 11;
    }

    //UnionFindで島のチェック用
    struct JyoutaiDefUnionFind
    {
        internal int CurrX;
        internal int CurrY;
        internal int CurrZ;
    }

    //始点を引数として、連結した空白マスのHashSetを返す
    static HashSet<int> ExecUnionFind(char[, ,] pBanArr, int pStaX, int pStaY, int pStaZ)
    {
        var Stk = new Stack<JyoutaiDefUnionFind>();
        JyoutaiDefUnionFind WillPush;

        WillPush.CurrX = pStaX;
        WillPush.CurrY = pStaY;
        WillPush.CurrZ = pStaZ;
        Stk.Push(WillPush);

        var VisitedSet = new HashSet<int>();
        VisitedSet.Add(GetHash(pStaX, pStaY, pStaZ));

        while (Stk.Count > 0) {
            JyoutaiDefUnionFind Popped = Stk.Pop();

            Action<int, int, int> PushSyori = (pNewX, pNewY, pNewZ) =>
            {
                if (pNewX < 0 || UB < pNewX) return;
                if (pNewY < 0 || UB < pNewY) return;
                if (pNewZ < 0 || UB < pNewZ) return;

                if (pBanArr[pNewX, pNewY, pNewZ] != ' ') return;
                int wkHash = GetHash(pNewX, pNewY, pNewZ);
                if (VisitedSet.Add(wkHash) == false) return;

                WillPush.CurrX = pNewX;
                WillPush.CurrY = pNewY;
                WillPush.CurrZ = pNewZ;
                Stk.Push(WillPush);
            };

            PushSyori(Popped.CurrX, Popped.CurrY, Popped.CurrZ - 1);
            PushSyori(Popped.CurrX, Popped.CurrY, Popped.CurrZ + 1);
            PushSyori(Popped.CurrX, Popped.CurrY - 1, Popped.CurrZ);
            PushSyori(Popped.CurrX, Popped.CurrY + 1, Popped.CurrZ);
            PushSyori(Popped.CurrX - 1, Popped.CurrY, Popped.CurrZ);
            PushSyori(Popped.CurrX + 1, Popped.CurrY, Popped.CurrZ);
        }
        return VisitedSet;
    }

    static int GetHash(int pX, int pY, int pZ)
    {
        return pX * (UB + 1) * (UB + 1) + pY * (UB + 1) + pZ;
    }

    //解を出力
    static void PrintBan(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] == ' ' ? '/' : pBanArr[X, Y, Z]);
                }
                sb.AppendLine();
            }
        }
        Console.WriteLine(sb.ToString());
    }
}


実行結果

解を発見。経過時間=00:12:18.5191397
Z=0の平面
/12/
/423
4443
455/
Z=1の平面
/173
6123
9128
/A55
Z=2の平面
6777
6123
9998
AA58
Z=3の平面
/7BB
6BB8
69B8
/AA/


解説

深さ優先探索でピースを敷き詰めてます。

Cマガ電脳クラブ(第060回) Fペントミノ
Cマガ電脳クラブ(第112回) Fペントミノ