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

ABC339-B Langton's Takahashi


問題へのリンク


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

    static char[,] mBanArr;
    static long UB_X;
    static long UB_Y;
    static long mN;

    static long mVectX = 0;
    static long mVectY = -1;

    static long mCurrX = 0;
    static long mCurrY = 0;

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        UB_Y = wkArr[0] - 1;
        UB_X = wkArr[1] - 1;
        mN = wkArr[2];

        mBanArr = new char[UB_X + 1, UB_Y + 1];
        for (long X = 0; X <= UB_X; X++) {
            for (long Y = 0; Y <= UB_Y; Y++) {
                mBanArr[X, Y] = '.';
            }
        }

        for (long I = 1; I <= mN; I++) {
            if (mBanArr[mCurrX, mCurrY] == '.') {
                mBanArr[mCurrX, mCurrY] = '#';
                KaitenVect90Do(ref mVectX, ref mVectY);
            }
            else {
                mBanArr[mCurrX, mCurrY] = '.';
                KaitenVect270Do(ref mVectX, ref mVectY);
            }

            mCurrX += mVectX;
            mCurrY += mVectY;

            mCurrX %= UB_X + 1;
            mCurrY %= UB_Y + 1;
            if (mCurrX == -1) mCurrX = UB_X;
            if (mCurrY == -1) mCurrY = UB_Y;
        }

        var sb = new System.Text.StringBuilder();
        for (int Y = 0; Y <= mBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= mBanArr.GetUpperBound(0); X++) {
                sb.Append(mBanArr[X, Y]);
            }
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }

    // ベクトルのX成分とY成分を引数とし、90度回転させた値を設定
    static void KaitenVect90Do(ref long pVectX, ref long pVectY)
    {
        long NewX = -pVectY, NewY = pVectX;
        pVectX = NewX; pVectY = NewY;
    }

    // ベクトルのX成分とY成分を引数とし、180度回転させた値を設定
    static void KaitenVect180Do(ref long pVectX, ref long pVectY)
    {
        long NewX = -pVectX, NewY = -pVectX;
        pVectX = NewX; pVectY = NewY;
    }

    // ベクトルのX成分とY成分を引数とし、270度回転させた値を設定
    static void KaitenVect270Do(ref long pVectX, ref long pVectY)
    {
        long NewX = pVectY, NewY = -pVectX;
        pVectX = NewX; pVectY = NewY;
    }

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


解説

変位ベクトルを持ってシュミレーションしてます。