fork download
  1. using System;
  2. using System.Linq;
  3.  
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. var table = "煩悩煩悩煩煩悩煩\r\n"
  9. + "煩煩悩空悩悩悩悩\r\n"
  10. + "悩空悩空悩空悩煩\r\n"
  11. + "空煩煩悩悩悩煩煩\r\n"
  12. + "悩悩悩煩悩煩煩悩\r\n"
  13. + "煩悩空空悩煩悩煩\r\n"
  14. + "煩煩煩悩煩空悩煩";
  15. var bt = new BonnoTable(table);
  16. bt.DestroyBonno();
  17. Console.Write(bt.ToString());
  18. }
  19. }
  20.  
  21. public class BonnoTableItem
  22. {
  23. public char Char { get; set; }
  24. public int BonnoCount { get; set; }
  25. }
  26.  
  27. public class BonnoTable
  28. {
  29. private BonnoTableItem[][] mRows;
  30.  
  31. public int RowCount { get { return mRows.Length; } }
  32.  
  33. public int BonnoCount { get; private set; }
  34.  
  35. public BonnoTable(string table)
  36. {
  37. var rows = table.Split(new[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
  38. mRows = new BonnoTableItem[rows.Length][];
  39. for (int r = 0; r < RowCount; r++)
  40. {
  41. var row = new BonnoTableItem[rows[r].Length];
  42. for (int c = 0; c < row.Length; c++) row[c] = new BonnoTableItem { Char = rows[r][c] };//row[c].Char = rows[r][c];
  43. mRows[r] = row;
  44. }
  45. UpdateBonnoCount();
  46. }
  47.  
  48. public BonnoTableItem this[RowCol rc]
  49. {
  50. get
  51. {
  52. if (rc.Row < 0 || rc.Row >= RowCount) return null;
  53. if (rc.Column < 0 || rc.Column >= mRows[rc.Row].Length) return null;
  54. return mRows[rc.Row][rc.Column];
  55. }
  56. }
  57.  
  58. private BonnoTableItem[] GetNeighbors(RowCol rc)
  59. {
  60. return rc.GetNeighbors().Select(x => this[x]).ToArray();
  61. }
  62.  
  63. private void UpdateBonnoCount()
  64. {
  65. BonnoCount = 0;
  66. for (int r = 0; r < RowCount; r++)
  67. {
  68. for (int c = 0; c < mRows[r].Length; c++)
  69. {
  70. var rc = new RowCol(r, c);
  71. var target = this[rc];
  72. target.BonnoCount = 0;
  73. char matchChar;
  74. switch(target.Char)
  75. {
  76. case '煩':
  77. matchChar = '悩';
  78. break;
  79.  
  80. case '悩':
  81. matchChar = '煩';
  82. break;
  83.  
  84. default:
  85. continue;
  86. }
  87.  
  88. var ns = GetNeighbors(rc);
  89. foreach (var item in ns)
  90. if (item != null && item.Char == matchChar) target.BonnoCount++;
  91. if (target.Char == '煩') this.BonnoCount += target.BonnoCount;
  92. }
  93. }
  94. }
  95.  
  96. private RowCol? FindMaxBonnoCountCell()
  97. {
  98. var max = 0;
  99. RowCol? maxCell = null;
  100. for (int r = 0; r < RowCount; r++)
  101. for (int c = 0; c < mRows[r].Length; c++)
  102. {
  103. RowCol rc = new RowCol(r, c);
  104. if (this[rc].BonnoCount > max)
  105. {
  106. max = this[rc].BonnoCount;
  107. maxCell = rc;
  108. }
  109. }
  110. return maxCell;
  111. }
  112.  
  113. private void ReplaceToKane(RowCol rc)
  114. {
  115. this[rc].Char = '鐘';
  116. UpdateBonnoCount();
  117. }
  118.  
  119. public void DestroyBonno()
  120. {
  121. while(BonnoCount > 0)
  122. {
  123. var rc = FindMaxBonnoCountCell();
  124. ReplaceToKane(rc.Value);
  125. }
  126. }
  127.  
  128. public override string ToString()
  129. {
  130. return string.Join("\r\n", mRows.Select(row => new string(row.Select(x => x.Char).ToArray())).ToArray());
  131. }
  132. }
  133.  
  134. public struct RowCol
  135. {
  136. public int Row { get; set; }
  137. public int Column { get; set; }
  138. public RowCol(int row, int col)
  139. :this()
  140. {
  141. Row = row; Column = col;
  142. }
  143.  
  144. public RowCol[] GetNeighbors()
  145. {
  146. var ns = new RowCol[8];
  147. var i = 0;
  148. for (int c = -1; c < 2; c++)
  149. for (int r = -1; r < 2; r++)
  150. ns[r == 0 && c == 0 ? i : i++] = new RowCol(Row + r, Column + c);
  151. return ns;
  152. }
  153. }
  154.  
Success #stdin #stdout 0.07s 34256KB
stdin
Standard input is empty
stdout
煩鐘鐘悩鐘鐘悩鐘
鐘鐘悩空悩悩悩悩
悩空悩空悩空鐘鐘
空鐘鐘悩悩鐘煩煩
悩悩悩鐘悩鐘煩鐘
鐘鐘空空鐘鐘鐘煩
煩煩煩鐘煩空鐘煩