典型90問    次の典型90問へ    前の典型90問へ

典型90問 063 Monochromatic Subgrid(★4)


問題へのリンク


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

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

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

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

        // Y座標の全ての組み合わせを列挙
        List<JyoutaiDef> ResultList = ExecDFS();

        Console.WriteLine(ResultList.Max(pX => DeriveMaxScore(pX.SelectedIndList)));
    }

    // Y座標の組み合わせを引数として、最大スコアを返す
    static int DeriveMaxScore(List<int> pSelectedIndList)
    {
        // 件数[マスの値]な度数分布表
        var CntDict = new Dictionary<int, int>();

        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            var AppearValSet = new HashSet<int>();
            foreach (int EachY in pSelectedIndList) {
                AppearValSet.Add(mBanArr[LoopX, EachY]);
            }
            if (AppearValSet.Count == 1) {
                int AppearVal = AppearValSet.First();
                if (CntDict.ContainsKey(AppearVal) == false) {
                    CntDict[AppearVal] = 0;
                }
                CntDict[AppearVal]++;
            }
        }
        if (CntDict.Count == 0) return 0;
        return CntDict.Values.Max() * pSelectedIndList.Count;
    }

    struct JyoutaiDef
    {
        internal int CurrInd;
        internal List<int> SelectedIndList;
    }

    static List<JyoutaiDef> ExecDFS()
    {
        var WillReturn = new List<JyoutaiDef>();

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;

        for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
            WillPush.CurrInd = LoopY + 1;
            WillPush.SelectedIndList = new List<int>();
            WillPush.SelectedIndList.Add(LoopY);
            Stk.Push(WillPush);
        }

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

            for (int LoopY = Popped.CurrInd; LoopY <= UB_Y; LoopY++) {
                WillPush.CurrInd = LoopY + 1;
                WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList);
                WillPush.SelectedIndList.Add(LoopY);
                Stk.Push(WillPush);
            }
        }
        return WillReturn;
    }

    ////////////////////////////////////////////////////////////////
    // 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.Split(' ').Select(pX => int.Parse(pX)).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;
    }
}


解説

グリッドの高さ上限が8なので
Y座標の選択有無を全探索してます。