結果
問題 | No.38 赤青白ブロック |
ユーザー | 明智重蔵 |
提出日時 | 2015-10-31 19:40:33 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 178 ms / 5,000 ms |
コード長 | 3,607 bytes |
コンパイル時間 | 1,292 ms |
コンパイル使用メモリ | 109,568 KB |
実行使用メモリ | 24,576 KB |
最終ジャッジ日時 | 2024-12-23 12:53:54 |
合計ジャッジ時間 | 3,555 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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; } }