fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace SudokuSolver
  7. {
  8. public class STable
  9. {
  10. readonly static int SPSIZE = 81;
  11. int[] SPlacesInit = new int[SPSIZE];
  12. int[] SPlaces = new int[SPSIZE];
  13. List<int[]> SAreas;
  14.  
  15. public STable()
  16. {
  17. SAreas = new List<int[]>();
  18. //Areas
  19. for(int ia = 0; ia<9; ia++)
  20. {
  21. //Row
  22. int[] ira = new int[9];
  23. for(int ir = 0; ir <9; ir++)
  24. {
  25. ira[ir] = 9 * ia + ir;
  26. }
  27. SAreas.Add(ira);
  28. //Col
  29. int[] ica = new int[9];
  30. for (int ic = 0; ic < 9; ic++)
  31. {
  32. ica[ic] = 9 * ic + ia;
  33. }
  34. SAreas.Add(ica);
  35. //Block
  36. int[] iba = new int[9];
  37. for (int ib = 0; ib < 9; ib++)
  38. {
  39. iba[ib] = ((int)(ia / 3) * 27 + (ia % 3) * 3) + ((int)(ib / 3) * 9 + ib % 3);
  40. }
  41. SAreas.Add(iba);
  42. }
  43. }
  44.  
  45. public bool LoadTable(Dictionary<int, int> Values)
  46. {
  47. foreach (KeyValuePair<int, int> kvp in Values)
  48. {
  49. if (kvp.Key > 0 && kvp.Key < SPSIZE &&
  50. kvp.Value >= 0 && kvp.Value <= 9)
  51. SPlacesInit[kvp.Key] = kvp.Value;
  52. else
  53. {
  54. Array.Clear(SPlacesInit, 0, SPSIZE);
  55. return false;
  56. }
  57. }
  58. if (TestAreas())
  59. {
  60. Array.Copy(SPlacesInit, SPlaces, SPSIZE);
  61. return true;
  62. }
  63. else
  64. {
  65. Array.Clear(SPlacesInit, 0, SPSIZE);
  66. return false;
  67. }
  68. }
  69.  
  70. public bool LoadTable(List<int> Values)
  71. {
  72. if (Values.Count != SPSIZE)
  73. return false;
  74. int counter = 0;
  75. foreach (int value in Values)
  76. {
  77. if (value < 0 && value > 9)
  78. {
  79. Array.Clear(SPlacesInit, 0, SPSIZE);
  80. return false;
  81. }
  82. SPlacesInit[counter] = value;
  83. counter++;
  84. }
  85. if (TestAreas())
  86. {
  87. Array.Copy(SPlacesInit, SPlaces, SPSIZE);
  88. return true;
  89. }
  90. else
  91. {
  92. Array.Clear(SPlacesInit, 0, SPSIZE);
  93. return false;
  94. }
  95. }
  96.  
  97. private List<int[]> GetAreas(int Place)
  98. {
  99. return SAreas.FindAll(arr => arr.Contains(Place));
  100. }
  101.  
  102. private List<int> AvailableValues(int Place)
  103. {
  104. List<int> Avaliable = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
  105. List<int[]> RespectiveAreas = GetAreas(Place);
  106. foreach (int[] Area in RespectiveAreas)
  107. foreach (int TPlace in Area)
  108. if (SPlaces[TPlace] != 0)
  109. Avaliable.Remove(SPlaces[TPlace]);
  110. return Avaliable;
  111. }
  112.  
  113. public bool TestAreas()
  114. {
  115. for (int ix = 0; ix<SPSIZE ; ix ++)
  116. {
  117. if (SPlaces[ix] == 0)
  118. {
  119. List<int> AV = AvailableValues(ix);
  120. if (AV.Count == 0)
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126.  
  127. public override string ToString()
  128. {
  129. StringBuilder sb = new StringBuilder();
  130. for (int ix = 0; ix < SPSIZE;)
  131. {
  132. sb.Append(SPlaces[ix]);
  133. ix++;
  134. if (ix % 9 == 0)
  135. sb.Append(Environment.NewLine);
  136. else if (ix % 3 == 0)
  137. sb.Append("|");
  138. if (ix % 27 == 0)
  139. {
  140. sb.Append("---+---+---");
  141. sb.Append(Environment.NewLine);
  142. }
  143. }
  144. return sb.ToString();
  145. }
  146.  
  147. public bool FillTable()
  148. {
  149. Console.Clear();
  150. int cPos = 0;
  151. bool rollbacked = false;
  152. while (cPos < SPSIZE)
  153. {
  154. //Console.SetCursorPosition(0, 0);
  155. //Console.Write(this.ToString());
  156. int cValue = SPlaces[cPos];
  157. if (!rollbacked && cValue > 0 && TestAreas())
  158. {
  159. cPos++;
  160. rollbacked = false;
  161. continue;
  162. }
  163. else if (cValue > 0 && (rollbacked || !TestAreas()))
  164. {
  165. List<int> AV = AvailableValues(cPos);
  166. var cNewValues = from v in AV
  167. where v > cValue
  168. select v;
  169. if (cNewValues.Count() == 0)
  170. {
  171. //Rollback
  172. SPlaces[cPos] = 0;
  173. cPos--;
  174. while (SPlacesInit[cPos] != 0)
  175. {
  176. if (cPos < 0)
  177. return false;
  178. cPos--;
  179. }
  180. rollbacked = true;
  181. }
  182. else
  183. {
  184. //New Position Value
  185. SPlaces[cPos] = cNewValues.Min();
  186. rollbacked = false;
  187. }
  188. }
  189. else if (cValue == 0)
  190. {
  191. List<int> AV = AvailableValues(cPos);
  192. var cNewValues = from v in AV
  193. where v > cValue
  194. select v;
  195. SPlaces[cPos] = cNewValues.Min();
  196. rollbacked = false;
  197. }
  198. else
  199. {
  200. //Should not be
  201. return false;
  202. }
  203. }
  204. return true;
  205. }
  206. }
  207. }
  208.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
error CS5001: Program `prog.exe' does not contain a static `Main' method suitable for an entry point
Compilation failed: 1 error(s), 0 warnings
stdout
Standard output is empty