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

AOJ 0612 砂の城


問題へのリンク


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("5 6");
            WillReturn.Add("......");
            WillReturn.Add(".939..");
            WillReturn.Add(".3428.");
            WillReturn.Add(".9393.");
            WillReturn.Add("......");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10 10");
            WillReturn.Add("..........");
            WillReturn.Add(".99999999.");
            WillReturn.Add(".9.323239.");
            WillReturn.Add(".91444449.");
            WillReturn.Add(".91444449.");
            WillReturn.Add(".91444449.");
            WillReturn.Add(".91444449.");
            WillReturn.Add(".91232329.");
            WillReturn.Add(".99999999.");
            WillReturn.Add("..........");
            //35
        }
        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 int[,] mCntArr;

    // 8近傍に既に配った座標のset
    static HashSet<int> mSendPosSet = new HashSet<int>();

    // 新しく崩れるマスの座標のList
    static List<int> mNewDestroyList = new List<int>();

    // 既に崩れた座標のset
    static HashSet<int> mDestroySet = new HashSet<int>();

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

        mCntArr = new int[UB_X + 1, UB_Y + 1];

        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                if (mBanArr[X, Y] == '.') {
                    int Hash = GetHash(X, Y);
                    if (mSendPosSet.Add(Hash) == false) continue;

                    ExecAdd(X - 1, Y - 1);
                    ExecAdd(X - 1, Y);
                    ExecAdd(X - 1, Y + 1);

                    ExecAdd(X, Y - 1);
                    ExecAdd(X, Y + 1);

                    ExecAdd(X + 1, Y - 1);
                    ExecAdd(X + 1, Y);
                    ExecAdd(X + 1, Y + 1);
                }
            }
        }

        int Answer = 0;
        while (mNewDestroyList.Count > 0) {
            Answer++;
            int[] DestroyArr = mNewDestroyList.ToArray();
            mNewDestroyList.Clear();

            foreach (int EachHash in DestroyArr) {
                int CurrX = EachHash / 10000;
                int CurrY = EachHash % 10000;

                int Hash = GetHash(CurrX, CurrY);
                if (mSendPosSet.Add(Hash) == false) continue;

                ExecAdd(CurrX - 1, CurrY - 1);
                ExecAdd(CurrX - 1, CurrY);
                ExecAdd(CurrX - 1, CurrY + 1);

                ExecAdd(CurrX, CurrY - 1);
                ExecAdd(CurrX, CurrY + 1);

                ExecAdd(CurrX + 1, CurrY - 1);
                ExecAdd(CurrX + 1, CurrY);
                ExecAdd(CurrX + 1, CurrY + 1);
            }
        }
        Console.WriteLine(Answer);
    }

    // 指定座標に加算を行う
    static void ExecAdd(int pBaseX, int pBaseY)
    {
        if (pBaseX < 0 || UB_X < pBaseX) return;
        if (pBaseY < 0 || UB_Y < pBaseY) return;

        mCntArr[pBaseX, pBaseY]++;

        // 新しく崩れる場合
        int CurrCnt = mCntArr[pBaseX, pBaseY];
        if (mBanArr[pBaseX, pBaseY] != '.') {
            int CharNum = mBanArr[pBaseX, pBaseY] - '0';
            if (CharNum <= CurrCnt) {
                int Hash = GetHash(pBaseX, pBaseY);
                if (mDestroySet.Add(Hash)) {
                    mNewDestroyList.Add(Hash);
                }
            }
        }
    }

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

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


解説

崩れた座標を、差分更新してます。