AtCoderのPAST    次のPASTの問題へ    前のPASTの問題へ

第5回PAST G ヘビ


問題へのリンク


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

    struct PointDef
    {
        internal int X;
        internal int Y;
    }
    struct JyoutaiDef
    {
        internal List<PointDef> PointList;
    }

    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);

        int NeedLevel = BanArr.Cast<char>().Count(pX => pX == '#');

        JyoutaiDef WillPush;
        var Stk = new Stack<JyoutaiDef>();
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (BanArr[LoopX, LoopY] == '#') {
                    WillPush.PointList = new List<PointDef>();
                    WillPush.PointList.Add(new PointDef() { X = LoopX, Y = LoopY });
                    Stk.Push(WillPush);
                }
            }
        }

        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();

            // クリア判定
            if (Popped.PointList.Count == NeedLevel) {
                Console.WriteLine(NeedLevel);
                foreach (PointDef EachPoint in Popped.PointList) {
                    Console.WriteLine("{0} {1}", EachPoint.Y + 1, EachPoint.X + 1);
                }
                return;
            }

            Action<int, int> PushAct = (pNewX, pNewY) =>
            {
                if (pNewX < 0 || UB_X < pNewX) return;
                if (pNewY < 0 || UB_Y < pNewY) return;

                if (BanArr[pNewX, pNewY] != '#') return;

                PointDef NewPos = new PointDef() { X = pNewX, Y = pNewY };
                if (Popped.PointList.Contains(NewPos)) {
                    return;
                }
                WillPush.PointList = new List<PointDef>(Popped.PointList);
                WillPush.PointList.Add(NewPos);
                Stk.Push(WillPush);
            };

            PointDef CurrPos = Popped.PointList.Last();
            int CurrX = CurrPos.X;
            int CurrY = CurrPos.Y;
            PushAct(CurrX, CurrY - 1);
            PushAct(CurrX, CurrY + 1);
            PushAct(CurrX - 1, CurrY);
            PushAct(CurrX + 1, CurrY);
        }
    }

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


解説

全ての始点からDFSを行い、
一筆書きできるかを判定してます。