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

AOJ 0634 尾根 (Ridge)


問題へのリンク


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("2 9 4");
            WillReturn.Add("7 5 3");
            WillReturn.Add("6 1 8");
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 5");
            WillReturn.Add("5 3 8 2 14");
            WillReturn.Add("9 10 4 1 13");
            WillReturn.Add("12 7 11 6 15");
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int[,] mBanArr;
    static int UB_X;
    static int UB_Y;

    struct PosInfoDef
    {
        internal int X;
        internal int Y;
        internal int Height;
    }
    static List<PosInfoDef> mPosInfoList = new List<PosInfoDef>();

    static int[,] mCntArr;

    static void Main()
    {
        List<string> InputList = GetInputList();
        mBanArr = CreateBanArr(InputList.Skip(1));
        UB_X = mBanArr.GetUpperBound(0);
        UB_Y = mBanArr.GetUpperBound(1);

        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                PosInfoDef WillAdd;
                WillAdd.X = X;
                WillAdd.Y = Y;
                WillAdd.Height = mBanArr[X, Y];
                mPosInfoList.Add(WillAdd);
            }
        }

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

        foreach (PosInfoDef EachPosInfo in mPosInfoList) {
            int CurrX = EachPosInfo.X;
            int CurrY = EachPosInfo.Y;
            List<int> HeightList = DeriveHeightList(CurrX, CurrY);
            if (HeightList.TrueForAll(pX => pX > EachPosInfo.Height)) {
                ExecBFS(CurrX, CurrY);
            }
        }

        int Answer = 0;
        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                if (mCntArr[X, Y] >= 2) {
                    Answer++;
                }
            }
        }
        Console.WriteLine(Answer);
    }

    struct JyoutaiDef
    {
        internal int CurrX;
        internal int CurrY;
    }

    static void ExecBFS(int pStaX, int pStaY)
    {
        var Que = new Queue<JyoutaiDef>();
        JyoutaiDef WillEnqueue;
        WillEnqueue.CurrX = pStaX;
        WillEnqueue.CurrY = pStaY;
        Que.Enqueue(WillEnqueue);

        var VisitedSet = new HashSet<int>();
        while (Que.Count > 0) {
            JyoutaiDef Dequeued = Que.Dequeue();

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

                if (mBanArr[Dequeued.CurrX, Dequeued.CurrY] > mBanArr[pNewX, pNewY]) {
                    return;
                }
                int Hash = pNewX * 10000 + pNewY;
                if (VisitedSet.Add(Hash)) {
                    mCntArr[pNewX, pNewY]++;
                    WillEnqueue.CurrX = pNewX;
                    WillEnqueue.CurrY = pNewY;
                    Que.Enqueue(WillEnqueue);
                }
            };
            EnqueueAct(Dequeued.CurrX, Dequeued.CurrY - 1);
            EnqueueAct(Dequeued.CurrX, Dequeued.CurrY + 1);
            EnqueueAct(Dequeued.CurrX - 1, Dequeued.CurrY);
            EnqueueAct(Dequeued.CurrX + 1, Dequeued.CurrY);
        }
    }

    // 座標を引数として、4近傍の高さのListを返す
    static List<int> DeriveHeightList(int pX, int pY)
    {
        var WillReturn = new List<int>();

        Action<int, int> AddAct = (pTargetX, pTargetY) =>
        {
            if (pTargetX < 0 || UB_X < pTargetX) return;
            if (pTargetY < 0 || UB_Y < pTargetY) return;

            WillReturn.Add(mBanArr[pTargetX, pTargetY]);
        };

        AddAct(pX, pY - 1);
        AddAct(pX, pY + 1);
        AddAct(pX - 1, pY);
        AddAct(pX + 1, pY);
        return WillReturn;
    }

    ////////////////////////////////////////////////////////////////
    // 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.Split(' ').Select(pX => int.Parse(pX)).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;
    }
}


解説

4近傍の高さが全て自分超えの座標を
BFSの始点とし、マスごとの訪問数を集計してます。