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

ABC173-C H and V


問題へのリンク


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

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

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

        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        UB_Y = wkArr[0] - 1;
        UB_X = wkArr[1] - 1;
        mK = wkArr[2];

        char[,] BanArr = CreateBanArr(InputList.Skip(1).ToList());

        Solve(BanArr);
    }

    // stringのListをcharの2次元配列に設定する
    static char[,] CreateBanArr(List<string> pStringList)
    {
        if (pStringList.Count == 0) {
            return new char[0, 0];
        }

        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] = pStringList[Y][X];
            }
        }
        return WillReturn;
    }

    // 2次元配列のデバッグ出力
    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();
        }
    }

    struct JyoutaiDef
    {
        internal int CurrInd;
        internal char[,] BanArr;
    }

    static void Solve(char[,] pBanArr)
    {
        JyoutaiDef WillPush;
        WillPush.CurrInd = 0;
        WillPush.BanArr = pBanArr;
        var Stk = new Stack<JyoutaiDef>();
        Stk.Push(WillPush);

        var BanArrList = new List<char[,]>();

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

            // 枝切り
            if (DeriveBlackCnt(Popped.BanArr) < mK) continue;

            BanArrList.Add(Popped.BanArr);

            for (int I = Popped.CurrInd; I <= UB_X + UB_Y + 1; I++) {
                WillPush.CurrInd = I + 1;
                WillPush.BanArr = (char[,])Popped.BanArr.Clone();
                if (I <= UB_X) {
                    PaintTate(Popped.BanArr, I);
                    Stk.Push(WillPush);
                }
                else {
                    PaintYoko(Popped.BanArr, I - UB_X - 1);
                    Stk.Push(WillPush);
                }
            }
        }

        long Answer = 0;
        foreach (char[,] EachBanArr in BanArrList) {
            if (DeriveBlackCnt(EachBanArr) == mK) {
                Answer++;
            }
        }
        Console.WriteLine(Answer);
    }

    //盤面を引数として、横方向に白く塗った盤面を返す
    static void PaintYoko(char[,] pBanArr, int pConstY)
    {
        for (int X = 0; X <= UB_X; X++) {
            pBanArr[X, pConstY] = '.';
        }
    }

    //盤面を引数として、縦方向に白く塗った盤面を返す
    static void PaintTate(char[,] pBanArr, int pConstX)
    {
        for (int Y = 0; Y <= UB_Y; Y++) {
            pBanArr[pConstX, Y] = '.';
        }
    }

    // 黒マスの数を返す
    static int DeriveBlackCnt(char[,] pBanArr)
    {
        int WillReturn = 0;
        for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
                if (pBanArr[X, Y] == '#') {
                    WillReturn++;
                }
            }
        }
        return WillReturn;
    }
}


解説

深さ優先探索で、全ての列と行の選択を列挙してます。