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

ABC241-C Connect 6


問題へのリンク


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

    static char[,] mBanArr;
    static int UB;

    static void Main()
    {
        List<string> InputList = GetInputList();
        mBanArr = CreateBanArr(InputList.Skip(1));
        UB = mBanArr.GetUpperBound(0);

        for (int LoopX = 0; LoopX <= UB; LoopX++) {
            for (int LoopY = 0; LoopY <= UB; LoopY++) {
                bool Result1 = IsOK(LoopX, LoopY, 1, 0);
                bool Result2 = IsOK(LoopX, LoopY, 0, 1);
                bool Result3 = IsOK(LoopX, LoopY, 1, 1);
                bool Result4 = IsOK(LoopX, LoopY, -1, 1);

                if (Result1 || Result2 || Result3 || Result4) {
                    Console.WriteLine("Yes");
                    return;
                }
            }
        }
        Console.WriteLine("No");
    }

    static bool IsOK(int pBaseX, int pBaseY, int pVectX, int pVectY)
    {
        int PaintCnt = 0;
        if (mBanArr[pBaseX, pBaseY] == '.') {
            PaintCnt++;
        }
        int CurrX = pBaseX;
        int CurrY = pBaseY;
        for (int I = 1; I <= 5; I++) {
            CurrX += pVectX;
            CurrY += pVectY;

            if (CurrX < 0 || UB < CurrX) return false;
            if (CurrY < 0 || UB < CurrY) return false;
            if (mBanArr[CurrX, CurrY] == '.') {
                PaintCnt++;
            }
        }
        return PaintCnt <= 2;
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をcharの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static char[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new char[0, 0];
        }
        int UB_X = StrList[0].Length - 1;
        int UB_Y = StrList.Count - 1;

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

        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = StrList[Y][X];
            }
        }
        return WillReturn;
    }
}


解説

全ての座標から連続マスを試してます。