AtCoderのPAST    次のPASTの問題へ    前のPASTの問題へ

第5回PAST E ハンコ


問題へのリンク


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("3 3");
            WillReturn.Add("...");
            WillReturn.Add(".#.");
            WillReturn.Add("..#");
            WillReturn.Add("#.#");
            WillReturn.Add("###");
            WillReturn.Add("...");
            //Yes
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 3");
            WillReturn.Add("...");
            WillReturn.Add("#..");
            WillReturn.Add("#.#");
            WillReturn.Add(".#.");
            WillReturn.Add(".##");
            WillReturn.Add("##.");
            //No
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("2 5");
            WillReturn.Add(".....");
            WillReturn.Add("..#..");
            WillReturn.Add("..##.");
            WillReturn.Add(".###.");
            //Yes
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static char[,] mBanArr;
    static int UB_X;
    static int UB_Y;

    static char[,] mStampArr;

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int H = wkArr[0];

        mBanArr = CreateBanArr(InputList.Skip(1).Take(H));
        UB_X = mBanArr.GetUpperBound(0);
        UB_Y = mBanArr.GetUpperBound(1);

        mStampArr = CreateBanArr(InputList.Skip(1 + H));
        mStampArr = ArrSyukusyou(mStampArr);

        if (DeriveCanStamp()) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }

    // ハンコを押せるかの判定
    static bool DeriveCanStamp()
    {
        List<char[,]> KaitenArrList = DeriveKaitenArrList(mStampArr);

        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                foreach (char[,] EachKaitenArr in KaitenArrList) {
                    if (DeriveCanStampHelper(EachKaitenArr, LoopX, LoopY, mBanArr)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    // マス目にハンコを押せるか
    static bool DeriveCanStampHelper(char[,] pStampMap, int pTargetX, int pTargetY, char[,] pBanArr)
    {
        if (pTargetX + pStampMap.GetUpperBound(0) > UB_X) return false;
        if (pTargetY + pStampMap.GetUpperBound(1) > UB_Y) return false;

        for (int X = 0; X <= pStampMap.GetUpperBound(0); X++) {
            for (int Y = 0; Y <= pStampMap.GetUpperBound(1); Y++) {
                if (pStampMap[X, Y] == '#' && pBanArr[pTargetX + X, pTargetY + Y] == '#')
                    return false;
            }
        }
        return true;
    }

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

        //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 char[BaseArrUB_X + 1, BaseArrUB_Y + 1];
        for (int P = 1; P <= 7; 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;

                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;
            }
        }
        // 対称移動は不可なので、Remove
        KaitenArrList.RemoveRange(4, 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<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;
    }


    // 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)
    {
        for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
                Console.Write(pBanArr[X, Y]);
            }
            Console.WriteLine();
        }
    }
}


解説

始点座標を全探索して、
ハンコを回転させて敷き詰め可能かの判定を行ってます。