AtCoderのPAST    次のPASTの問題へ    前のPASTの問題へ

第2回PAST C 山崩し


問題へのリンク


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");
            WillReturn.Add("....#....");
            WillReturn.Add("...##X...");
            WillReturn.Add("..#####..");
            WillReturn.Add(".#X#####.");
            WillReturn.Add("#########");
            //....X....
            //...XXX...
            //..XX###..
            //.#X#####.
            //#########
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2");
            WillReturn.Add(".#.");
            WillReturn.Add("#X#");
            //.X.
            //#X#
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10");
            WillReturn.Add(".........#.........");
            WillReturn.Add("........###........");
            WillReturn.Add(".......#####.......");
            WillReturn.Add("......#######......");
            WillReturn.Add(".....#########.....");
            WillReturn.Add("....###########....");
            WillReturn.Add("...#############...");
            WillReturn.Add("..###############..");
            WillReturn.Add(".#################.");
            WillReturn.Add("X#X########X#X####X");
            //.........X.........
            //........XXX........
            //.......XXXXX.......
            //......XXXXXXX......
            //.....XXXXXXXXX.....
            //....XXXXXXXXXXX....
            //...XXX##XXXXXXXX...
            //..XXX####XXXXXXXX..
            //.XXX######XXXXX##X.
            //X#X########X#X####X
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        int[] wkArr = InputList[0].Split(' ').Select(X => int.Parse(X)).ToArray();
        int N = wkArr[0];
        int UB_X = InputList[1].Length - 1;
        int UB_Y = N - 1;

        char[,] TArr = new char[UB_X + 1, UB_Y + 1];

        for (int Y = 1; Y <= N; Y++) {
            for (int X = 0; X <= InputList[Y].Length - 1; X++) {
                TArr[X, Y - 1] = InputList[Y][X];
            }
        }

        for (int Y = UB_Y - 1; 0 <= Y; Y--) {
            for (int X = 0; X <= UB_X; X++) {
                if (TArr[X, Y] != '#') continue;
                if (TArr[X - 1, Y + 1] == 'X') TArr[X, Y] = 'X';
                if (TArr[X + 0, Y + 1] == 'X') TArr[X, Y] = 'X';
                if (TArr[X + 1, Y + 1] == 'X') TArr[X, Y] = 'X';
            }
        }

        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                Console.Write(TArr[X, Y]);
            }
            Console.WriteLine();
        }
    }
}


解説

下から上に向かって、ナイーブに走査してます。