AtCoderのAGC    次のAGCの問題へ    前のAGCの問題へ

AGC033-A Darker and Darker


問題へのリンク


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

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

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

        char[,] BanArr = CreateBanArr(InputList.Skip(1).ToList());
        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        var Que = new Queue<JyoutaiDef>();
        JyoutaiDef WillEnqueue;
        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                if (BanArr[X, Y] == '#') {
                    WillEnqueue.CurrX = X;
                    WillEnqueue.CurrY = Y;
                    WillEnqueue.Level = 0;
                    Que.Enqueue(WillEnqueue);
                }
            }
        }

        int Answer = int.MinValue;
        while (Que.Count > 0) {
            JyoutaiDef Dequeued = Que.Dequeue();

            Answer = Math.Max(Answer, Dequeued.Level);

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

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

                BanArr[pNewX, pNewY] = '#';
                WillEnqueue.CurrX = pNewX;
                WillEnqueue.CurrY = pNewY;
                WillEnqueue.Level = Dequeued.Level + 1;
                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);
        }
        Console.WriteLine(Answer);
    }

    ////////////////////////////////////////////////////////////////
    // stringのListをcharの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static char[,] CreateBanArr(List<string> pStringList)
    {
        if (pStringList.Count == 0) {
            return new char[0, 0];
        }
        int UB_X = pStringList[0].Length - 1;
        int UB_Y = pStringList.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] = pStringList[Y][X];
            }
        }
        return WillReturn;
    }

    ////////////////////////////////////////////////////////////////
    // 2次元配列(char型)のデバッグ出力
    ////////////////////////////////////////////////////////////////
    static void PrintBan(char[,] pBanArr)
    {
        for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
                Console.Write(pBanArr[X, Y]);
            }
            Console.WriteLine();
        }
    }
}


解説

#の位置から幅優先探索して
レベルの最大値が解になります。