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

20-016 アソートキューブ(平面)

問題

小田原充宏さんのアソートキューブの平面問題を解きます。



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


Q1 8×8を作る。解数は100万以上


Q2 nを除いたピースで、4×5を3つ作る。


ソース

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 = "FPTUVWXZlnotabd".ToCharArray();

    static int mQuestionNo = 1;
    static List<char[,]> mQuestionArrList = new List<char[,]>(); //問題の初期盤面

    static int UB_X;
    static int UB_Y;

    struct JyoutaiDef
    {
        internal List<char[,]> BanArrList;
        internal int CurrBanInd;
        internal int CurrX;
        internal int CurrY;
        internal HashSet<char> UsedPieceSet;
    }

    //問題を定義
    static void QuestionDef()
    {
        if (mQuestionNo == 1) {
            UB_X = UB_Y = 8 - 1;

            char[,] wkArr = new char[UB_X + 1, UB_Y + 1];
            for (int X = 0; X <= UB_X; X++)
                for (int Y = 0; Y <= UB_Y; Y++)
                    wkArr[X, Y] = ' ';
            mQuestionArrList.Add(wkArr);
        }

        if (mQuestionNo == 2) {
            UB_X = 4 - 1;
            UB_Y = 5 - 1;

            char[,] wkArr = new char[UB_X + 1, UB_Y + 1];
            for (int X = 0; X <= UB_X; X++)
                for (int Y = 0; Y <= UB_Y; Y++)
                    wkArr[X, Y] = ' ';

            for (int I = 1; I <= 3; I++)
                mQuestionArrList.Add(wkArr);
        }
    }

    static void Main()
    {
        //問題を定義
        QuestionDef();

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

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.BanArrList = mQuestionArrList;
        WillPush.CurrBanInd = 0;
        WillPush.CurrX = WillPush.CurrY = 0;
        WillPush.UsedPieceSet = new HashSet<char>();
        Stk.Push(WillPush);

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

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

                    Console.WriteLine("解を発見");
                    Popped.BanArrList.ForEach(X => PrintBan(X));
                    return;
                }
                Popped.CurrX = Popped.CurrY = 0;
            }

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

                //特殊枝切り 対称解の除外
                if (Popped.CurrY == 1) {
                    if (Popped.BanArrList[Popped.CurrBanInd][0, 0] >
                       Popped.BanArrList[Popped.CurrBanInd][UB_X, 0])
                        continue;
                }
            }

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

            foreach (char EachPiece in PieceNameArr) {
                if (Popped.UsedPieceSet.Contains(EachPiece)) continue;

                //2問目は、nを使用不可
                if (mQuestionNo == 2 && EachPiece == 'n') continue;

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

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

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

                WillPush.UsedPieceSet = new HashSet<char>(Popped.UsedPieceSet);
                WillPush.UsedPieceSet.Add(EachPiece);

                //ピースを配置する経路のPush処理
                foreach (bool[,] EachPieceMap in HaitiKouhoList) {
                    WillPush.CurrBanInd = Popped.CurrBanInd;
                    WillPush.BanArrList = new List<char[,]>(Popped.BanArrList);
                    WillPush.BanArrList[WillPush.CurrBanInd] =
                        (char[,])Popped.BanArrList[Popped.CurrBanInd].Clone();
                    if (EachPiece == 'F' || EachPiece == 'T' || EachPiece == 'V'
                     || EachPiece == 'W' || EachPiece == 'Z' || EachPiece == 'l'
                     || EachPiece == 'o' || EachPiece == 'b' || EachPiece == 'd')
                        WillPush.CurrX = Popped.CurrX;
                    else WillPush.CurrX = Popped.CurrX + 1;

                    WillPush.CurrY = Popped.CurrY;

                    for (int X = 0; X <= EachPieceMap.GetUpperBound(0); X++) {
                        for (int Y = 0; Y <= EachPieceMap.GetUpperBound(1); Y++) {
                            if (EachPieceMap[X, Y] == false) continue;
                            WillPush.BanArrList[WillPush.CurrBanInd]
                                [Popped.CurrX + X, Popped.CurrY + Y] = EachPiece;
                        }
                    }
                    Stk.Push(WillPush);
                }
            }

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

    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return DeriveKaitenArrList(wkArr);
    }

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

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

        for (int I = 1; I <= 8; I++) KaitenArrList.Add(null);
        for (int P = 0; P <= 6; P += 2) KaitenArrList[P] = new bool[BaseArrUB_X + 1, BaseArrUB_Y + 1];
        for (int P = 1; P <= 7; P += 2) KaitenArrList[P] = new bool[BaseArrUB_Y + 1, BaseArrUB_X + 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][Y, BaseArrUB_X - X] = SetVal;
                KaitenArrList[2][BaseArrUB_X - X, BaseArrUB_Y - Y] = SetVal;
                KaitenArrList[3][BaseArrUB_Y - Y, X] = SetVal;

                KaitenArrList[4][X, BaseArrUB_Y - Y] = SetVal;
                KaitenArrList[5][BaseArrUB_Y - Y, BaseArrUB_X - X] = SetVal;
                KaitenArrList[6][BaseArrUB_X - X, Y] = SetVal;
                KaitenArrList[7][Y, X] = 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 PrintBan(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());
    }
}


実行結果

解を発見
ddbbaaat
oobnlltt
ooZnnlPt
ZZZTnlPP
ZTTTWWPP
UUXTFWWV
UXXXFFWV
UUXFFVVV


解説

20-024 イージーキューブの合体問題(立体)(Q12)のソースをベースにしてます。