AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC322-D Polyomino


問題へのリンク


C#のソース

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

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("....");
            WillReturn.Add("###.");
            WillReturn.Add(".#..");
            WillReturn.Add("....");
            WillReturn.Add("....");
            WillReturn.Add(".###");
            WillReturn.Add(".##.");
            WillReturn.Add("....");
            WillReturn.Add("..#.");
            WillReturn.Add(".##.");
            WillReturn.Add(".##.");
            WillReturn.Add(".##.");
            //Yes
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("###.");
            WillReturn.Add("#.#.");
            WillReturn.Add("##..");
            WillReturn.Add("....");
            WillReturn.Add("....");
            WillReturn.Add("..#.");
            WillReturn.Add("....");
            WillReturn.Add("....");
            WillReturn.Add("####");
            WillReturn.Add("##..");
            WillReturn.Add("#...");
            WillReturn.Add("#...");
            //Yes
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("##..");
            WillReturn.Add("#..#");
            WillReturn.Add("####");
            WillReturn.Add("....");
            WillReturn.Add("....");
            WillReturn.Add("##..");
            WillReturn.Add(".##.");
            WillReturn.Add("....");
            WillReturn.Add(".#..");
            WillReturn.Add(".#..");
            WillReturn.Add(".#..");
            WillReturn.Add(".#..");
            //No
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("....");
            WillReturn.Add("..#.");
            WillReturn.Add("....");
            WillReturn.Add("....");
            WillReturn.Add("....");
            WillReturn.Add("..#.");
            WillReturn.Add("....");
            WillReturn.Add("....");
            WillReturn.Add("....");
            WillReturn.Add("..#.");
            WillReturn.Add("....");
            WillReturn.Add("....");
            //No
        }
        else if (InputPattern == "Input5") {
            WillReturn.Add("....");
            WillReturn.Add("####");
            WillReturn.Add("#...");
            WillReturn.Add("#...");
            WillReturn.Add("....");
            WillReturn.Add("####");
            WillReturn.Add("...#");
            WillReturn.Add("..##");
            WillReturn.Add("....");
            WillReturn.Add("..##");
            WillReturn.Add("..#.");
            WillReturn.Add("..##");
            //No
        }
        else if (InputPattern == "Input6") {
            WillReturn.Add("###.");
            WillReturn.Add(".##.");
            WillReturn.Add("..#.");
            WillReturn.Add(".###");
            WillReturn.Add("....");
            WillReturn.Add("...#");
            WillReturn.Add("..##");
            WillReturn.Add("...#");
            WillReturn.Add("....");
            WillReturn.Add("#...");
            WillReturn.Add("#...");
            WillReturn.Add("#...");
            //Yes
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static char[,] mPiece1;
    static char[,] mPiece2;
    static char[,] mPiece3;

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

    static char[] PieceNameArr = { '1', '2', '3' };

    static int UB_X;
    static int UB_Y;

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

    static void Main()
    {
        List<string> InputList = GetInputList();
        mPiece1 = CreateBanArr(InputList.Take(4));
        mPiece2 = CreateBanArr(InputList.Skip(4).Take(4));
        mPiece3 = CreateBanArr(InputList.Skip(8));
        mPiece1 = ArrSyukusyou(mPiece1);
        mPiece2 = ArrSyukusyou(mPiece2);
        mPiece3 = ArrSyukusyou(mPiece3);

        // 埋める必要があるマスが1
        // 埋める必要がないマスが0
        int[,] Q01Arr = {{1,1,1,1},
                         {1,1,1,1},
                         {1,1,1,1},
                         {1,1,1,1}};

        int[,] WkArr = Q01Arr;

        UB_X = WkArr.GetUpperBound(1);
        UB_Y = WkArr.GetUpperBound(0);

        int[,] QuestionArr = new int[UB_X + 1, UB_Y + 1];
        for (int X = 0; X <= UB_X; X++)
            for (int Y = 0; Y <= UB_Y; Y++)
                QuestionArr[X, Y] = WkArr[Y, X];

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

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

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

        bool FoundAnswer = false;

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

            // クリア判定
            if (Popped.BanArr.Cast<char>().All(X => X != ' ')) {
                FoundAnswer = true;
                break;
            }

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

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

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

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

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

        Console.WriteLine(FoundAnswer ? "Yes" : "No");
    }

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

        Action<char[,]> wkAct = (pPiece) =>
        {
            wkArr = new bool[pPiece.GetUpperBound(0) + 1, pPiece.GetUpperBound(1) + 1];
            for (int X = 0; X <= pPiece.GetUpperBound(0); X++) {
                for (int Y = 0; Y <= pPiece.GetUpperBound(1); Y++) {
                    // マス目ありならTrue
                    // マス目なしならFalse
                    wkArr[X, Y] = pPiece[X, Y] == '#';
                }
            }
        };

        if (pPieceName == '1') wkAct(mPiece1);
        if (pPieceName == '2') wkAct(mPiece2);
        if (pPieceName == '3') wkAct(mPiece3);

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

        // 鏡像は不許可なので、4回RemoveAt
        KaitenArrList.RemoveAt(4);
        KaitenArrList.RemoveAt(4);
        KaitenArrList.RemoveAt(4);
        KaitenArrList.RemoveAt(4);

        // 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());
    }

    // 2次元配列の使用してない部分を縮小して返す
    static char[,] ArrSyukusyou(char[,] pTargetArr)
    {
        int XMin = pTargetArr.GetUpperBound(0), YMin = pTargetArr.GetUpperBound(1);
        int XMax = 0, YMax = 0;

        for (int X = 0; X <= pTargetArr.GetUpperBound(0); X++) {
            for (int Y = 0; Y <= pTargetArr.GetUpperBound(1); Y++) {
                if (pTargetArr[X, Y] == '.') continue; // マス目が未使用ならContinue
                if (XMin > X) XMin = X;
                if (YMin > Y) YMin = Y;
                if (XMax < X) XMax = X;
                if (YMax < Y) YMax = Y;
            }
        }

        char[,] WillReturnArr = new char[XMax - XMin + 1, YMax - YMin + 1];
        for (int X = 0; X <= WillReturnArr.GetUpperBound(0); X++) {
            for (int Y = 0; Y <= WillReturnArr.GetUpperBound(1); Y++) {
                WillReturnArr[X, Y] = pTargetArr[XMin + X, YMin + Y];
            }
        }

        return WillReturnArr;
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をcharの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static char[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new char[0, 0];
        }
        int UB_X = StrList[0].Length - 1;
        int UB_Y = StrList.Count - 1;

        char[,] WillReturn = new char[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = StrList[Y][X];
            }
        }
        return WillReturn;
    }

    ////////////////////////////////////////////////////////////////
    // 2次元配列(char型)のデバッグ出力
    ////////////////////////////////////////////////////////////////
    static void PrintBan(char[,] pBanArr)
    {
        var sb = new System.Text.StringBuilder();
        for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
                sb.Append(pBanArr[X, Y]);
            }
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }
}


解説

DFSでナイーブに敷き詰めてます。