AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC351-D Grid and Magnet


問題へのリンク


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 5");
            WillReturn.Add(".#...");
            WillReturn.Add(".....");
            WillReturn.Add(".#..#");
            //9
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 3");
            WillReturn.Add("..#");
            WillReturn.Add("#..");
            WillReturn.Add("..#");
            //1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

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

        // 全て磁石と隣接してる場合
        bool AllRinsetuJisyaku = true;
        for (long X = 0; X <= UB_X; X++) {
            for (long Y = 0; Y <= UB_Y; Y++) {
                if (mBanArr[X, Y] == '#') continue;

                if (IsRinsetuJisyaku(X, Y) == false) {
                    AllRinsetuJisyaku = false;
                    long Hash = GetHash(X, Y);
                }
            }
        }

        if (AllRinsetuJisyaku) {
            Console.WriteLine(1);
            return;
        }

        var AnswerList = new List<long>();
        for (long X = 0; X <= UB_X; X++) {
            for (long Y = 0; Y <= UB_Y; Y++) {
                if (mBanArr[X, Y] == '#') continue;

                if (IsRinsetuJisyaku(X, Y)) {
                    continue;
                }

                long Hash = GetHash(X, Y);
                if (mNotStartSet.Contains(Hash)) continue;

                long Result = ExecDFS(X, Y);
                AnswerList.Add(Result);
            }
        }
        Console.WriteLine(AnswerList.Max());
    }

    // DFSの開始座標としない座標
    static HashSet<long> mNotStartSet = new HashSet<long>();

    // DFSを行い、訪問した座標数を返す
    static long ExecDFS(long pStaX, long pStaY)
    {
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrX = pStaX;
        WillPush.CurrY = pStaY;
        Stk.Push(WillPush);

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

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

            if (IsRinsetuJisyaku(Popped.CurrX, Popped.CurrY)) {
                continue;
            }

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

                if (mBanArr[pNewX, pNewY] == '#') return;

                long Hash = GetHash(pNewX, pNewY);
                if (IsRinsetuJisyaku(pNewX, pNewY) == false) {
                    mNotStartSet.Add(Hash);
                }
                if (VisitedSet.Contains(Hash)) return;
                VisitedSet.Add(Hash);
                WillPush.CurrX = pNewX;
                WillPush.CurrY = pNewY;
                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 VisitedSet.Count;
    }

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

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

    // 座標を引数として、磁石と隣接してるかを返す
    static bool IsRinsetuJisyaku(long pX, long pY)
    {
        if (IsJisyaku(pX, pY - 1)) return true;
        if (IsJisyaku(pX, pY + 1)) return true;
        if (IsJisyaku(pX - 1, pY)) return true;
        if (IsJisyaku(pX + 1, pY)) return true;
        return false;
    }

    // 座標を引数として、磁石かを返す
    static bool IsJisyaku(long pX, long pY)
    {
        if (pX < 0 || UB_X < pX) return false;
        if (pY < 0 || UB_Y < pY) return false;

        return mBanArr[pX, 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;
    }
}


解説

座標を引数として、磁石に隣接してるかの判定メソッドを作ってます。

場合分けし、
磁石に隣接してるマスしかなければ、解は1です。
磁石に隣接してないマスがあれば、開始座標を制限するDFSを実行してます。