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

ARC007-C 節約生活


問題へのリンク


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("oxoxx");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("oxxxxoooo");
            //2
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("ox");
            //2
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("o");
            //1
        }
        else if (InputPattern == "Input5") {
            WillReturn.Add("xxxoxo");
            //4
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct JyoutaiDef
    {
        internal string CurrStr;
        internal HashSet<int> SelectedIndSet;
        internal int Level;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        string S = InputList[0];
        int UB = S.Length - 1;

        // 処理01 順番を入れ替えた配列を作る
        string S2 = S + S;
        var RotateSList = new List<string>();
        for (int I = 1; I <= UB; I++) {
            RotateSList.Add(S2.Substring(I, S.Length));
        }

        // 処理02 順番を入れ替えた配列とマージするDFSを行う
        int Answer = int.MaxValue;
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrStr = S;
        WillPush.SelectedIndSet = new HashSet<int>();
        WillPush.Level = 1;
        Stk.Push(WillPush);

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

            // クリア判定
            if (Popped.CurrStr.Contains('x') == false) {
                Answer = Math.Min(Answer, Popped.Level);
                continue;
            }

            for (int I = 0; I <= RotateSList.Count - 1; I++) {
                if (Popped.SelectedIndSet.Contains(I)) continue;

                var sb = new System.Text.StringBuilder();
                for (int J = 0; J <= Popped.CurrStr.Length - 1; J++) {
                    if (Popped.CurrStr[J] == 'o' || RotateSList[I][J] == 'o') {
                        sb.Append('o');
                    }
                    else {
                        sb.Append('x');
                    }
                }
                WillPush.CurrStr = sb.ToString();
                WillPush.SelectedIndSet = new HashSet<int>(Popped.SelectedIndSet);
                WillPush.SelectedIndSet.Add(I);
                WillPush.Level = Popped.Level + 1;
                Stk.Push(WillPush);
            }
        }
        Console.WriteLine(Answer);
    }
}


解説

10の階上は、3628800なので
ナイーブに全探索してます。