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

A25 Number of Routes


問題へのリンク


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 8");
            WillReturn.Add(".....#..");
            WillReturn.Add("........");
            WillReturn.Add("..#...#.");
            WillReturn.Add("#.......");
            //35
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 8");
            WillReturn.Add("....#...");
            WillReturn.Add("....#...");
            //0
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("30 30");
            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("..............................");
            WillReturn.Add("..............................");
            WillReturn.Add("..............................");
            WillReturn.Add("..............................");
            WillReturn.Add("..............................");
            WillReturn.Add("..............................");
            WillReturn.Add("..............................");
            WillReturn.Add("..............................");
            WillReturn.Add("..............................");
            WillReturn.Add("..............................");
            WillReturn.Add("..............................");
            //30067266499541040
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    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;

        // 配るDP
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (DPArr[LoopX, LoopY] == 0) continue;

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

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

                    DPArr[pNewX, pNewY] += DPArr[LoopX, LoopY];
                };
                SendAct(LoopX, LoopY + 1);
                SendAct(LoopX + 1, LoopY);
            }
        }
        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で解けます。