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

23-06 明治ハイミルクチョコレートパズル

問題

ハナヤマの明治ハイミルクチョコレートパズルを解きます。



12ピースは下記となります。
各ピースは長方形の組み合わせなので、
180度の回転、裏返し、裏返し180度の回転でも使えます。

■■■  ■■  ■
     ■■  ■
         ■

■■■  ■   ■
■    ■   ■
     ■   ■■
     ■

■    ■    ■■
■■   ■■  ■■
 ■

      ■   ■
■■■■ ■■■ ■■
          ■


ソース

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

class Program
{
    const int UB_X = 9 - 1;
    const int UB_Y = 5 - 1;

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

    static char[] PieceNameArr = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
                                   'B', 'C'};

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

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

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

        //回転解を除外(ピース6は回転させない)
        HaitiKouhoListDict['6'].RemoveRange(1, HaitiKouhoListDict['6'].Count - 1);

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

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

        int AnwserCnt = 0;

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

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

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

            //最終列を超えた場合
            if (Popped.CurrX > UB_X) continue;

            //使用済のピース名の配列
            char[] UsedPieceArr = Popped.BanArr.Cast<char>().Distinct().ToArray();

            foreach (char AnyPiece in PieceNameArr) {
                if (Array.IndexOf(UsedPieceArr, AnyPiece) >= 0) continue;

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

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

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

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

                    for (int X = 0; X <= AnyPieceMap.GetUpperBound(0); X++) {
                        for (int Y = 0; Y <= AnyPieceMap.GetUpperBound(1); Y++) {
                            if (AnyPieceMap[X, Y] == false) continue;
                            WillPush.BanArr[Popped.CurrX + X, Popped.CurrY + Y] = AnyPiece;
                        }
                    }
                    stk.Push(WillPush);
                }
            }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        //1個目はそのまま
        //■
        //■■■

        //2個目は1個目を時計回りに180度回転
        //■■■
        //  ■

        //3個目は2個目とX軸で線対称
        //■■■
        //■

        //4個目は3個目を時計回りに180度回転
        //  ■
        //■■■

        int BaseArrUB_X = pBaseArr.GetUpperBound(0);
        int BaseArrUB_Y = pBaseArr.GetUpperBound(1);

        for (int I = 1; I <= 4; I++) KaitenArrList.Add(null);
        for (int P = 0; P <= 3; P++) KaitenArrList[P] = new bool[BaseArrUB_X + 1, BaseArrUB_Y + 1];

        for (int X = 0; X <= BaseArrUB_X; X++) {
            for (int Y = 0; Y <= BaseArrUB_Y; Y++) {
                bool SetVal = pBaseArr[X, Y];
                KaitenArrList[0][X, Y] = SetVal;
                KaitenArrList[1][BaseArrUB_X - X, BaseArrUB_Y - Y] = SetVal;
                KaitenArrList[2][X, BaseArrUB_Y - Y] = SetVal;
                KaitenArrList[3][BaseArrUB_X - X, Y] = SetVal;
            }
        }

        //Distinctする
        for (int I = KaitenArrList.Count - 1; 0 <= I; I--) {
            for (int J = 0; J <= I - 1; J++) {
                if (KaitenArrList[I].GetUpperBound(0) !=
                    KaitenArrList[J].GetUpperBound(0)) continue;
                if (KaitenArrList[I].GetUpperBound(1) !=
                    KaitenArrList[J].GetUpperBound(1)) 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, char[,] pBanArr)
    {
        if (pTargetX + pPieceMap.GetUpperBound(0) > UB_X) return false;
        if (pTargetY + pPieceMap.GetUpperBound(1) > UB_Y) return false;
        
        for (int X = 0; X <= pPieceMap.GetUpperBound(0); X++) {
            for (int Y = 0; Y <= pPieceMap.GetUpperBound(1); Y++) {
                if (pPieceMap[X, Y] && pBanArr[pTargetX + X, pTargetY + Y] != ' ')
                    return false;
            }
        }
        return true;
    }

    //解を出力
    static void PrintAnswer(char[,] pBanArr)
    {
        var sb = new System.Text.StringBuilder();
        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                sb.Append(pBanArr[X, Y]);
            }
            sb.AppendLine();
        }
        Console.WriteLine(sb.ToString());
    }
}


実行結果

解1を発見。経過時間=00:00:00.3379384
CBBB99223
CCB995223
CAAAA5673
884445677
811145667


解説

ピースを90度回転させて使えないので、
DeriveKaitenArrListメソッドにおいて、
X成分の変位とY成分の変位を入れ替えた配列を作らないようにしてます。