AtCoderの企業コンテスト    次の企業コンテストの問題へ    前の企業コンテストの問題へ

エイシング プログラミング コンテスト 2019 C Alternating Path


問題へのリンク


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

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

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

        // 訪問済の白マスの座標のHash値のSet
        var VisitedWhitePosSet = new HashSet<int>();

        long Answer = 0;
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (mBanArr[LoopX, LoopY] != '.') continue;

                int Hash = GetHash(LoopX, LoopY);
                if (VisitedWhitePosSet.Contains(Hash)) continue;

                List<JyoutaiDef> JyoutaiList = ExecDFS(LoopX, LoopY);

                long WhiteCnt = 0;
                long BlackCnt = 0;
                foreach (JyoutaiDef EachJyoutai in JyoutaiList) {
                    int CurrX = EachJyoutai.CurrX;
                    int CurrY = EachJyoutai.CurrY;
                    int CurrHash = GetHash(CurrX, CurrY);

                    if (mBanArr[CurrX, CurrY] == '.') {
                        VisitedWhitePosSet.Add(CurrHash);
                        WhiteCnt++;
                    }
                    if (mBanArr[CurrX, CurrY] == '#') {
                        BlackCnt++;
                    }
                }
                // 積の法則と和の法則
                Answer += WhiteCnt * BlackCnt;
            }
        }
        Console.WriteLine(Answer);
    }

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

    // DFSを行い、色[座標のハッシュ値]のListを返す
    static List<JyoutaiDef> ExecDFS(int pStaX, int pStaY)
    {
        var WillReturn = new List<JyoutaiDef>();

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrX = pStaX;
        WillPush.CurrY = pStaY;
        WillPush.Level = 0;
        Stk.Push(WillPush);

        var VisitedSet = new HashSet<int>();
        VisitedSet.Add(GetHash(pStaX, pStaY));

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

            WillReturn.Add(Popped);

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

                WillPush.CurrX = pNewX;
                WillPush.CurrY = pNewY;
                WillPush.Level = Popped.Level + 1;

                if (WillPush.Level % 2 == 1 && mBanArr[pNewX, pNewY] != '#') return;
                if (WillPush.Level % 2 == 0 && mBanArr[pNewX, pNewY] != '.') return;

                int Hash = GetHash(pNewX, pNewY);
                if (VisitedSet.Add(Hash) == false) return;

                Stk.Push(WillPush);
            };

            PushAct(Popped.CurrX, Popped.CurrY - 1);
            PushAct(Popped.CurrX, Popped.CurrY + 1);
            PushAct(Popped.CurrX - 1, Popped.CurrY);
            PushAct(Popped.CurrX + 1, Popped.CurrY);
        }
        return WillReturn;
    }

    static int GetHash(int pX, int pY)
    {
        return pX * 1000 + pY;
    }

    ////////////////////////////////////////////////////////////////
    // 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;
    }
}


解説

白マスからのDFSを繰り返してます。