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

ABC357-C Sierpinski carpet


問題へのリンク


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

    static void Main()
    {
        List<string> InputList = GetInputList();
        int N = int.Parse(InputList[0]);
        if (N == 0) {
            Console.WriteLine("#");
            return;
        }

        char[,] pLevel1 = new char[3, 3];
        pLevel1[0, 0] = '#';
        pLevel1[1, 0] = '#';
        pLevel1[2, 0] = '#';
        pLevel1[0, 1] = '#';
        pLevel1[1, 1] = '.';
        pLevel1[2, 1] = '#';
        pLevel1[0, 2] = '#';
        pLevel1[1, 2] = '#';
        pLevel1[2, 2] = '#';

        Action<int, char[,], char[,]> FillAct = (pBeki3, pNewBan, pBase) =>
        {
            FillSpace(pNewBan);
            Fill(pNewBan, 0, 0, pBase);
            Fill(pNewBan, 3 * pBeki3, 0, pBase);
            Fill(pNewBan, 6 * pBeki3, 0, pBase);
            Fill(pNewBan, 0, 3 * pBeki3, pBase);
            Fill(pNewBan, 6 * pBeki3, 3 * pBeki3, pBase);
            Fill(pNewBan, 0, 6 * pBeki3, pBase);
            Fill(pNewBan, 3 * pBeki3, 6 * pBeki3, pBase);
            Fill(pNewBan, 6 * pBeki3, 6 * pBeki3, pBase);
        };

        char[,] pLevel2 = new char[3 * 3, 3 * 3];
        int Beki3 = 1;
        FillAct(Beki3, pLevel2, pLevel1);

        char[,] pLevel3 = new char[3 * 3 * 3, 3 * 3 * 3];
        Beki3 *= 3;
        FillAct(Beki3, pLevel3, pLevel2);

        char[,] pLevel4 = new char[3 * 3 * 3 * 3, 3 * 3 * 3 * 3];
        Beki3 *= 3;
        FillAct(Beki3, pLevel4, pLevel3);

        char[,] pLevel5 = new char[3 * 3 * 3 * 3 * 3, 3 * 3 * 3 * 3 * 3];
        Beki3 *= 3;
        FillAct(Beki3, pLevel5, pLevel4);

        Beki3 *= 3;
        char[,] pLevel6 = new char[3 * 3 * 3 * 3 * 3 * 3, 3 * 3 * 3 * 3 * 3 * 3];
        FillAct(Beki3, pLevel6, pLevel5);

        if (N == 1) PrintBan(pLevel1);
        if (N == 2) PrintBan(pLevel2);
        if (N == 3) PrintBan(pLevel3);
        if (N == 4) PrintBan(pLevel4);
        if (N == 5) PrintBan(pLevel5);
        if (N == 6) PrintBan(pLevel6);
    }

    static void FillSpace(char[,] pTargetArr)
    {
        for (int X = 0; X <= pTargetArr.GetUpperBound(0); X++) {
            for (int Y = 0; Y <= pTargetArr.GetUpperBound(1); Y++) {
                pTargetArr[X, Y] = '.';
            }
        }
    }

    static void Fill(char[,] pTargetArr, int pStaX, int pStaY, char[,] pFillArr)
    {
        for (int X = 0; X <= pFillArr.GetUpperBound(0); X++) {
            for (int Y = 0; Y <= pFillArr.GetUpperBound(1); Y++) {
                pTargetArr[pStaX + X, pStaY + Y] = pFillArr[X, Y];
            }
        }
    }

    ////////////////////////////////////////////////////////////////
    // 2次元配列(char型)のデバッグ出力
    ////////////////////////////////////////////////////////////////
    static void PrintBan(char[,] 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());
    }
}


解説

定義にそって
レベル1からレベル6までを求めてます。