DPコンテスト    次のDPコンテストの問題へ    前のDPコンテストの問題へ

Educational DP Contest H Grid 1


問題へのリンク


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

    const long Hou = 1000000007;

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

        char[,] BanArr = CreateBanArr(InputList.Skip(1));
        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        long[,] DPArr = new long[UB_X + 1, UB_Y + 1];
        DPArr[0, 0] = 1;

        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                if (BanArr[X, Y] == '#') continue;

                Action<int, int> UpdateAct = (pNewX, pNewY) =>
                {
                    if (pNewX < 0 || UB_X < pNewX) return;
                    if (pNewY < 0 || UB_Y < pNewY) return;

                    if (BanArr[pNewX, pNewY] == '#') return;

                    // 配るDP
                    DPArr[pNewX, pNewY] += DPArr[X, Y];
                    DPArr[pNewX, pNewY] %= Hou;
                };

                UpdateAct(X, Y + 1);
                UpdateAct(X + 1, Y);
            }
        }
        Console.WriteLine(DPArr[UB_X, UB_Y]);
    }

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


解説

配るDPで、場合の数の、和の法則を使ってます。