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

ABC307-C Ideal Sheet


問題へのリンク


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

    static char[,] mBanArrA;
    static char[,] mBanArrB;
    static char[,] mBanArrC;

    static int UB_A_X;
    static int UB_A_Y;
    static int UB_B_X;
    static int UB_B_Y;
    static int UB_C_X;
    static int UB_C_Y;

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        SplitAct(InputList[0]);
        int A_H = wkArr[0];

        SplitAct(InputList[A_H + 1]);
        int B_H = wkArr[0];

        mBanArrA = CreateBanArr(InputList.Skip(1).Take(A_H));
        mBanArrB = CreateBanArr(InputList.Skip(1 + A_H + 1).Take(B_H));
        mBanArrC = CreateBanArr(InputList.Skip(1 + A_H + 1 + B_H + 1));

        UB_A_X = mBanArrA.GetUpperBound(0);
        UB_A_Y = mBanArrA.GetUpperBound(1);
        UB_B_X = mBanArrB.GetUpperBound(0);
        UB_B_Y = mBanArrB.GetUpperBound(1);
        UB_C_X = mBanArrC.GetUpperBound(0);
        UB_C_Y = mBanArrC.GetUpperBound(1);

        List<VectDef> VectListA = DeriveVectList(mBanArrA);
        List<VectDef> VectListB = DeriveVectList(mBanArrB);

        // Aの1個目の#ごとの、Cの#と
        // Bの1個目の#ごとの、Cの#の
        // マッピングを全探索

        // Cの#の座標のList
        var CPointlist = new List<PointDef>();
        for (int Y = 0; Y <= UB_C_Y; Y++) {
            for (int X = 0; X <= UB_C_X; X++) {
                if (mBanArrC[X, Y] != '#') continue;
                PointDef WillAdd;
                WillAdd.X = X;
                WillAdd.Y = Y;
                CPointlist.Add(WillAdd);
            }
        }

        int ACnt = mBanArrA.Cast<char>().Count(pX => pX == '#'); // Aの#の数
        int BCnt = mBanArrB.Cast<char>().Count(pX => pX == '#'); // Bの#の数
        int CCnt = mBanArrC.Cast<char>().Count(pX => pX == '#'); // Cの#の数

        for (int I = 0; I <= CPointlist.Count - 1; I++) {
            var MatchedPosSet1 = new HashSet<string>();

            // 必要条件1 Aの#を全て含むこと
            foreach (VectDef EachVect in VectListA) {
                int Curr_C_X = CPointlist[I].X + EachVect.X;
                int Curr_C_Y = CPointlist[I].Y + EachVect.Y;

                if (Curr_C_X < 0 || UB_C_X < Curr_C_X) break;
                if (Curr_C_Y < 0 || UB_C_Y < Curr_C_Y) break;

                if (mBanArrC[Curr_C_X, Curr_C_Y] == '#') {
                    MatchedPosSet1.Add(string.Format("{0},{1}", Curr_C_X, Curr_C_Y));
                }
            }

            if (MatchedPosSet1.Count != ACnt) {
                continue;
            }

            for (int J = 0; J <= CPointlist.Count - 1; J++) {
                var MatchedPosSet2 = new HashSet<string>();

                // 必要条件2 Bの#を全て含むこと
                foreach (VectDef EachVect in VectListB) {
                    int Curr_C_X = CPointlist[J].X + EachVect.X;
                    int Curr_C_Y = CPointlist[J].Y + EachVect.Y;

                    if (Curr_C_X < 0 || UB_C_X < Curr_C_X) break;
                    if (Curr_C_Y < 0 || UB_C_Y < Curr_C_Y) break;

                    if (mBanArrC[Curr_C_X, Curr_C_Y] == '#') {
                        MatchedPosSet2.Add(string.Format("{0},{1}", Curr_C_X, Curr_C_Y));
                    }
                }
                if (MatchedPosSet2.Count != BCnt) {
                    continue;
                }

                // 必要条件3 Cの#は、全てAまたはBの#であること
                MatchedPosSet2.UnionWith(MatchedPosSet1);
                if (MatchedPosSet2.Count == CCnt) {
                    Console.WriteLine("Yes");
                    return;
                }
            }
        }
        Console.WriteLine("No");
    }

    struct PointDef
    {
        internal int X;
        internal int Y;
    }

    // ベクトルの定義
    struct VectDef
    {
        internal int X;
        internal int Y;
    }

    // シートの盤面を引数とし、最初の#の位置からそれぞれの#までのベクトルのListを返す
    static List<VectDef> DeriveVectList(char[,] pBanArr)
    {
        int Base_X = -1;
        int Base_Y = -1;
        bool Found = false;

        var WillReturn = new List<VectDef>();

        for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
                if (pBanArr[X, Y] != '#') continue;

                if (Found == false) {
                    Found = true;
                    Base_X = X;
                    Base_Y = Y;
                }
                VectDef WillAdd;
                WillAdd.X = X - Base_X;
                WillAdd.Y = Y - Base_Y;
                WillReturn.Add(WillAdd);
            }
        }
        return WillReturn;
    }

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


解説

3つの必要条件に分けて考えてます。