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

AOJ 1160 島はいくつある?


問題へのリンク


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("1 1");
            WillReturn.Add("0");
            WillReturn.Add("2 2");
            WillReturn.Add("0 1");
            WillReturn.Add("1 0");
            WillReturn.Add("3 2");
            WillReturn.Add("1 1 1");
            WillReturn.Add("1 1 1");
            WillReturn.Add("5 4");
            WillReturn.Add("1 0 1 0 0");
            WillReturn.Add("1 0 0 0 0");
            WillReturn.Add("1 0 1 0 1");
            WillReturn.Add("1 0 0 1 0");
            WillReturn.Add("5 4");
            WillReturn.Add("1 1 1 0 1");
            WillReturn.Add("1 0 1 0 1");
            WillReturn.Add("1 0 1 0 1");
            WillReturn.Add("1 0 1 1 1");
            WillReturn.Add("5 5");
            WillReturn.Add("1 0 1 0 1");
            WillReturn.Add("0 0 0 0 0");
            WillReturn.Add("1 0 1 0 1");
            WillReturn.Add("0 0 0 0 0");
            WillReturn.Add("1 0 1 0 1");
            WillReturn.Add("0 0");
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        int CurrInd = 0;
        while (true) {
            SplitAct(InputList[CurrInd]);
            int W = wkArr[0];
            int H = wkArr[1];

            if (W == 0 && H == 0) break;

            int[,] BanArr = CreateBanArr(InputList.Skip(CurrInd + 1).Take(H));
            Solve(BanArr);

            CurrInd += H + 1;
        }
    }

    static int UB_X;
    static int UB_Y;

    static void Solve(int[,] pBanArr)
    {
        UB_X = pBanArr.GetUpperBound(0);
        UB_Y = pBanArr.GetUpperBound(1);

        int ShimaCnt = 0;
        var VisitedSet = new HashSet<int>();
        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                if (pBanArr[X, Y] == 0) continue;

                int Hash = GetHash(X, Y);
                if (VisitedSet.Contains(Hash)) continue;

                ShimaCnt++;
                HashSet<int> Result = ExecDFS(X, Y, pBanArr);

                VisitedSet.UnionWith(Result);
            }
        }
        Console.WriteLine(ShimaCnt);
    }

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

    static HashSet<int> ExecDFS(int pStaX, int pStaY, int[,] pBanArr)
    {
        var Result = new HashSet<int>();

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

        Result.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] == 0) return;

                int Hash = GetHash(pNewX, pNewY);
                if (Result.Add(Hash)) {
                    WillPush.CurrX = pNewX;
                    WillPush.CurrY = pNewY;
                    Stk.Push(WillPush);
                }
            };

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

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

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

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

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

    ////////////////////////////////////////////////////////////////
    // 2次元配列(int型)のデバッグ出力
    ////////////////////////////////////////////////////////////////
    static void PrintBan(int[,] pBanArr)
    {
        var sb = new System.Text.StringBuilder();
        for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
                sb.Append(pBanArr[X, Y]);
            }
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }
}


解説

再訪防止のDFSで解いてます。