Advertisement
Guest User

Untitled

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