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

Cマガ電脳クラブ(第103回) ノナヒューブ

問題

シヴィ・ファフィという人が「ノナヒューブ」と名付けた9色キューブパズルがある。
ノナはギリシャ語の9、ヒューブは色合いのヒューと立方体のキューブからの合成語である。

このパズルは、
1*1*3のトリキューブ(立方体を3個つないだもの:Fig.A)が1個と、
1*1*2のダイキューブ(立方体を2個つないだもの:Fig.B)が12個で、
計13個のピースを使う。

Fig.A トリキューブ


Fig.B ダイキューブ


単位の立方体は、それぞれある色で塗られていて、色は全部で9種類ある。
誌面ではあいにく色での表現ができないので、9色をAからIのアルファベットで表すとすると、
各ピースを構成する立方体の色は次のようになる。

トリキューブ(1個)
(片方の端から順に)ABC

ダイキューブ(12個)
DE,CF,BH,GI,FH,CI
AE,DG,DH,EG,AI,BF

これを見ると、13個のピース全体で、9色それぞれにつき3個ずつの立方体があることが分かる。

さて、このパズルの目的は、
13個のピースを3*3*3の立方体(Fig.C)に組み、各面に9色全てが揃うようにすることだ。
何通りの組み方があるだろうか。

Fig.C 3*3*3の立方体


ノナヒューブの紹介画像


ソース

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

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

    const int UB = 3 - 1;

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

    struct JyoutaiDef
    {
        internal char[, ,] BanArr;
        internal int[, ,] BanPieceArr;
        internal int CurrX;
        internal int CurrY;
        internal int CurrZ;
        internal List<int> UsedPieceList;
    }

    struct AnswerInfoDef
    {
        internal char[, ,] BanArr;
        internal int[, ,] BanPieceArr;
    }

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

        for (int I = 1; I <= 13; I++) {
            HaitiKouhoListDict[I] = DeriveHaitiKouhoList(I);
        }

        var AnswerInfoList = new List<AnswerInfoDef>();
        for (int I = 1; I <= 3; I++) {
            Console.WriteLine("トリキューブの配置パターン{0}を検証中。経過時間={1}", I, sw.Elapsed);
            AnswerInfoList.AddRange(ExecDFS(I));
        }

        Console.WriteLine("回転解を除外後の解を列挙します。");
        RemoveKaitenkai(AnswerInfoList);
        foreach (AnswerInfoDef EachAnswerInfo in AnswerInfoList) {
            PrintAnswer(EachAnswerInfo.BanArr, EachAnswerInfo.BanPieceArr);
        }
        Console.WriteLine("解は{0}通り。経過時間={1}", AnswerInfoList.Count, sw.Elapsed);
    }

    //ピースABCの配置パターンを引数とし、深さ優先探索でピースを敷き詰める
    static AnswerInfoDef[] ExecDFS(int pABCPattern)
    {
        var WillReturn = new List<AnswerInfoDef>();

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.BanArr = new char[UB + 1, UB + 1, UB + 1];
        WillPush.BanPieceArr = new int[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] = ' ';

        if (pABCPattern == 1) {
            WillPush.BanArr[0, 0, 0] = 'A'; WillPush.BanPieceArr[0, 0, 0] = 1;
            WillPush.BanArr[1, 0, 0] = 'B'; WillPush.BanPieceArr[1, 0, 0] = 1;
            WillPush.BanArr[2, 0, 0] = 'C'; WillPush.BanPieceArr[2, 0, 0] = 1;
            WillPush.CurrX = 0; WillPush.CurrY = 1; WillPush.CurrZ = 0;
        }
        else if (pABCPattern == 2) {
            WillPush.BanArr[0, 1, 0] = 'A'; WillPush.BanPieceArr[0, 1, 0] = 1;
            WillPush.BanArr[1, 1, 0] = 'B'; WillPush.BanPieceArr[1, 1, 0] = 1;
            WillPush.BanArr[2, 1, 0] = 'C'; WillPush.BanPieceArr[2, 1, 0] = 1;
            WillPush.CurrX = WillPush.CurrY = WillPush.CurrZ = 0;
        }
        else {
            WillPush.BanArr[0, 1, 1] = 'A'; WillPush.BanPieceArr[0, 1, 1] = 1;
            WillPush.BanArr[1, 1, 1] = 'B'; WillPush.BanPieceArr[1, 1, 1] = 1;
            WillPush.BanArr[2, 1, 1] = 'C'; WillPush.BanPieceArr[2, 1, 1] = 1;
            WillPush.CurrX = WillPush.CurrY = WillPush.CurrZ = 0;
        }

        WillPush.UsedPieceList = new List<int>() { 1 };
        stk.Push(WillPush);

        bool wkIsClear, wkIsValid;
        while (stk.Count > 0) {
            JyoutaiDef Popped = stk.Pop();

            //クリア判定
            CheckBan(out wkIsClear, out wkIsValid, Popped.BanArr);
            if (wkIsClear) {
                AnswerInfoDef WillAdd;
                WillAdd.BanArr = Popped.BanArr;
                WillAdd.BanPieceArr = Popped.BanPieceArr;
                WillReturn.Add(WillAdd);
                continue;
            }

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

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

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

            for (int I = 1; I <= 13; I++) {
                if (Popped.UsedPieceList.Contains(I)) continue;

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

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

                //ピースを配置する経路のPush処理
                foreach (char[, ,] EachPieceArr in HaitiKouhoList) {
                    WillPush.BanArr = (char[, ,])Popped.BanArr.Clone();
                    WillPush.BanPieceArr = (int[, ,])Popped.BanPieceArr.Clone();
                    WillPush.CurrX = Popped.CurrX + 1;
                    WillPush.CurrY = Popped.CurrY;
                    WillPush.CurrZ = Popped.CurrZ;
                    WillPush.UsedPieceList = new List<int>(Popped.UsedPieceList) { I };

                    for (int X = 0; X <= EachPieceArr.GetUpperBound(0); X++) {
                        for (int Y = 0; Y <= EachPieceArr.GetUpperBound(1); Y++) {
                            for (int Z = 0; Z <= EachPieceArr.GetUpperBound(2); Z++) {
                                WillPush.BanArr[Popped.CurrX + X, Popped.CurrY + Y, Popped.CurrZ + Z]
                                    = EachPieceArr[X, Y, Z];
                                WillPush.BanPieceArr[Popped.CurrX + X, Popped.CurrY + Y, Popped.CurrZ + Z]
                                    = I;
                            }
                        }
                    }
                    if (WillEdakiri(pABCPattern, WillPush.BanArr)) continue;

                    CheckBan(out wkIsClear, out wkIsValid, WillPush.BanArr);
                    if (wkIsValid) stk.Push(WillPush);
                }
            }

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

    //枝切りするかを判定
    static bool WillEdakiri(int pABCPattern, char[, ,] BanArr)
    {
        //[0,0,0],[1,0,0],[2,0,0]にABCを配置した場合の回転解の除外
        if (pABCPattern == 1) {
            if (BanArr[1, 0, 0] == ' ' || BanArr[0, 0, 1] == ' ') return false;
            if (BanArr[1, 0, 0] > BanArr[0, 0, 1]) return true;
        }
        //[0,1,0],[1,1,0],[2,1,0]にABCを配置した場合の回転解の除外
        if (pABCPattern == 2) {
            if (BanArr[0, 0, 0] == ' ' || BanArr[2, 0, 0] == ' ') return false;
            if (BanArr[0, 0, 0] > BanArr[2, 0, 0]) return true;
        }
        //[0,1,1],[1,1,1],[2,1,1]にABCを配置した場合の回転解の除外
        if (pABCPattern == 3) {
            if (BanArr[0, 0, 0] == ' ' || BanArr[2, 0, 0] == ' ') return false;
            if (BanArr[0, 0, 0] > BanArr[2, 0, 0]) return true;
        }
        return false;
    }

    //ピース番号を引数として、回転させた配置のListを返す
    static List<char[, ,]> DeriveHaitiKouhoList(int pPieceNo)
    {
        char[, ,] wkArr = null;
        Action<char[]> wkAct = pArr =>
        {
            wkArr = new char[pArr.Length, 1, 1];
            for (int I = 0; I <= pArr.GetUpperBound(0); I++) {
                wkArr[I, 0, 0] = pArr[I];
            }
        };
        if (pPieceNo == 1) wkAct(new char[] { 'A', 'B', 'C' });
        if (pPieceNo == 2) wkAct(new char[] { 'D', 'E' });
        if (pPieceNo == 3) wkAct(new char[] { 'C', 'F' });
        if (pPieceNo == 4) wkAct(new char[] { 'B', 'H' });
        if (pPieceNo == 5) wkAct(new char[] { 'G', 'I' });
        if (pPieceNo == 6) wkAct(new char[] { 'F', 'H' });
        if (pPieceNo == 7) wkAct(new char[] { 'C', 'I' });
        if (pPieceNo == 8) wkAct(new char[] { 'A', 'E' });
        if (pPieceNo == 9) wkAct(new char[] { 'D', 'G' });
        if (pPieceNo == 10) wkAct(new char[] { 'D', 'H' });
        if (pPieceNo == 11) wkAct(new char[] { 'E', 'G' });
        if (pPieceNo == 12) wkAct(new char[] { 'A', 'I' });
        if (pPieceNo == 13) wkAct(new char[] { 'B', 'F' });
        return DeriveKaitenArrList(wkArr);
    }

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

        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 char[BaseUB_X + 1, BaseUB_Y + 1, BaseUB_Z + 1];
        for (int P = 8; P <= 15; P++) KaitenArrList[P] = new char[BaseUB_X + 1, BaseUB_Z + 1, BaseUB_Y + 1];
        for (int P = 16; P <= 23; P++) KaitenArrList[P] = new char[BaseUB_Y + 1, BaseUB_X + 1, BaseUB_Z + 1];
        for (int P = 24; P <= 31; P++) KaitenArrList[P] = new char[BaseUB_Y + 1, BaseUB_Z + 1, BaseUB_X + 1];
        for (int P = 32; P <= 39; P++) KaitenArrList[P] = new char[BaseUB_Z + 1, BaseUB_X + 1, BaseUB_Y + 1];
        for (int P = 40; P <= 47; P++) KaitenArrList[P] = new char[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++) {
                    char 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<char> wkEnum1 = KaitenArrList[I].Cast<char>();
                IEnumerable<char> wkEnum2 = KaitenArrList[J].Cast<char>();
                if (wkEnum1.SequenceEqual(wkEnum2) == false) continue;

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

    //盤面をチェック
    static void CheckBan(out bool pIsClear, out bool pIsValid, char[, ,] pBanArr)
    {
        pIsClear = false;
        pIsValid = true;

        Predicate<List<char>> IsOKPred = pList => pList.Count == pList.Distinct().Count();
        var wkList = new List<char>();

        //固定するX座標をループ
        for (int LoopX = 0; LoopX <= UB; LoopX++) {
            wkList.Clear();
            for (int LoopY = 0; LoopY <= UB; LoopY++) {
                for (int LoopZ = 0; LoopZ <= UB; LoopZ++) {
                    if (pBanArr[LoopX, LoopY, LoopZ] != ' ')
                        wkList.Add(pBanArr[LoopX, LoopY, LoopZ]);
                }
            }
            if (IsOKPred(wkList) == false) { pIsValid = false; return; }
        }

        //固定するY座標をループ
        for (int LoopY = 0; LoopY <= UB; LoopY++) {
            wkList.Clear();
            for (int LoopX = 0; LoopX <= UB; LoopX++) {
                for (int LoopZ = 0; LoopZ <= UB; LoopZ++) {
                    if (pBanArr[LoopX, LoopY, LoopZ] != ' ')
                        wkList.Add(pBanArr[LoopX, LoopY, LoopZ]);
                }
            }
            if (IsOKPred(wkList) == false) { pIsValid = false; return; }
        }

        //固定するZ座標をループ
        for (int LoopZ = 0; LoopZ <= UB; LoopZ++) {
            wkList.Clear();
            for (int LoopX = 0; LoopX <= UB; LoopX++) {
                for (int LoopY = 0; LoopY <= UB; LoopY++) {
                    if (pBanArr[LoopX, LoopY, LoopZ] != ' ')
                        wkList.Add(pBanArr[LoopX, LoopY, LoopZ]);
                }
            }
            if (IsOKPred(wkList) == false) { pIsValid = false; return; }
        }

        //[0,0,0],[2,0,0],[0,2,0],[2,2,0],[1,1,1]が5つとも埋まっている場合の枝切り
        if (pBanArr[0, 0, 0] != ' ' && pBanArr[2, 0, 0] != ' '
         && pBanArr[0, 2, 0] != ' ' && pBanArr[2, 2, 0] != ' '
         && pBanArr[1, 1, 1] != ' ') {
            if (pBanArr[0, 0, 0] != pBanArr[1, 1, 1]
             && pBanArr[2, 0, 0] != pBanArr[1, 1, 1]
             && pBanArr[0, 2, 0] != pBanArr[1, 1, 1]
             && pBanArr[2, 2, 0] != pBanArr[1, 1, 1]) {
                pIsValid = false; return;
            }
        }

        pIsClear = (pBanArr.Cast<char>().All(X => X != ' '));
    }

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

    //回転を除外
    static void RemoveKaitenkai(List<AnswerInfoDef> pTargetList)
    {
        Predicate<int> IsExist = (pCurrInd) =>
        {
            for (int I = 0; I <= pCurrInd - 1; I++) {
                bool[] IsOKArr = new bool[47];

                for (int X = 0; X <= UB; X++) {
                    for (int Y = 0; Y <= UB; Y++) {
                        for (int Z = 0; Z <= UB; Z++) {
                            char CurrVal = pTargetList[pCurrInd].BanArr[X, Y, Z];
                            char[, ,] wkP = pTargetList[I].BanArr;
                            if (wkP[X, Y, UB - Z] != CurrVal) IsOKArr[0] = true;
                            if (wkP[X, UB - Y, Z] != CurrVal) IsOKArr[1] = true;
                            if (wkP[X, UB - Y, UB - Z] != CurrVal) IsOKArr[2] = true;
                            if (wkP[UB - X, Y, Z] != CurrVal) IsOKArr[3] = true;
                            if (wkP[UB - X, Y, UB - Z] != CurrVal) IsOKArr[4] = true;
                            if (wkP[UB - X, UB - Y, Z] != CurrVal) IsOKArr[5] = true;
                            if (wkP[UB - X, UB - Y, UB - Z] != CurrVal) IsOKArr[6] = true;

                            if (wkP[X, Z, Y] != CurrVal) IsOKArr[7] = true;
                            if (wkP[X, Z, UB - Y] != CurrVal) IsOKArr[8] = true;
                            if (wkP[X, UB - Z, Y] != CurrVal) IsOKArr[9] = true;
                            if (wkP[X, UB - Z, UB - Y] != CurrVal) IsOKArr[10] = true;
                            if (wkP[UB - X, Z, Y] != CurrVal) IsOKArr[11] = true;
                            if (wkP[UB - X, Z, UB - Y] != CurrVal) IsOKArr[12] = true;
                            if (wkP[UB - X, UB - Z, Y] != CurrVal) IsOKArr[13] = true;
                            if (wkP[UB - X, UB - Z, UB - Y] != CurrVal) IsOKArr[14] = true;

                            if (wkP[Y, X, Z] != CurrVal) IsOKArr[15] = true;
                            if (wkP[Y, X, UB - Z] != CurrVal) IsOKArr[16] = true;
                            if (wkP[Y, UB - X, Z] != CurrVal) IsOKArr[17] = true;
                            if (wkP[Y, UB - X, UB - Z] != CurrVal) IsOKArr[18] = true;
                            if (wkP[UB - Y, X, Z] != CurrVal) IsOKArr[19] = true;
                            if (wkP[UB - Y, X, UB - Z] != CurrVal) IsOKArr[20] = true;
                            if (wkP[UB - Y, UB - X, Z] != CurrVal) IsOKArr[21] = true;
                            if (wkP[UB - Y, UB - X, UB - Z] != CurrVal) IsOKArr[22] = true;

                            if (wkP[Y, Z, X] != CurrVal) IsOKArr[23] = true;
                            if (wkP[Y, Z, UB - X] != CurrVal) IsOKArr[24] = true;
                            if (wkP[Y, UB - Z, X] != CurrVal) IsOKArr[25] = true;
                            if (wkP[Y, UB - Z, UB - X] != CurrVal) IsOKArr[26] = true;
                            if (wkP[UB - Y, Z, X] != CurrVal) IsOKArr[27] = true;
                            if (wkP[UB - Y, Z, UB - X] != CurrVal) IsOKArr[28] = true;
                            if (wkP[UB - Y, UB - Z, X] != CurrVal) IsOKArr[29] = true;
                            if (wkP[UB - Y, UB - Z, UB - X] != CurrVal) IsOKArr[30] = true;

                            if (wkP[Z, X, Y] != CurrVal) IsOKArr[31] = true;
                            if (wkP[Z, X, UB - Y] != CurrVal) IsOKArr[32] = true;
                            if (wkP[Z, UB - X, Y] != CurrVal) IsOKArr[33] = true;
                            if (wkP[Z, UB - X, UB - Y] != CurrVal) IsOKArr[34] = true;
                            if (wkP[UB - Z, X, Y] != CurrVal) IsOKArr[35] = true;
                            if (wkP[UB - Z, X, UB - Y] != CurrVal) IsOKArr[36] = true;
                            if (wkP[UB - Z, UB - X, Y] != CurrVal) IsOKArr[37] = true;
                            if (wkP[UB - Z, UB - X, UB - Y] != CurrVal) IsOKArr[38] = true;

                            if (wkP[Z, Y, X] != CurrVal) IsOKArr[39] = true;
                            if (wkP[Z, Y, UB - X] != CurrVal) IsOKArr[40] = true;
                            if (wkP[Z, UB - Y, X] != CurrVal) IsOKArr[41] = true;
                            if (wkP[Z, UB - Y, UB - X] != CurrVal) IsOKArr[42] = true;
                            if (wkP[UB - Z, Y, X] != CurrVal) IsOKArr[43] = true;
                            if (wkP[UB - Z, Y, UB - X] != CurrVal) IsOKArr[44] = true;
                            if (wkP[UB - Z, UB - Y, X] != CurrVal) IsOKArr[45] = true;
                            if (wkP[UB - Z, UB - Y, UB - X] != CurrVal) IsOKArr[46] = true;
                        }
                    }
                }

                if (IsOKArr.Contains(false))
                    return true;
            }
            return false;
        };

        for (int I = pTargetList.Count - 1; I >= 0; I--) {
            if (IsExist(I)) pTargetList.RemoveAt(I);
        }
    }

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

        sb.AppendLine("ピースの色配置");
        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();
            }
        }

        sb.AppendLine();
        sb.AppendLine("ピース番号の配置");
        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++) {
                    int CurrNum = pBanPieceArr[X, Y, Z];
                    if (CurrNum < 10) sb.Append(CurrNum);
                    if (CurrNum == 10) sb.Append('A');
                    if (CurrNum == 11) sb.Append('B');
                    if (CurrNum == 12) sb.Append('C');
                    if (CurrNum == 13) sb.Append('D');
                }
                sb.AppendLine();
            }
        }

        Console.WriteLine(sb.ToString());
    }
}


実行結果

トリキューブの配置パターン1を検証中。経過時間=00:00:00.1022062
トリキューブの配置パターン2を検証中。経過時間=00:01:31.2635058
トリキューブの配置パターン3を検証中。経過時間=00:01:47.8941876
回転解を除外後の解を列挙します。
ピースの色配置
Z=0の平面
ABC
GDH
IEF
Z=1の平面
DHG
FIE
CAB
Z=2の平面
EFI
BCA
HGD

ピース番号の配置
Z=0の平面
111
5AA
58D
Z=1の平面
26B
37B
38D
Z=2の平面
26C
47C
499

解は1通り。経過時間=00:02:19.8703198


解説

トリキューブの配置パターンごとに深さ優先探索してます。

底面と上面は、問題文により、1ブロックずつ9色ですが、
全部で27ブロックで、1色につき、3ブロックあるので、中間の面に
同じ色のブロックが存在すると解がなくなりますので、枝切り条件としてます。

また、立方体の中央に使用した色は、立方体の6面のどの面にも接しないので、
同じ色の残り2ブロックで、6面をカバーする必要があります。
よって、残り2ブロックが、立方体の対角線に存在し、6面をカバーしていることを、枝切り条件としてます。

解答例