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

A17 Dungeon 2


問題へのリンク


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");
            WillReturn.Add("2 4 1 3");
            WillReturn.Add("5 3 3");
            //4
            //1 2 4 5
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10");
            WillReturn.Add("1 19 75 37 17 16 33 18 22");
            WillReturn.Add("41 28 89 74 98 43 42 31");
            //7
            //1 2 4 5 6 8 10
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        int N = int.Parse(InputList[0]);
        int UB = N - 1;
        int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int[] BArr = InputList[2].Split(' ').Select(pX => int.Parse(pX)).ToArray();

        // 最小コスト[現在の部屋]なDP表
        long?[] DPArr = new long?[UB + 1];
        DPArr[0] = 0;

        for (int I = 0; I <= UB - 1; I++) {
            Action<long, long> UpdateAct = (pNewI, pNewCost) =>
            {
                if (pNewI > UB) return;

                if (DPArr[pNewI].HasValue) {
                    if (DPArr[pNewI] <= pNewCost) {
                        return;
                    }
                }
                DPArr[pNewI] = pNewCost;
            };

            if (AArr.GetUpperBound(0) >= I) {
                UpdateAct(I + 1, DPArr[I].Value + AArr[I]);
            }
            if (BArr.GetUpperBound(0) >= I) {
                UpdateAct(I + 2, DPArr[I].Value + BArr[I]);
            }
        }

        // DPの復元処理
        int CurrInd = UB;

        var AnswerList = new List<int>();
        while (true) {
            AnswerList.Add(CurrInd);
            if (CurrInd == 0) break;

            Func<int, bool> BackAct = (pNewInd) =>
            {
                if (pNewInd < 0) return false;

                long CorrectCost = DPArr[pNewInd].Value;
                int IndDiff = Math.Abs(CurrInd - pNewInd);

                long NewCost = DPArr[CurrInd].Value;
                if (IndDiff == 1) {
                    NewCost -= AArr[pNewInd];
                }
                else {
                    NewCost -= BArr[pNewInd];
                }

                if (NewCost == CorrectCost) {
                    CurrInd = pNewInd;
                    return true;
                }
                return false;
            };

            if (BackAct(CurrInd - 1)) {
                continue;
            }
            if (BackAct(CurrInd - 2)) {
                continue;
            }
        }

        AnswerList.Reverse();
        for (int I = 0; I <= AnswerList.Count - 1; I++) {
            AnswerList[I]++;
        }
        Console.WriteLine(AnswerList.Count);
        Console.WriteLine(IntEnumJoin(" ", AnswerList));
    }

    // セパレータとInt型の列挙を引数として、結合したstringを返す
    static string IntEnumJoin(string pSeparater, IEnumerable<int> pEnum)
    {
        string[] StrArr = Array.ConvertAll(pEnum.ToArray(), pX => pX.ToString());
        return string.Join(pSeparater, StrArr);
    }
}


解説

DP復元を行ってます。