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

20-044 ノノナイン

問題

小田原充宏さんのノノナインを解きます。

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


問題1 この中の9ピースを使い、9×9をつくる(ユニーク解)。

問題2 この中の16ピースを使い、9×16をつくる(ユニーク解)。


ソース

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

    static int mQuestionNo = 1;
    static char[,] mQuestionArr; //問題の初期盤面

    static int UB_X;
    static int UB_Y;

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

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

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

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

        //問題を定義
        QuestionDef();

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

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.BanArr = mQuestionArr;
        WillPush.CurrX = WillPush.CurrY = 0;
        WillPush.PieceCnt = 0;
        Stk.Push(WillPush);

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

            //クリア判定
            if (mQuestionNo == 1 && Popped.PieceCnt == 9
             || mQuestionNo == 2 && Popped.PieceCnt == 16) {
                Console.WriteLine("解を発見。経過時間={0}", sw.Elapsed);
                PrintBan(Popped.BanArr);
                break;
            }

            //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 EachPiece in PieceNameArr) {
                if (Array.IndexOf(UsedPieceArr, EachPiece) >= 0) continue;

                //回転解の除外
                if (EachPiece == '1') {
                    if (Popped.CurrX > UB_X / 2) continue;
                    if (Popped.CurrY > UB_Y / 2) continue;
                }

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

                //現在のマス目が空白の場合は、マス目を埋める必要あり
                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[,] EachPieceMap in HaitiKouhoList) {
                    WillPush.BanArr = (char[,])Popped.BanArr.Clone();
                    WillPush.CurrX = Popped.CurrX;
                    WillPush.CurrY = Popped.CurrY + 1;
                    WillPush.PieceCnt = Popped.PieceCnt + 1;

                    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.BanArr[Popped.CurrX + X, Popped.CurrY + Y] = EachPiece;
                        }
                    }
                    if (WillEdakiri(WillPush.BanArr)) continue;
                    Stk.Push(WillPush);
                }
            }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        // ■
        // ■
        //■■■■
        // ■■■
        if (pPieceName == 'I') {
            wkArr = new bool[4, 4];
            wkArr[1, 0] = true;
            wkArr[1, 1] = true;
            wkArr[0, 2] = wkArr[1, 2] = wkArr[2, 2] = wkArr[3, 2] = true;
            wkArr[1, 3] = wkArr[2, 3] = wkArr[3, 3] = 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;
    }

    //空白マスからUnionFindして9の倍数でなかったら枝切り
    static bool WillEdakiri(char[,] pBanArr)
    {
        var SpaceSetList = new List<HashSet<int>>();
        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                if (pBanArr[X, Y] != ' ') continue;

                int CurrHash = GetHash(X, Y);
                bool IsExist = false;
                foreach (var EachSet in SpaceSetList) {
                    if (EachSet.Contains(CurrHash)) {
                        IsExist = true;
                        break;
                    }
                }
                if (IsExist) continue;

                //始点を引数として、連結した空白マスのHashSetを返す
                HashSet<int> wkSet = ExecUnionFind(pBanArr, X, Y);

                //9の倍数でなかったら枝切り
                if (wkSet.Count % 9 > 0) return true;

                SpaceSetList.Add(wkSet);
            }
        }
        return false;
    }

    //UnionFindで島のチェック用
    struct JyoutaiDefUnionFind
    {
        internal int CurrX;
        internal int CurrY;
    }

    //始点を引数として、連結した空白マスのHashSetを返す
    static HashSet<int> ExecUnionFind(char[,] pBanArr, int pStaX, int pStaY)
    {
        var Stk = new Stack<JyoutaiDefUnionFind>();
        JyoutaiDefUnionFind WillPush;

        WillPush.CurrX = pStaX;
        WillPush.CurrY = pStaY;
        Stk.Push(WillPush);

        var VisitedSet = new HashSet<int>();
        VisitedSet.Add(GetHash(pStaX, pStaY));

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

            Action<int, int> PushSyori = (pNewX, pNewY) =>
            {
                if (pNewX < 0 || UB_X < pNewX) return;
                if (pNewY < 0 || UB_Y < pNewY) return;

                if (pBanArr[pNewX, pNewY] != ' ') return;
                if (VisitedSet.Add(GetHash(pNewX, pNewY)) == false) return;

                WillPush.CurrX = pNewX;
                WillPush.CurrY = pNewY;
                Stk.Push(WillPush);
            };

            PushSyori(Popped.CurrX, Popped.CurrY - 1);
            PushSyori(Popped.CurrX, Popped.CurrY + 1);
            PushSyori(Popped.CurrX - 1, Popped.CurrY);
            PushSyori(Popped.CurrX + 1, Popped.CurrY);
        }
        return VisitedSet;
    }

    static int GetHash(int pX, int pY)
    {
        return (UB_Y + 1) * pX + pY;
    }

    //盤面を出力
    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] == ' ' ? '/' : pBanArr[X, Y]);
            }
            sb.AppendLine();
        }
        Console.WriteLine(sb.ToString());
    }
}


実行結果

解を発見。経過時間=00:00:22.5758637
111156666
111155566
1888D5566
888DD5563
888DD5333
GGDDD3333
GGGDHH993
GGHHHH999
GGHHH9999


解説

全てのピースが9単位なので、問題4の高速化で、
面積が9の倍数でない空白を連結した島が存在したら、
枝切りしてます。