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

19-04 かくれんぼパズル

問題

匹見パズルのかくれんぼパズルを解きます。

    

ワクの中に4つのピースを入れるのが基本。
とりあえず入れてみるだけでもちょっと戸惑います。

さらに問題のシートが10枚ついています。
シートを1枚ずつワクの中に置き、
そこに描かれた動物が全部かくれるようにピースを入れていきます。





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

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

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

Q01
□□□□□
□□□□□
□□動動□
□動動□□
動動動□□

Q02
□□□動□
動動□動□
□□動□□
□動□動動
□動□□□

Q06
動□□□□
□動□動□
□□動□□
□動□動□
□□□□動

Q21
動□□□動
動□□□□
動□□□□
□□□□動
動□動□動

Q30
□□□□動
□動□□□
□動動□□
□□動動□
□□□□□

Q31
□□動□□
□動□動□
動□動□動
□□動□□
□□動□□

Q40
□□□□□
□□動□動
□□動動動
動動動□□
□動動□□


ソース

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 = { '1', '2', '3', '4' };

    const int UB = 5 * 2 - 1;

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

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

        int[,] Q01Arr = {{0,0,0,0,0},
                         {0,0,0,0,0},
                         {0,0,1,1,0},
                         {0,1,1,0,0},
                         {1,1,1,0,0}};

        int[,] Q02Arr = {{0,0,0,1,0},
                         {1,1,0,1,0},
                         {0,0,1,0,0},
                         {0,1,0,1,1},
                         {0,1,0,0,0}};

        int[,] Q06Arr = {{1,0,0,0,0},
                         {0,1,0,1,0},
                         {0,0,1,0,0},
                         {0,1,0,1,0},
                         {0,0,0,0,1}};

        int[,] Q21Arr = {{1,0,0,0,1},
                         {1,0,0,0,0},
                         {1,0,0,0,0},
                         {0,0,0,0,1},
                         {1,0,1,0,1}};

        int[,] Q30Arr = {{0,0,0,0,1},
                         {0,1,0,0,0},
                         {0,1,1,0,0},
                         {0,0,1,1,0},
                         {0,0,0,0,0}};

        int[,] Q31Arr = {{0,0,1,0,0},
                         {0,1,0,1,0},
                         {1,0,1,0,1},
                         {0,0,1,0,0},
                         {0,0,1,0,0}};

        int[,] Q40Arr = {{0,0,0,0,0},
                         {0,0,1,0,1},
                         {0,0,1,1,1},
                         {1,1,1,0,0},
                         {0,1,1,0,0}};

        int[,] WkArr = Q01Arr;

        int[,] QuestionArr = new int[UB + 1, UB + 1];
        for (int X = 0; X <= UB; X++)
            for (int Y = 0; Y <= UB; Y++)
                QuestionArr[X, Y] = WkArr[Y / 2, X / 2];

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

        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] = (QuestionArr[X, Y] == 1 ? '?' : ' ');

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

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

            char[] wkArr = Popped.BanArr.Cast<char>().Distinct().ToArray();

            //クリア判定
            if (wkArr.All(X => X != '?') && wkArr.Contains('1') && wkArr.Contains('2')
                  && wkArr.Contains('3') && wkArr.Contains('4')) {
                Console.WriteLine("解を発見。経過時間={0}", sw.Elapsed);
                PrintAnswer(Popped.BanArr);
                return;
            }

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

            //最終行の1行前を超えた場合(必ずY軸方向の長さが2以上なので)
            if (Popped.CurrY + 1 > UB) continue;

            //使用済のピース名の配列
            char[] UsedPieceArr = Popped.BanArr.Cast<char>().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 + 1;
                    WillPush.CurrY = Popped.CurrY;

                    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;
                        }
                    }

                    //残りピースの面積で枝切り
                    if (IsValid(WillPush.BanArr) == false)
                        continue;

                    stk.Push(WillPush);
                }
            }

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

    //残りピースの面積で枝切り
    static bool IsValid(char[,] pBanArr)
    {
        char[] wkArr = pBanArr.Cast<char>().ToArray();

        int CanFillMenseki = 16 + 16 + 20 + 20;
        if (Array.IndexOf(wkArr, '1') >= 0) CanFillMenseki -= 16;
        if (Array.IndexOf(wkArr, '2') >= 0) CanFillMenseki -= 16;
        if (Array.IndexOf(wkArr, '3') >= 0) CanFillMenseki -= 20;
        if (Array.IndexOf(wkArr, '4') >= 0) CanFillMenseki -= 20;

        int NeedFillMenseki = wkArr.Count(X => X == '?');

        return NeedFillMenseki <= CanFillMenseki;
    }

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

        //■■■■
        //■■■■
        // ■■
        // ■■
        //■■
        //■■
        if (pPieceName == '1') {
            wkArr = new bool[4, 6];
            for (int Y = 0; Y <= 1; Y++) {
                wkArr[0, Y] = wkArr[1, Y] = wkArr[2, Y] = wkArr[3, Y] = true;
            }
            for (int Y = 2; Y <= 3; Y++) {
                wkArr[0, Y] = false; wkArr[1, Y] = wkArr[2, Y] = true; wkArr[3, Y] = false;
            }
            for (int Y = 4; Y <= 5; Y++) {
                wkArr[0, Y] = wkArr[1, Y] = true; wkArr[2, Y] = wkArr[3, Y] = false;
            }
        }

        //   ■■■■
        //   ■■■■
        //■■■■
        //■■■■
        if (pPieceName == '2') {
            wkArr = new bool[7, 4];

            for (int Y = 0; Y <= 1; Y++) {
                wkArr[0, Y] = wkArr[1, Y] = wkArr[2, Y] = false;
                wkArr[3, Y] = wkArr[4, Y] = wkArr[5, Y] = wkArr[6, Y] = true;
            }

            for (int Y = 2; Y <= 3; Y++) {
                wkArr[0, Y] = wkArr[1, Y] = wkArr[2, Y] = wkArr[3, Y] = true;
                wkArr[4, Y] = wkArr[5, Y] = wkArr[6, Y] = false;
            }
        }

        //■■■■
        //■■■■
        // ■■■■■■
        // ■■■■■■
        if (pPieceName == '3') {
            wkArr = new bool[7, 4];
            for (int Y = 0; Y <= 1; Y++) {
                wkArr[0, Y] = wkArr[1, Y] = wkArr[2, Y] = wkArr[3, Y] = true;
                wkArr[4, Y] = wkArr[5, Y] = wkArr[6, Y] = false;
            }

            for (int Y = 2; Y <= 3; Y++) {
                wkArr[0, Y] = false;
                wkArr[1, Y] = wkArr[2, Y] = wkArr[3, Y] = wkArr[4, Y] = wkArr[5, Y] = wkArr[6, Y] = true;
            }
        }

        //    ■■
        //  ■■■■
        //■■■■■■■■
        //■■    ■■■■
        if (pPieceName == '4') {
            wkArr = new bool[8, 4];

            wkArr[0, 0] = wkArr[1, 0] = wkArr[2, 0] = wkArr[3, 0] = false;
            wkArr[4, 0] = wkArr[5, 0] = true; wkArr[6, 0] = wkArr[7, 0] = false;

            wkArr[0, 1] = wkArr[1, 1] = false; wkArr[2, 1] = wkArr[3, 1] = true;
            wkArr[4, 1] = wkArr[5, 1] = true; wkArr[6, 1] = wkArr[7, 1] = false;

            wkArr[0, 2] = wkArr[1, 2] = wkArr[2, 2] = wkArr[3, 2] = true;
            wkArr[4, 2] = wkArr[5, 2] = wkArr[6, 2] = wkArr[7, 2] = true;

            wkArr[0, 3] = wkArr[1, 3] = true; wkArr[2, 3] = wkArr[3, 3] = false;
            wkArr[4, 3] = wkArr[5, 3] = wkArr[6, 3] = wkArr[7, 3] = true;
        }

        return DeriveKaitenArrList(wkArr);
    }

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

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

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

        //3個目は2個目を時計回りに90度回転
        //■■■
        //  ■

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

        //5個目は1個目とX軸で線対称
        //■■■
        //■

        //6個目は5個目を時計回りに90度回転
        //■■
        // ■
        // ■

        //7個目は6個目を時計回りに90度回転
        //  ■
        //■■■

        //8個目は7個目を時計回りに90度回転
        //■
        //■
        //■■

        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)
    {
        for (int X = 0; X <= pPieceMap.GetUpperBound(0); X++) {
            if (pTargetX + X > UB) return false;
            for (int Y = 0; Y <= pPieceMap.GetUpperBound(1); Y++) {
                if (pTargetY + Y > UB) return false;
                if (pPieceMap[X, Y] && pBanArr[pTargetX + X, pTargetY + 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++) {
            for (int X = 0; X <= UB; X++) {
                char wkChar = pBanArr[X, Y];
                sb.Append(wkChar == ' ' ? '*' : wkChar);
            }
            sb.AppendLine();
        }
        Console.WriteLine(sb.ToString());
    }
}


実行結果

解を発見。経過時間=00:01:41.4764882
********44
********44
2222**4444
2222**4444
11*222244*
11*222244*
*113333*44
*113333*44
1111333333
1111333333


解説

20-020 イージーキューブ2(平面)のソースをベースにしてます。

使用可能なピースの面積合計と、
埋める必要のある面積合計を比較して、枝切りしてます。