AtCoderのARC    次のARCの問題へ    前のARCの問題へ

ARC131-B Grid Repainting 4


問題へのリンク


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("...");
            //132
            //313
            //541
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 7");
            WillReturn.Add("1.2.3.4");
            WillReturn.Add(".5.1.2.");
            WillReturn.Add("3.4.5.1");
            WillReturn.Add(".2.3.4.");
            WillReturn.Add("5.1.2.3");
            //1425314
            //2531425
            //3142531
            //4253142
            //5314253
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("1 1");
            WillReturn.Add(".");
            //4
        }
        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));
        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (BanArr[LoopX, LoopY] != '.') {
                    continue;
                }

                var ColorList = new List<int>();

                Action<int, int> AddColor = (pX, pY) =>
                {
                    if (pX < 0 || UB_X < pX) return;
                    if (pY < 0 || UB_Y < pY) return;

                    ColorList.Add(BanArr[pX, pY]);
                };

                AddColor(LoopX, LoopY - 1);
                AddColor(LoopX, LoopY + 1);
                AddColor(LoopX - 1, LoopY);
                AddColor(LoopX + 1, LoopY);

                for (char I = '1'; I <= '5'; I++) {
                    if (ColorList.Contains(I)) continue;
                    BanArr[LoopX, LoopY] = I;
                    break;
                }
            }
        }
        PrintBan(BanArr);
    }

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

    ////////////////////////////////////////////////////////////////
    // 2次元配列(char型)のデバッグ出力
    ////////////////////////////////////////////////////////////////
    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マスで
5色使えますので、塗れる色を塗っていっても
詰むことは無いと分かります。

詰むことが無いので、DFSを行う必要は無く
貪欲法で解けます。