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

ABC196-D Hanjo


問題へのリンク


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("2 2 1 2");
            //4
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 3 4 1");
            //18
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("4 4 8 0");
            //36
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("4 4 0 16");
        }
        else if (InputPattern == "Input5") {
            WillReturn.Add("4 4 8 0");
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int UB_X;
    static int UB_Y;
    static int mA;
    static int mB;

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

        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        UB_Y = wkArr[0] - 1;
        UB_X = wkArr[1] - 1;
        mA = wkArr[2];
        mB = wkArr[3];

        Solve();
    }

    //ピースごとの配置候補
    static Dictionary<char, List<bool[,]>> HaitiKouhoListDict =
        new Dictionary<char, List<bool[,]>>();

    static char[] PieceNameArr = { 'A', 'B' };

    struct JyoutaiDef
    {
        internal char[,] BanArr;
        internal int CurrX;
        internal int CurrY;
        internal Dictionary<char, int> UsePieceDict;
    }

    static void Solve()
    {
        int[,] QuestionArr = new int[UB_X + 1, UB_Y + 1];
        Dictionary<char, int> CanUsePieceDict = new Dictionary<char, int>() { { 'A', mA }, { 'B', mB } };

        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                QuestionArr[X, Y] = 1;
            }
        }

        foreach (char AnyPiece in PieceNameArr) {
            if (mA == 0 && AnyPiece == 'A') continue;
            if (mB == 0 && AnyPiece == 'B') continue;
            HaitiKouhoListDict[AnyPiece] = DeriveHaitiKouhoList(AnyPiece);
        }
        if (mA == 0) HaitiKouhoListDict.Remove('A');
        if (mB == 0) HaitiKouhoListDict.Remove('B');

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.BanArr = new char[UB_X + 1, UB_Y + 1];
        for (int X = 0; X <= UB_X; X++)
            for (int Y = 0; Y <= UB_Y; Y++)
                WillPush.BanArr[X, Y] = (QuestionArr[X, Y] == 1 ? ' ' : '*');

        WillPush.CurrX = WillPush.CurrY = 0;
        WillPush.UsePieceDict = new Dictionary<char, int>();
        stk.Push(WillPush);

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

            //クリア判定
            if (Popped.BanArr.Cast<char>().All(X => X != ' ')) {
                //Console.WriteLine("解を発見。");
                //PrintAnswer(Popped.BanArr);
                Answer++;
                continue;
            }

            //Y座標の繰上げ処理
            if (Popped.CurrY > UB_Y) {
                Popped.CurrY = 0;
                Popped.CurrX++;
            }

            //最終列を超えた場合
            if (Popped.CurrX > UB_X) continue;

            foreach (char AnyPiece in PieceNameArr) {
                if (mA == 0 && AnyPiece == 'A') continue;
                if (mB == 0 && AnyPiece == 'B') continue;
                if (CanUsePieceDict.ContainsKey(AnyPiece) == false) continue;
                if (Popped.UsePieceDict.ContainsKey(AnyPiece)) {
                    //使用上限に到達している場合は、不可
                    if (Popped.UsePieceDict[AnyPiece] >= CanUsePieceDict[AnyPiece])
                        continue;
                }

                //ピースの配置候補リスト
                List<bool[,]> HaitiKouhoList = new List<bool[,]>();
                HaitiKouhoList.AddRange(HaitiKouhoListDict[AnyPiece]);

                //現在のマス目が空白の場合は、マス目を埋める必要あり
                if (Popped.BanArr[Popped.CurrX, Popped.CurrY] == ' ') {
                    HaitiKouhoList.RemoveAll(X => X[0, 0] == false);
                }

                //マス目にピースを埋めれない候補をRemove
                HaitiKouhoList.RemoveAll(X =>
                    CanFillPiece(X, Popped.CurrX, Popped.CurrY, Popped.BanArr) == false);

                WillPush.UsePieceDict = new Dictionary<char, int>(Popped.UsePieceDict);
                if (WillPush.UsePieceDict.ContainsKey(AnyPiece))
                    WillPush.UsePieceDict[AnyPiece]++;
                else WillPush.UsePieceDict[AnyPiece] = 1;

                //ピースを配置する経路のPush処理
                foreach (bool[,] AnyPieceMap in HaitiKouhoList) {
                    WillPush.BanArr = (char[,])Popped.BanArr.Clone();
                    WillPush.CurrX = Popped.CurrX;
                    WillPush.CurrY = Popped.CurrY + 1;

                    for (int X = 0; X <= AnyPieceMap.GetUpperBound(0); X++) {
                        for (int Y = 0; Y <= AnyPieceMap.GetUpperBound(1); Y++) {
                            if (AnyPieceMap[X, Y] == false) continue;
                            WillPush.BanArr[Popped.CurrX + X, Popped.CurrY + Y] = AnyPiece;
                        }
                    }
                    stk.Push(WillPush);
                }
            }

            //現在のマス目が空白でない場合は、ピースを配置しない経路のPush
            if (Popped.BanArr[Popped.CurrX, Popped.CurrY] != ' ') {
                WillPush.BanArr = Popped.BanArr;
                WillPush.CurrX = Popped.CurrX;
                WillPush.CurrY = Popped.CurrY + 1;
                WillPush.UsePieceDict = Popped.UsePieceDict;
                stk.Push(WillPush);
            }
        }
        Console.WriteLine(Answer);
    }

    //ピース名を引数として、回転させた配置のListを返す
    static List<bool[,]> DeriveHaitiKouhoList(char pPieceName)
    {
        bool[,] wkArr = null;

        //■
        //■
        if (pPieceName == 'A') {
            wkArr = new bool[1, 2];
            wkArr[0, 0] = true;
            wkArr[0, 1] = true;
        }

        //■
        if (pPieceName == 'B') {
            wkArr = new bool[1, 1];
            wkArr[0, 0] = true;
        }

        return DeriveKaitenArrList(wkArr);
    }

    //配列を引数として、回転させた配列のリストをDistinctして返す
    static List<bool[,]> DeriveKaitenArrList(bool[,] pBaseArr)
    {
        var KaitenArrList = new List<bool[,]>();

        //1個目はそのまま
        //■
        //■■■

        //2個目は1個目を時計回りに90度回転
        //■■
        //■
        //■

        //3個目は2個目を時計回りに90度回転
        //■■■
        //  ■

        //4個目は3個目を時計回りに90度回転
        // ■
        // ■
        //■■

        //5個目は1個目とX軸で線対称
        //■■■
        //■

        //6個目は5個目を時計回りに90度回転
        //■■
        // ■
        // ■

        //7個目は6個目を時計回りに90度回転
        //  ■
        //■■■

        //8個目は7個目を時計回りに90度回転
        //■
        //■
        //■■

        int BaseArrUB_X = pBaseArr.GetUpperBound(0);
        int BaseArrUB_Y = pBaseArr.GetUpperBound(1);

        for (int I = 1; I <= 8; I++) KaitenArrList.Add(null);
        for (int P = 0; P <= 6; P += 2) KaitenArrList[P] = new bool[BaseArrUB_X + 1, BaseArrUB_Y + 1];
        for (int P = 1; P <= 7; P += 2) KaitenArrList[P] = new bool[BaseArrUB_Y + 1, BaseArrUB_X + 1];

        for (int X = 0; X <= BaseArrUB_X; X++) {
            for (int Y = 0; Y <= BaseArrUB_Y; Y++) {
                bool SetVal = pBaseArr[X, Y];
                KaitenArrList[0][X, Y] = SetVal;
                KaitenArrList[1][Y, BaseArrUB_X - X] = SetVal;
                KaitenArrList[2][BaseArrUB_X - X, BaseArrUB_Y - Y] = SetVal;
                KaitenArrList[3][BaseArrUB_Y - Y, X] = SetVal;

                KaitenArrList[4][X, BaseArrUB_Y - Y] = SetVal;
                KaitenArrList[5][BaseArrUB_Y - Y, BaseArrUB_X - X] = SetVal;
                KaitenArrList[6][BaseArrUB_X - X, Y] = SetVal;
                KaitenArrList[7][Y, X] = SetVal;
            }
        }

        //Distinctする
        for (int I = KaitenArrList.Count - 1; 0 <= I; I--) {
            for (int J = 0; J <= I - 1; J++) {
                if (KaitenArrList[I].GetUpperBound(0) !=
                    KaitenArrList[J].GetUpperBound(0)) continue;
                if (KaitenArrList[I].GetUpperBound(1) !=
                    KaitenArrList[J].GetUpperBound(1)) continue;

                IEnumerable<bool> wkEnum1 = KaitenArrList[I].Cast<bool>();
                IEnumerable<bool> wkEnum2 = KaitenArrList[J].Cast<bool>();
                if (wkEnum1.SequenceEqual(wkEnum2) == false) continue;

                KaitenArrList.RemoveAt(I);
                break;
            }
        }
        return KaitenArrList;
    }

    //マス目にピースを埋めれるか
    static bool CanFillPiece(bool[,] pPieceMap, int pTargetX, int pTargetY, char[,] pBanArr)
    {
        for (int X = 0; X <= pPieceMap.GetUpperBound(0); X++) {
            if (pTargetX + X > UB_X) return false;
            for (int Y = 0; Y <= pPieceMap.GetUpperBound(1); Y++) {
                if (pTargetY + Y > UB_Y) return false;
                if (pPieceMap[X, Y] && pBanArr[pTargetX + X, pTargetY + Y] != ' ')
                    return false;
            }
        }
        return true;
    }

    //解を出力
    static void PrintAnswer(char[,] pBanArr)
    {
        var sb = new System.Text.StringBuilder();
        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                sb.Append(pBanArr[X, Y]);
            }
            sb.AppendLine();
        }
        Console.WriteLine(sb.ToString());
    }
}


解説

DFSでナイーブに敷き詰めてます。