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

ABC211-E Red Polyomino


問題へのリンク


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

    static int UB;

    struct JyoutaiDef
    {
        internal char[,] CurrBanArr;
        internal int Level;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int K = int.Parse(InputList[1]);
        char[,] BanArr = CreateBanArr(InputList.Skip(2));
        UB = BanArr.GetUpperBound(0);

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;

        for (int LoopX = 0; LoopX <= UB; LoopX++) {
            for (int LoopY = 0; LoopY <= UB; LoopY++) {
                if (BanArr[LoopX, LoopY] == '.') {
                    WillPush.CurrBanArr = (char[,])BanArr.Clone();
                    WillPush.CurrBanArr[LoopX, LoopY] = '0';
                    WillPush.Level = 1;
                    Stk.Push(WillPush);
                }
            }
        }

        var VisitedSet = new HashSet<ulong>();

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

            // クリア判定
            if (Popped.Level == K) {
                Answer++;
                continue;
            }

            for (int LoopX = 0; LoopX <= UB; LoopX++) {
                for (int LoopY = 0; LoopY <= UB; LoopY++) {
                    if (Popped.CurrBanArr[LoopX, LoopY] != '0') {
                        continue;
                    }

                    Action<int, int> PushAct = (pTargetX, pTargetY) =>
                    {
                        if (pTargetX < 0 || UB < pTargetX) return;
                        if (pTargetY < 0 || UB < pTargetY) return;

                        if (Popped.CurrBanArr[pTargetX, pTargetY] != '.') {
                            return;
                        }
                        WillPush.CurrBanArr = (char[,])Popped.CurrBanArr.Clone();
                        WillPush.CurrBanArr[pTargetX, pTargetY] = '0';
                        ulong Hash = GetHash(WillPush.CurrBanArr);
                        if (VisitedSet.Add(Hash)) {
                            WillPush.Level = Popped.Level + 1;
                            Stk.Push(WillPush);
                        }
                    };

                    PushAct(LoopX, LoopY - 1);
                    PushAct(LoopX, LoopY + 1);
                    PushAct(LoopX - 1, LoopY);
                    PushAct(LoopX + 1, LoopY);
                }
            }
        }
        Console.WriteLine(Answer);
    }

    static ulong GetHash(char[,] pBanArr)
    {
        var sb = new System.Text.StringBuilder();
        for (int X = 0; X <= UB; X++) {
            for (int Y = 0; Y <= UB; Y++) {
                sb.Append(pBanArr[X, Y] == '.' ? 1 : 0);
            }
        }
        return Convert.ToUInt64(sb.ToString(), 2);
    }

    ////////////////////////////////////////////////////////////////
    // 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で列挙してます。