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

ABC398-D Bonfire


問題へのリンク


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("6 -2 1");
            WillReturn.Add("NNEEWS");
            //001010
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10 1 2");
            WillReturn.Add("NEESESWEES");
            //0001101011
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("20 -1 -2");
            WillReturn.Add("WWNNWSWEWNSWWENSNWWN");
            //00100111111000101111
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        char[] CharArr = InputList[1].ToCharArray();

        long BaseX = 0;
        long BaseY = 0;
        var SmokeSet = new HashSet<string>();
        SmokeSet.Add(GetHash(0, 0));

        foreach (char EachChar in CharArr) {
            if (EachChar == 'N') BaseY++;
            if (EachChar == 'S') BaseY--;

            if (EachChar == 'W') BaseX++;
            if (EachChar == 'E') BaseX--;

            string CurrHash = GetHash(C + BaseX, R + BaseY);
            if (SmokeSet.Contains(CurrHash)) {
                Console.Write(1);
            }
            else {
                Console.Write(0);
            }

            SmokeSet.Add(GetHash(BaseX, BaseY));
        }
        Console.WriteLine();
    }

    static string GetHash(long pX, long pY)
    {
        return string.Format("{0},{1}", pX, pY);
    }
}


解説

考察すると、
ナイーブにシュミレーションしつつも
煙の移動は、煙以外を逆ベクトルで移動させれば良いと分かります。