競技プログラミングの鉄則    次の問題へ    前の問題へ

A72 Tile Painting


問題へのリンク


C#のソース

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

class Program
{
    static string InputPattern = "Input1";

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

        if (InputPattern == "Input1") {
            WillReturn.Add("4 10 3");
            WillReturn.Add("##...#.##.");
            WillReturn.Add(".#....#...");
            WillReturn.Add("##.####..#");
            WillReturn.Add("#..######.");
            //37
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int mK;
    static int UB_X;
    static int UB_Y;

    static char[,] mBanArr;

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

        var YIndList = new List<int>();
        for (int I = 0; I <= UB_Y; I++) {
            YIndList.Add(I);
        }

        var Debug = SelectPatternClass<int>.SelectPattern(YIndList, 0, mK).ToList();

        var AnswerKouho = new List<int>();
        foreach (int[] EachSelectedArr in SelectPatternClass<int>.SelectPattern(YIndList, 0, mK)) {
            AnswerKouho.Add(DeriveAnswerKouho(EachSelectedArr));
        }
        Console.WriteLine(AnswerKouho.Max());
    }

    // 選択行の配列を引数として、黒くできるマス数を返す
    static int DeriveAnswerKouho(int[] pSelectedIndArr)
    {
        // X座標ごとの黒マスの数
        var BlackCntList = new List<int>();

        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            int CurrCnt = 0;
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (mBanArr[LoopX, LoopY] == '#') {
                    CurrCnt++;
                }
                else if (Array.IndexOf(pSelectedIndArr, LoopY) >= 0) {
                    CurrCnt++;
                }
            }
            BlackCntList.Add(CurrCnt);
        }

        BlackCntList.Sort();

        int AnswerKouho = 0;
        int RestK = mK - pSelectedIndArr.Length;
        for (int I = 0; I <= BlackCntList.Count - 1; I++) {
            if (RestK > 0) {
                AnswerKouho += UB_Y + 1;
                RestK--;
            }
            else {
                AnswerKouho += BlackCntList[I];
            }
        }
        return AnswerKouho;
    }

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

#region SelectPatternClass
// 列挙、取得数下限、取得数上限を引数とし、配列の列挙を返す
internal static class SelectPatternClass<Type>
{
    private struct JyoutaiDef_SelectPattern
    {
        internal int CurrP;
        internal List<int> SelectedIndList;
    }
    internal static IEnumerable<Type[]> SelectPattern(
        IEnumerable<Type> pEnum, int pTakeCntMin, int pTakeCntMax)
    {
        Type[] pArr = pEnum.ToArray();

        var Stk = new Stack<JyoutaiDef_SelectPattern>();
        JyoutaiDef_SelectPattern WillPush;
        WillPush.SelectedIndList = new List<int>();
        WillPush.CurrP = 0;
        Stk.Push(WillPush);

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

            // クリア判定
            if (pTakeCntMin <= Popped.SelectedIndList.Count &&
                Popped.SelectedIndList.Count <= pTakeCntMax) {
                var WillReturn = new List<Type>();
                Popped.SelectedIndList.ForEach(X => WillReturn.Add(pArr[X]));
                yield return WillReturn.ToArray();

                // 取得数上限の場合
                if (Popped.SelectedIndList.Count == pTakeCntMax) {
                    continue;
                }
            }

            for (int I = pArr.GetUpperBound(0); Popped.CurrP <= I; I--) {
                WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };
                WillPush.CurrP = I + 1;
                Stk.Push(WillPush);
            }
        }
    }
}
#endregion


解説

高さの制約が緩いので、
選択する行の組合せを全て列挙してます。