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

21-01 Aqua Belle

問題

SmartGamesのAqua Belleを解きます。



全ピース(両面不可で回転可)を敷き詰めて、下記の両方を満たせばクリアです。
●泡が重なってないMonsterFishがいないこと
●泡が重なっているGoodFishがいないこと

Q01 (Starterの01問目)
□M□
M□□
□□□

ピース
■■ ■○ ○○ ■■
 ○

Q02 (Juniorの01問目)
□M□□
□G□M
□□G□
□□G□

ピース
 ■  ○ ■  ■
○■ ○■ ■○ ○■
○■    ■■

Q03 (Expertの01問目)
MM□□
□GG□
□GGG
□G□□

ピース
■■ ■○  ○ ■■ ■■ ■
 ○    ○■ ○     ○■

Q04 (Masterの01問目)
GGM□
□G□G
M□□□
M□GG

ピースは、Expertの01問目と同じ

Q05 (Masterの18問目)
□□G□
MM□G
MM□G
□□M□

ピースは、Expertの01問目と同じ

Q06 (Masterの19問目)
GGMM
□M□□
□G□□
M□GG

ピースは、Expertの01問目と同じ

Q07 (Masterの20問目)
G□M□
□M□G
G□GG
□MM□

ピースは、Expertの01問目と同じ

Q08
M□GG
M□□G
G□□G
G□□M

ピースは、Expertの01問目と同じ


ソース

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

class Program
{
    const char SP = ' '; //全角空白

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

    static int UB;

    static char[,] mQuestionArr; //問題の盤面
    static char[] mCanUsePieceArr; //使用可能なピース

    //問題を定義
    static void QuestionDef()
    {
        string[] Q01Arr ={"□M□",
                          "M□□",
                          "□□□"};
        char[] CanUsePieceArr01 = "1234".ToCharArray();

        string[] Q02Arr ={"□M□□",
                          "□G□M",
                          "□□G□",
                          "□□G□"};
        char[] CanUsePieceArr02 = "5678".ToCharArray();

        string[] Q03Arr ={"MM□□",
                          "□GG□",
                          "□GGG",
                          "□G□□"};
        char[] CanUsePieceArr03 = "126948".ToCharArray();

        string[] Q04Arr ={"GGM□",
                          "□G□G",
                          "M□□□",
                          "M□GG"};
        char[] CanUsePieceArr04 = CanUsePieceArr03;

        string[] Q05Arr ={"□□G□",
                          "MM□G",
                          "MM□G",
                          "□□M□"};
        char[] CanUsePieceArr05 = CanUsePieceArr03;

        string[] Q06Arr ={"GGMM",
                          "□M□□",
                          "□G□□",
                          "M□GG"};
        char[] CanUsePieceArr06 = CanUsePieceArr03;

        string[] Q07Arr ={"G□M□",
                          "□M□G",
                          "G□GG",
                          "□MM□"};
        char[] CanUsePieceArr07 = CanUsePieceArr03;

        string[] Q08Arr ={"M□GG",
                          "M□□G",
                          "G□□G",
                          "G□□M"};
        char[] CanUsePieceArr08 = CanUsePieceArr03;

        mQuestionArr = XYRev(Q01Arr);
        mCanUsePieceArr = CanUsePieceArr01;

        UB = mQuestionArr.GetUpperBound(0);
    }

    //String型を引数として、X座標とY座標の入れ替えたChar型の配列を返す
    static char[,] XYRev(string[] pStrArr)
    {
        int RevArrUB_X = pStrArr[0].Length - 1;
        int RevArrUB_Y = pStrArr.GetUpperBound(0);
        char[,] WillReturnArr = new char[RevArrUB_X + 1, RevArrUB_Y + 1];
        for (int X = 0; X <= RevArrUB_X; X++) {
            for (int Y = 0; Y <= RevArrUB_Y; Y++) {
                WillReturnArr[X, Y] = pStrArr[Y][X];
            }
        }
        return WillReturnArr;
    }

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

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

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

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;

        WillPush.BanArr = new char[UB + 1, UB + 1];
        for (int X = 0; X <= UB; X++)
            for (int Y = 0; Y <= UB; Y++)
                WillPush.BanArr[X, Y] = SP;

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

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

            //クリア判定
            if (Popped.BanArr.Cast<char>().All(X => X != SP)) {
                Console.WriteLine("解を発見。");

                Console.WriteLine("ピース配置");
                PrintBan(Popped.BanArr, false);

                Console.WriteLine("泡配置");
                PrintBan(Popped.BanArr, true);

                return;
            }

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

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

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

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

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

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

                //ピースを配置する経路のPush処理
                foreach (char[,] EachPieceMap in HaitiKouhoList) {
                    WillPush.BanArr = (char[,])Popped.BanArr.Clone();
                    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] == SP) continue;
                            WillPush.BanArr[Popped.CurrX + X, Popped.CurrY + Y] = EachPiece;
                        }
                    }
                    Stk.Push(WillPush);
                }
            }
            //現在のマス目が空白でない場合は、ピースを配置しない経路のPush
            if (Popped.BanArr[Popped.CurrX, Popped.CurrY] != SP) {
                WillPush.BanArr = Popped.BanArr;
                WillPush.CurrX = Popped.CurrX + 1;
                WillPush.CurrY = Popped.CurrY;
                Stk.Push(WillPush);
            }
        }
    }

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

        if (pPieceName == '1') {
            string[] wkStr ={"■■",
                             " ○"};
            wkArr = XYRev(wkStr);
        }
        if (pPieceName == '2') {
            string[] wkStr = { "■○" };
            wkArr = XYRev(wkStr);
        }
        if (pPieceName == '3') {
            string[] wkStr = { "○○" };
            wkArr = XYRev(wkStr);
        }
        if (pPieceName == '4') {
            string[] wkStr = { "■■" };
            wkArr = XYRev(wkStr);
        }
        if (pPieceName == '5') {
            string[] wkStr ={" ■",
                             "○■",
                             "○■"};
            wkArr = XYRev(wkStr);
        }
        if (pPieceName == '6') {
            string[] wkStr ={" ○",
                             "○■"};
            wkArr = XYRev(wkStr);
        }
        if (pPieceName == '7') {
            string[] wkStr ={"■ ",
                             "■○",
                             "■■"};
            wkArr = XYRev(wkStr);
        }
        if (pPieceName == '8') {
            string[] wkStr ={"■ ",
                             "○■"};
            wkArr = XYRev(wkStr);
        }
        if (pPieceName == '9') {
            string[] wkStr ={"■■",
                             "○ "};
            wkArr = XYRev(wkStr);
        }

        return DeriveKaitenArrList(wkArr);
    }

    //配列を引数として、回転させた配列のリストをDistinctして返す
    static List<char[,]> DeriveKaitenArrList(char[,] pBaseArr)
    {
        var KaitenArrList = new List<char[,]>();
        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 <= 2; P += 2) KaitenArrList[P] = new char[BaseArrUB_X + 1, BaseArrUB_Y + 1];
        for (int P = 1; P <= 3; P += 2) KaitenArrList[P] = new char[BaseArrUB_Y + 1, BaseArrUB_X + 1];

        for (int X = 0; X <= BaseArrUB_X; X++) {
            for (int Y = 0; Y <= BaseArrUB_Y; Y++) {
                char 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;
            }
        }

        //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<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 bool CanFillPiece(char[,] pPieceMap, int pTargetX, int pTargetY, char[,] pBanArr)
    {
        if (pTargetX + pPieceMap.GetUpperBound(0) > UB) return false;
        if (pTargetY + pPieceMap.GetUpperBound(1) > UB) return false;

        for (int X = 0; X <= pPieceMap.GetUpperBound(0); X++) {
            for (int Y = 0; Y <= pPieceMap.GetUpperBound(1); Y++) {
                if (pPieceMap[X, Y] != SP && pBanArr[pTargetX + X, pTargetY + Y] != SP)
                    return false;

                //MonsterFishのマスは、泡を配置しなければいけない
                if (pPieceMap[X, Y] == '■' && mQuestionArr[pTargetX + X, pTargetY + Y] == 'M')
                    return false;

                //GoodFishのマスは、泡を配置できない
                if (pPieceMap[X, Y] == '○' && mQuestionArr[pTargetX + X, pTargetY + Y] == 'G')
                    return false;
            }
        }
        return true;
    }

    //盤面を出力
    static void PrintBan(char[,] pBanArr, bool pAwaYuusen)
    {
        var sb = new System.Text.StringBuilder();
        for (int Y = 0; Y <= UB; Y++) {
            for (int X = 0; X <= UB; X++) {
                if (pAwaYuusen && mQuestionArr[X, Y] == 'M')
                    sb.Append('○');
                else sb.Append(pBanArr[X, Y]);
            }
            sb.AppendLine();
        }
        Console.WriteLine(sb.ToString());
    }
}


実行結果

解を発見。
ピース配置
322
311
441

泡配置
3○2
○11
441


解説

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