結果

問題 No.38 赤青白ブロック
ユーザー 明智重蔵明智重蔵
提出日時 2015-10-31 19:40:33
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 202 ms / 5,000 ms
コード長 3,607 bytes
コンパイル時間 2,483 ms
コンパイル使用メモリ 104,920 KB
実行使用メモリ 29,264 KB
最終ジャッジ日時 2023-08-25 01:10:04
合計ジャッジ時間 5,486 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
23,008 KB
testcase_01 AC 116 ms
29,264 KB
testcase_02 AC 65 ms
25,060 KB
testcase_03 AC 65 ms
23,104 KB
testcase_04 AC 64 ms
21,124 KB
testcase_05 AC 65 ms
25,064 KB
testcase_06 AC 70 ms
29,032 KB
testcase_07 AC 68 ms
27,168 KB
testcase_08 AC 64 ms
21,224 KB
testcase_09 AC 71 ms
27,200 KB
testcase_10 AC 64 ms
24,984 KB
testcase_11 AC 64 ms
23,136 KB
testcase_12 AC 64 ms
23,008 KB
testcase_13 AC 64 ms
23,020 KB
testcase_14 AC 64 ms
23,012 KB
testcase_15 AC 65 ms
22,956 KB
testcase_16 AC 74 ms
27,164 KB
testcase_17 AC 66 ms
25,052 KB
testcase_18 AC 67 ms
25,184 KB
testcase_19 AC 69 ms
29,212 KB
testcase_20 AC 66 ms
27,080 KB
testcase_21 AC 65 ms
25,024 KB
testcase_22 AC 202 ms
29,240 KB
testcase_23 AC 63 ms
25,028 KB
testcase_24 AC 67 ms
27,016 KB
testcase_25 AC 65 ms
23,016 KB
testcase_26 AC 66 ms
22,896 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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("1 10");
            WillReturn.Add("RRRRRRRRRRWWWWWWWWWWBBBBBBBBBB");
            //21
            //Rの1個となりがRであってはならないのでRを9個抜く。
            //Bの10個となりにBがあることは無いのでBは抜かなくてよい。
            //最終的には RWWWWWWWWWWBBBBBBBBBB が残る。
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("1 2");
            WillReturn.Add("WRWWBBRRRWWBBRRRRWWWBBBRRWWBBB");
            //22
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("7 11");
            WillReturn.Add("BWBWRWRRWBRRWRRWWBBBRRWBBRWBBW");
            //27
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        var sw = System.Diagnostics.Stopwatch.StartNew();

        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(X => int.Parse(X)).ToArray();
        int Kr = wkArr[0];
        int Kb = wkArr[1];
        string S = InputList[1];

        Console.WriteLine(ExecDFS(S, Kr, Kb));
    }

    struct JyoutaiDef
    {
        internal int CurrP;
        internal string CurrStr;
        internal int RemoveCnt;
    }

    //2の20乗通りの順列を列挙
    static int ExecDFS(string pS, int pKr, int pKb)
    {
        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrP = 0;
        WillPush.CurrStr = "";
        WillPush.RemoveCnt = 0;
        stk.Push(WillPush);

        int AnswerRemoveCnt = int.MaxValue;

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

            //クリア判定
            if (Popped.CurrP > pS.Length - 1) {
                if (IsOK(Popped.CurrStr, pKr, pKb)) {
                    if (AnswerRemoveCnt > Popped.RemoveCnt) {
                        AnswerRemoveCnt = Popped.RemoveCnt;
                    }
                }
                continue;
            }

            Action<string> PushSyori = pAddStr =>
            {
                WillPush.CurrP = Popped.CurrP + 1;
                WillPush.CurrStr = Popped.CurrStr + pAddStr;
                WillPush.RemoveCnt = Popped.RemoveCnt;
                if (pAddStr.Length == 0) WillPush.RemoveCnt++;

                //下限値枝切り
                if (AnswerRemoveCnt <= WillPush.RemoveCnt)
                    return;

                stk.Push(WillPush);
            };

            if (pS[Popped.CurrP] == 'B') {
                PushSyori("B"); PushSyori("");
            }
            if (pS[Popped.CurrP] == 'R') {
                PushSyori("R"); PushSyori("");
            }
            if (pS[Popped.CurrP] == 'W') {
                PushSyori("W");
            }
        }
        return pS.Length - AnswerRemoveCnt;
    }

    //OKなレンガの配置かを判定
    static bool IsOK(string pS, int pKr, int pKb)
    {
        int UB = pS.Length - 1;
        for (int I = 0; I <= UB; I++) {
            if (pS[I] == 'R' && I + pKr <= UB && pS[I + pKr] == 'R')
                return false;
            if (pS[I] == 'B' && I + pKb <= UB && pS[I + pKb] == 'B')
                return false;
        }
        return true;
    }
}
0