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

AOJ 2272 蝉


問題へのリンク


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 3");
            WillReturn.Add("023");
            WillReturn.Add("321");
            WillReturn.Add("120");
            //5
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 6");
            WillReturn.Add("000000");
            WillReturn.Add("923450");
            WillReturn.Add("000000");
            WillReturn.Add("054329");
            WillReturn.Add("000000");
            //4
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("6 9");
            WillReturn.Add("089712364");
            WillReturn.Add("781263576");
            WillReturn.Add("937526351");
            WillReturn.Add("736589623");
            WillReturn.Add("217835653");
            WillReturn.Add("837479570");
            //46
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[,] BanArr = CreateBanArr(InputList.Skip(1));
        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.Curr_X = 0;
        WillPush.Curr_Y = 0;
        WillPush.CostSum = 0;
        Stk.Push(WillPush);

        // 最小スコア[座標のハッシュ値]
        var EdakiriDict = new Dictionary<string, int>();

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

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

                int NewCost = Popped.CostSum + BanArr[pNewX, pNewY];

                string Hash = string.Format("{0},{1}", pNewX, pNewY);
                if (EdakiriDict.ContainsKey(Hash)) {
                    if (NewCost >= EdakiriDict[Hash]) {
                        return;
                    }
                }
                EdakiriDict[Hash] = NewCost;
                WillPush.Curr_X = pNewX;
                WillPush.Curr_Y = pNewY;
                WillPush.CostSum = NewCost;
                Stk.Push(WillPush);
            };

            PushAct(Popped.Curr_X, Popped.Curr_Y + 1);
            PushAct(Popped.Curr_X + 1, Popped.Curr_Y);
        }
        string GoalHash = string.Format("{0},{1}", UB_X, UB_Y);
        Console.WriteLine(EdakiriDict[GoalHash]);
    }

    struct JyoutaiDef
    {
        internal int Curr_X;
        internal int Curr_Y;
        internal int CostSum;
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をintの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static int[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new int[0, 0];
        }

        int[] IntArr = { };
        Action<string> SplitAct = pStr => IntArr = pStr.Select(pX => pX - '0').ToArray();

        SplitAct(StrList[0]);

        int UB_X = IntArr.GetUpperBound(0);
        int UB_Y = StrList.Count - 1;

        int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            SplitAct(StrList[Y]);
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = IntArr[X];
            }
        }
        return WillReturn;
    }

    ////////////////////////////////////////////////////////////////
    // 2次元配列(int型)のデバッグ出力
    ////////////////////////////////////////////////////////////////
    static void PrintBan(int[,] pBanArr)
    {
        var sb = new System.Text.StringBuilder();
        for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
                sb.AppendFormat("{0},", pBanArr[X, Y]);
            }
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }
}


解説

ナイーブにDFSしてます。