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

ABC096-C Grid Repainting 2


問題へのリンク


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

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

        char[,] BanArr = CreateBanArr(InputList.Skip(1).ToList());
        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        Func<int, int, bool> IsBlack = (pBaseX, pBaseY) =>
        {
            if (pBaseX < 0 || UB_X < pBaseX) return false;
            if (pBaseY < 0 || UB_Y < pBaseY) return false;
            return BanArr[pBaseX, pBaseY] == '#';
        };

        // 単独の黒マスの有無を調べる
        bool IsOK = true;
        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                if (BanArr[X, Y] != '#') continue;

                // 4近傍の黒マスをチェック
                if (IsBlack(X, Y - 1) == false
                 && IsBlack(X, Y + 1) == false
                 && IsBlack(X - 1, Y) == false
                 && IsBlack(X + 1, Y) == false) {
                    IsOK = false;
                }
            }
        }
        Console.WriteLine(IsOK ? "Yes" : "No");
    }

    // stringのListをcharの2次元配列に設定する
    static char[,] CreateBanArr(List<string> pStringList)
    {
        if (pStringList.Count == 0) {
            return new char[0, 0];
        }
        int UB_X = pStringList[0].Length - 1;
        int UB_Y = pStringList.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] = pStringList[Y][X];
            }
        }
        return WillReturn;
    }

    // 2次元配列のデバッグ出力
    static void PrintBan(char[,] pBanArr)
    {
        for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
                Console.Write(pBanArr[X, Y]);
            }
            Console.WriteLine();
        }
    }
}


解説

(4近傍に黒マスが無い)黒マスの有無をチェックしてます。