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

第4回PAST H マス目のカット


問題へのリンク


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 4 3");
            WillReturn.Add("1123");
            WillReturn.Add("1214");
            WillReturn.Add("1810");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("8 6 40");
            WillReturn.Add("846444");
            WillReturn.Add("790187");
            WillReturn.Add("264253");
            WillReturn.Add("967004");
            WillReturn.Add("578258");
            WillReturn.Add("204367");
            WillReturn.Add("681998");
            WillReturn.Add("034243");
            //6
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

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

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

        int Answer = -1;
        for (int LoopN = 1; LoopN <= Math.Max(UB_X + 1, UB_Y + 1); LoopN++) {
            for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
                for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                    if (CanCreateSquare(LoopN, LoopX, LoopY)) {
                        Answer = LoopN;
                    }
                }
            }
        }
        Console.WriteLine(Answer);
    }

    // 正方形の開始座標を引数として、
    // K個以下の書き換えで数字をユニークにできるかを返す
    static bool CanCreateSquare(int pN, int pBaseX, int pBaseY)
    {
        if (UB_X < pBaseX + pN - 1) return false;
        if (UB_Y < pBaseY + pN - 1) return false;

        var NumList = new List<int>();
        for (int LoopX = pBaseX; LoopX <= pBaseX + pN - 1; LoopX++) {
            for (int LoopY = pBaseY; LoopY <= pBaseY + pN - 1; LoopY++) {
                NumList.Add(mBanArr[LoopX, LoopY]);
            }
        }
        var CntDict = new Dictionary<int, int>();
        var Query = NumList.GroupBy(pX => pX);
        foreach (var EachItem in Query) {
            CntDict[EachItem.Key] = EachItem.Count();
        }
        int MaxNum = CntDict.OrderByDescending(pX => pX.Value).First().Key;

        NumList.RemoveAll(pX => pX == MaxNum);

        return NumList.Count <= mK;
    }

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

        int[] IntArr = { };
        Action<string> SplitAct = pStr =>
            IntArr = pStr.ToCharArray().Select(pX => pX - '0').ToArray();

        SplitAct(StrList[0]);

        int UB_X = IntArr.GetUpperBound(0);
        int UB_Y = StrList.Count - 1;

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

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


解説

制約が緩いのでナイーブに解いてます。