// あなたのコーディングスキルを丸裸に! // http://w...content-available-to-author-only...o.jp/enterprise/articles/1004/03/news002.html using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Mahjong { class Program { static string[] machi = { "11", "22", "33", "44", "55", "66", "77", "88", "99", // shabo "12", "23", "34", "45", "56", "67", "78", "89", // ryanmen penchan "13", "24", "35", "46", "57", "68", "79", // kanchan }; static string[] mentsu = { "123", "234", "345", "456", "567", "678", "789", // shuntsu "111", "222", "333", "444", "555", "666", "777", "888", "999", // kohtsu }; static string[] atama = { "11", "22", "33", "44", "55", "66", "77", "88", "99", }; static bool TryExtract(string input, string mentsu, out string remains) { // input から mentsu を抜ければ抜いて残りをremainsに。 List chr = new List(input); bool ok = true; foreach (var c in mentsu) ok &= chr.Remove(c); remains = new string(chr.ToArray()); return ok; } static void Find(string remains, int index, string current, Action onFound) { // 抜ける三枚組を抜いて再帰探索。 //Console.Error.WriteLine(current + " / " + remains); string next; if (remains.Length == 1) { onFound(current + "[" + remains + "]"); // tanki } else if (remains.Length == 2) { if (machi.Contains(remains)) // is machi? onFound(current + "[" + remains + "]"); // ok } else { for (int i = index; i < mentsu.Length; i++) if (TryExtract(remains, mentsu[i], out next)) Find(next, i, current + "(" + mentsu[i] + ")", onFound); } } static void Find(string input, Action onFound) { string next; // 雀頭を抜いて探索 foreach (var m in atama) if (TryExtract(input, m, out next)) Find(next, 0, "(" + m + ")", onFound); // 単騎待ち仮定。 Find(input, 0, "", onFound); // てぃんいとぅてぃーとぅいとぅ // こーつがない && たーつ抜けるだけ抜いたら1枚になった? } static void Main(string[] args) { Array.Sort(mentsu); while (true) { string input = Console.ReadLine(); if (input == null) break; Console.WriteLine("Input: " + input); Find(input, Console.WriteLine); Console.WriteLine("end."); } } } }