Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace sudoku
  8. {
  9. public class Program
  10. {
  11. public static void Main(string[] args)
  12. {
  13. //byte[,] plocha = new byte[9, 9];
  14. byte aktivniKelii = 0;
  15. string vlez;
  16. bool aktivnost = true, validenVlez = false;
  17.  
  18. /*fillPlocha(plocha);*/
  19. byte[,] plocha = new byte[,]{
  20. {0, 2, 5, 0, 0, 1, 0, 0, 0},
  21. {1, 0, 4, 2, 5, 0, 0, 0, 0},
  22. {0, 5, 0, 0, 0, 0, 3, 2, 0},
  23. {6, 0, 0, 0, 2, 0, 0, 0, 9},
  24. {0, 8, 7, 0, 0, 0, 0, 6, 0},
  25. {0, 9, 1, 5, 0, 6, 6, 0, 0},
  26. {0, 9, 1, 5, 0, 0, 6, 0, 0},
  27. {0, 0, 0, 0, 7, 8, 1, 0, 3},
  28. {0, 0, 0, 6, 0, 0, 5, 9, 0},
  29. };
  30.  
  31. solve(plocha);
  32. /*while (aktivnost)
  33. {
  34. Console.WriteLine("Vnesi komanda (-iskluchi, -prikazhi, -vmetni, -reshi):\r\n");
  35. vlez = Console.ReadLine();
  36. switch (vlez)
  37. {
  38. case "-reshi":
  39. {
  40. for (byte x = 0; x < 9; x++)
  41. {
  42. for (byte y = 0; y < 9; y++)
  43. {
  44. if (plocha[x, y] != 0) aktivniKelii++;
  45. if (aktivniKelii > 17)
  46. {
  47. validenVlez = true;
  48. break;
  49. }
  50. }
  51. }
  52. if (!validenVlez)
  53. {
  54. Console.WriteLine("\r\nVmetni barem 17 vrednosti. Obidi se povtorno!\r\n");
  55. break;
  56. }
  57.  
  58. solve(plocha);
  59.  
  60. break;
  61. }
  62. case "-prikazhi":
  63. {
  64. Console.WriteLine();
  65. for (byte x = 0; x < 9; x++)
  66. {
  67. for (byte y = 0; y < 9; y++)
  68. {
  69. if (y == 8)
  70. {
  71. Console.WriteLine(plocha[x, y]);
  72. continue;
  73. }
  74. Console.Write(plocha[x, y] + " ");
  75. }
  76. }
  77. Console.WriteLine();
  78. break;
  79. }
  80. case "-vmetni":
  81. {
  82. Console.WriteLine("\r\nVnesi pozicija i vrednost (x y vrednost):\r\n");
  83. string baferVlez = Console.ReadLine();
  84. plocha[Byte.Parse(baferVlez[0].ToString()), Byte.Parse(baferVlez[2].ToString())] = Byte.Parse(baferVlez[4].ToString());
  85. Console.WriteLine("\r\nVmetnato.\r\n");
  86. break;
  87. }
  88. case "-iskluchi":
  89. {
  90. aktivnost = false;
  91. Console.WriteLine("\r\nAdios.");
  92. break;
  93. }
  94. default:
  95. {
  96. Console.Write("\r\nNevalidna komanda! ");
  97. break;
  98. }
  99. }
  100. }
  101. */
  102. }
  103.  
  104. private static void solve(byte[,] plocha)
  105. {
  106.  
  107. Console.WriteLine("Solving...");
  108. List<List<int>> validSet = new List<List<int>>();
  109. List<int> validRow = new List<int>();
  110. List<int> validCol = new List<int>();
  111. List<int> validBox = new List<int>();
  112.  
  113. for (int i = 0; i < 9; i++)
  114. {
  115. for (int j = 0; j < 9; j++)
  116. {
  117. int[] currRow = new int[9];
  118. int[] currCol = new int[9];
  119. int[] currBox = new int[9];
  120. int k = 0;
  121. //currRow
  122. for (int cntJ=0; cntJ<9; cntJ++)
  123. {
  124. if(plocha[i,cntJ] != 0)
  125. currRow[k] = plocha[i, cntJ];
  126.  
  127. k++;
  128. }
  129. Console.WriteLine("currRow filled");
  130. //currCol
  131. k = 0;
  132. for (int cntI = 0; cntI < 9; cntI++)
  133. {
  134. if (plocha[cntI, j] != 0)
  135. currCol[k] = plocha[cntI, j];
  136.  
  137. k++;
  138. }
  139. Console.WriteLine("currCol filled");
  140. //currBox
  141. int regSize = 3;
  142. int subBoxI = i / regSize;
  143. int subBoxJ = j / regSize;
  144. int offsetI = regSize * subBoxI;
  145. int offsetJ = regSize * subBoxJ;
  146. k = 0;
  147. for (int r = 0; r < 3; r++)
  148. {
  149. for(int c = 0; c < 3; c++)
  150. {
  151. if (plocha[offsetI + r, offsetJ + c] != 0)
  152. currBox[k] = plocha[offsetI + r, offsetJ + c];
  153.  
  154. k++;
  155. }
  156. }
  157.  
  158. Console.WriteLine("currBox filled");
  159. if (plocha[i,j] == 0)
  160. {
  161. for (int r = 1; r < 10; r++)
  162. {
  163. if (!currRow.Contains(r))
  164. validRow.Add(r);
  165. if (!currCol.Contains(r))
  166. validCol.Add(r);
  167. if (!currBox.Contains(r))
  168. validBox.Add(r);
  169. Console.WriteLine("Valid sets filled");
  170. }
  171.  
  172. List<int> temp = new List<int>();
  173.  
  174. for (int r = 1; r < 10; r++)
  175. {
  176. if (validRow.Contains(r) && validCol.Contains(r) && validBox.Contains(r))
  177. {
  178. temp = new List<int>();
  179. temp.Add(r);
  180. }
  181. }
  182. Console.WriteLine("Final valid set filled..");
  183. validSet.Add(temp);
  184. }
  185. }
  186. }
  187.  
  188. Console.WriteLine(validSet);
  189. }
  190. private static void fillPlocha(byte[,] plocha)
  191. {
  192. for (byte x = 0; x < 9; x++)
  193. {
  194. for (byte y = 0; y < 9; y++)
  195. {
  196. plocha[x, y] = 0;
  197. }
  198. }
  199. }
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement