AOJ本の読書メモ   AOJ    次のAOJの問題へ    前のAOJの問題へ

AOJ 0515 通学経路 (School Road)


問題へのリンク


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("5 4");
            WillReturn.Add("3");
            WillReturn.Add("2 2");
            WillReturn.Add("2 3");
            WillReturn.Add("4 2");
            WillReturn.Add("5 4");
            WillReturn.Add("3");
            WillReturn.Add("2 2");
            WillReturn.Add("2 3");
            WillReturn.Add("4 2");
            WillReturn.Add("0 0");
            //5
            //5
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        int CurrInd = 0;
        while (true) {
            SplitAct(InputList[CurrInd]);
            int Width = wkArr[0];
            int Height = wkArr[1];
            if (Width == 0 && Height == 0) break;

            CurrInd++;
            int N = int.Parse(InputList[CurrInd]);

            string[] InputArr = InputList.Skip(CurrInd + 1).Take(N).ToArray();
            Solve(Width, Height, InputArr);

            CurrInd += 1 + N;
        }
    }

    static void Solve(int pWidth, int pHeight, string[] pInputArr)
    {
        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        var KoujiPosSet = new HashSet<int>();
        foreach (string EachStr in pInputArr) {
            SplitAct(EachStr);
            int KoujiX = wkArr[0];
            int KoujiY = wkArr[1];
            KoujiPosSet.Add(GetHash(KoujiX, KoujiY));
        }

        // 場合の数[X座標,Y座標]なインラインDP表
        int[,] DPArr = new int[pWidth + 1, pHeight + 1];
        DPArr[1, 1] = 1;

        for (int X = 1; X <= pWidth; X++) {
            for (int Y = 1; Y <= pHeight; Y++) {
                int CurrHash = GetHash(X, Y);
                if (KoujiPosSet.Contains(CurrHash)) continue;

                Action<int, int> SendAct = (pSendX, pSendY) =>
                {
                    if (pWidth < pSendX) return;
                    if (pHeight < pSendY) return;

                    int SendHash = GetHash(pSendX, pSendY);
                    if (KoujiPosSet.Contains(SendHash)) return;

                    DPArr[pSendX, pSendY] += DPArr[X, Y];
                };

                SendAct(X, Y + 1);
                SendAct(X + 1, Y);
            }
        }

        Console.WriteLine(DPArr[pWidth, pHeight]);
    }

    // 座標のハッシュ値を返す
    static int GetHash(int pX, int pY)
    {
        return pX * 100 + pY;
    }
}


解説

場合の数[X座標,Y座標]を更新するインラインDPで解いてます。