fork download
  1. // あなたのコーディングスキルを丸裸に!
  2. // http://w...content-available-to-author-only...o.jp/enterprise/articles/1004/03/news002.html
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. namespace Mahjong
  10. {
  11. class Program
  12. {
  13. static string[] machi =
  14. {
  15. "11", "22", "33", "44", "55", "66", "77", "88", "99", // shabo
  16. "12", "23", "34", "45", "56", "67", "78", "89", // ryanmen penchan
  17. "13", "24", "35", "46", "57", "68", "79", // kanchan
  18. };
  19.  
  20. static string[] mentsu =
  21. {
  22. "123", "234", "345", "456", "567", "678", "789", // shuntsu
  23. "111", "222", "333", "444", "555", "666", "777", "888", "999", // kohtsu
  24. };
  25.  
  26. static string[] atama =
  27. {
  28. "11", "22", "33", "44", "55", "66", "77", "88", "99",
  29. };
  30.  
  31. static bool TryExtract(string input, string mentsu, out string remains)
  32. {
  33. // input から mentsu を抜ければ抜いて残りをremainsに。
  34. List<char> chr = new List<char>(input);
  35. bool ok = true;
  36. foreach (var c in mentsu)
  37. ok &= chr.Remove(c);
  38.  
  39. remains = new string(chr.ToArray());
  40. return ok;
  41. }
  42.  
  43. static void Find(string remains, int index, string current, Action<string> onFound)
  44. {
  45. // 抜ける三枚組を抜いて再帰探索。
  46. //Console.Error.WriteLine(current + " / " + remains);
  47. string next;
  48. if (remains.Length == 1)
  49. {
  50. onFound(current + "[" + remains + "]"); // tanki
  51. }
  52. else if (remains.Length == 2)
  53. {
  54. if (machi.Contains(remains)) // is machi?
  55. onFound(current + "[" + remains + "]"); // ok
  56. }
  57. else
  58. {
  59. for (int i = index; i < mentsu.Length; i++)
  60. if (TryExtract(remains, mentsu[i], out next))
  61. Find(next, i, current + "(" + mentsu[i] + ")", onFound);
  62. }
  63. }
  64.  
  65. static void Find(string input, Action<string> onFound)
  66. {
  67. string next;
  68. // 雀頭を抜いて探索
  69. foreach (var m in atama)
  70. if (TryExtract(input, m, out next))
  71. Find(next, 0, "(" + m + ")", onFound);
  72.  
  73. // 単騎待ち仮定。
  74. Find(input, 0, "", onFound);
  75.  
  76. // てぃんいとぅてぃーとぅいとぅ
  77. // こーつがない && たーつ抜けるだけ抜いたら1枚になった?
  78. }
  79.  
  80. static void Main(string[] args)
  81. {
  82. Array.Sort(mentsu);
  83.  
  84. while (true)
  85. {
  86. string input = Console.ReadLine();
  87. if (input == null) break;
  88. Console.WriteLine("Input: " + input);
  89. Find(input, Console.WriteLine);
  90. Console.WriteLine("end.");
  91. }
  92. }
  93. }
  94. }
Success #stdin #stdout 0.06s 37392KB
stdin
1112224588899
1122335556799
1112223335559
1223344888999
1112345678999
stdout
Input: 1112224588899
(99)(111)(222)(888)[45]
end.
Input: 1122335556799
(55)(123)(123)(567)[99]
(99)(123)(123)(555)[67]
(99)(123)(123)(567)[55]
end.
Input: 1112223335559
(111)(222)(333)(555)[9]
(123)(123)(123)(555)[9]
end.
Input: 1223344888999
(44)(123)(888)(999)[23]
(123)(234)(888)(999)[4]
(234)(234)(888)(999)[1]
end.
Input: 1112345678999
(11)(123)(456)(789)[99]
(11)(123)(456)(999)[78]
(11)(123)(678)(999)[45]
(11)(345)(678)(999)[12]
(99)(111)(234)(567)[89]
(99)(111)(234)(789)[56]
(99)(111)(456)(789)[23]
(99)(123)(456)(789)[11]
(111)(234)(567)(999)[8]
(111)(234)(678)(999)[5]
(111)(345)(678)(999)[2]
end.