AtCoderのPAST    次のPASTの問題へ    前のPASTの問題へ

第4回PAST G 村整備


問題へのリンク


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("3 3");
            WillReturn.Add("##.");
            WillReturn.Add("##.");
            WillReturn.Add("...");
            //3
        }
        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);

        int StaX = -1, StaY = -1;
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (mBanArr[LoopX, LoopY] == '.') {
                    StaX = LoopX;
                    StaY = LoopY;
                }
            }
        }

        // 初期状態の壁以外のマスの数
        int NonWallCnt = mBanArr.Cast<char>().Count(pX => pX == '.');

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

                    char[,] CloneBanArr = (char[,])mBanArr.Clone();
                    CloneBanArr[LoopX, LoopY] = '.';

                    if (ExecDFS(CloneBanArr, LoopX, LoopY) == NonWallCnt + 1) {
                        Answer++;
                    }
                }
            }
        }
        Console.WriteLine(Answer);
    }

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

    // 開始地点から移動可能なマスの数を返す
    static int ExecDFS(char[,] pBanArr, int pStaX, int pStaY)
    {
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrX = pStaX;
        WillPush.CurrY = pStaY;
        Stk.Push(WillPush);

        var VisitedSet = new HashSet<int>();
        VisitedSet.Add(GetHash(pStaX, pStaY));
        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();

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

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

                int Hash = GetHash(pNewX, pNewY);
                if (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;
    }

    static int GetHash(int pX, int pY)
    {
        return pX * 100 + 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で島が1つしかないかを判定してます。