Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Threading;
  12.  
  13.  
  14. namespace WindowsFormsApplication1
  15. {
  16. public partial class Form1 : Form
  17. {
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. Grid.RowCount = 10;
  22. Grid.AllowUserToAddRows = false;
  23.  
  24. //чтение из файла
  25. var names = new List<string>();
  26. string[] lines = File.ReadAllLines(@"E:/table.txt", Encoding.Default);
  27. for (int i = 0; i < 9; i++)
  28. {
  29. string[] array = lines[i].Split(';');
  30. for (int n = 0; n < 9;n++ )
  31. { Grid[n, i].Value = array[n]; }
  32. }
  33.  
  34. }
  35.  
  36. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  37. {
  38.  
  39. }
  40.  
  41. private void HorBtn_Click(object sender, EventArgs e)
  42. {
  43. int i, j;
  44. int[,] vgrid = new int[9, 9];//создание массива
  45. int[,] oldgrid = new int[9, 9];//создание старого массива
  46. int change = 1;
  47.  
  48. for (i = 0; i < 9; ++i)//запись из таблицы в массив
  49. for (j = 0; j < 9; ++j)
  50. if (Grid.Rows[i].Cells[j].Value!="")
  51. vgrid[i, j]=Convert.ToInt32(Grid.Rows[i].Cells[j].Value);
  52.  
  53.  
  54. // while (change != 0)//цикл редактирования
  55. // {
  56. for (i = 0; i < 9; ++i)//запись из нового массива в старый
  57. for (j = 0; j < 9; ++j)
  58. oldgrid[i, j] = vgrid[i, j];
  59. //начинаем редактирование
  60.  
  61. for (i = 0; i < 9; ++i) //горизонтальный прогон
  62. {
  63. int recx = 0;
  64. int recy = 0;
  65. int zeros = 0;
  66. int sum = 0;
  67. for (j = 0; j < 9; ++j)
  68. {
  69. sum = sum + vgrid[i, j];
  70. if (vgrid[i, j] == 0)
  71. {
  72. zeros++;
  73. recx = i; recy = j;
  74. }
  75. }
  76. if (zeros == 1) { vgrid[recx, recy] = 45 - sum;
  77. }
  78. }
  79. //////////////////////////////////////////////////
  80. for (i = 0; i < 9; ++i) //вертикальный прогон
  81. {
  82. int recx = 0;
  83. int recy = 0;
  84. int zeros = 0;
  85. int sum = 0;
  86. for (j = 0; j < 9; ++j)
  87. {
  88. sum = sum + vgrid[j, i];
  89. if (vgrid[j, i] == 0)
  90. {
  91. zeros++;
  92. recx = j; recy = i;
  93. }
  94. }
  95. if (zeros == 1) { vgrid[recx, recy] = 45 - sum;
  96. }
  97. }
  98. /////////////////////////////////////
  99. for (int x = 0; x < 3; x++)//прогон блока
  100. for (int y = 0; y < 3; y++)
  101. {
  102. int recx = 0;
  103. int recy = 0;
  104. int zeros = 0;
  105. int sum = 0;
  106. for (i = 3 * x; i < 3 + 3 * x; ++i) //горизонтальный прогон
  107. {
  108.  
  109. for (j = 3 * y; j < 3 + 3 * y; ++j)
  110. {
  111. sum = sum + vgrid[i, j];
  112. if (vgrid[i, j] == 0)
  113. {
  114. zeros++;
  115. recx = i; recy = j;
  116. }
  117. }
  118.  
  119. }
  120.  
  121. if (zeros == 1) { vgrid[recx, recy] = 45 - sum; }
  122. }
  123.  
  124. //метод исключения
  125. for (int x = 0; x < 3; x++)//прогон блока
  126. for (int y = 0; y < 3; y++)
  127. {
  128. int zeros = 0;
  129. int[] xblank = new int[10]; //х-координата пустой клетки
  130. int[] yblank = new int[10]; //у-координата пустой клетки
  131. bool[] available = new bool[10]; //доступность пустой клетки
  132. bool[] used = new bool[10]; //использованность пустой клетки другим числом
  133. bool present = false;
  134. int possibleblanks = 0;
  135. for(int a = 1; a<=9; a++)
  136. {
  137. available[a]=false;
  138. used[a]=false;
  139. }
  140.  
  141. for (i = 3 * x; i < 3 + 3 * x; ++i) //Сколько нулей, сука?
  142. {
  143. for (j = 3 * y; j < 3 + 3 * y; ++j)
  144. if (vgrid[i, j] == 0)
  145. {
  146. zeros++;
  147. xblank[zeros]=i;
  148. yblank[zeros] = j;
  149. available[zeros]=true;
  150. }
  151. }
  152. //Каких цифр не хватает?
  153. for(int n = 1; n<=9;n++)
  154. { present = false;
  155. possibleblanks = 0;
  156. for(int a = 1; a<=9; a++)
  157. {
  158. available[a]=true;
  159. }
  160. for (i = 3 * x; i < 3 + 3 * x; ++i)
  161. {
  162. for (j = 3 * y; j < 3 + 3 * y; ++j)
  163. if (vgrid[i, j] == n)
  164. {
  165. present = true;
  166. }
  167. }
  168. if(present==false)//если такой цифры нет, смотрим, можно ли подставить
  169. {
  170. for (int blankcount = 1; blankcount <= zeros; blankcount++)
  171. {
  172. if (used[blankcount] == false)//если пустая клетка не использована
  173. {
  174. for (int a = 0; a < 9; a++)//перекрестная проверка конфликта
  175. {
  176. if (vgrid[a, yblank[blankcount]] == n)
  177. { available[blankcount] = false; }
  178. if (vgrid[xblank[blankcount], a] == n)
  179. { available[blankcount] = false; }
  180. }
  181. }
  182. }
  183. for(int a = 1; a<=zeros; a++)//считаем колво возможных клеток
  184. {
  185. if((available[a]==true)&&(used[a]==false)) possibleblanks++;
  186. }
  187. if(possibleblanks==1)
  188. for(int a = 1; a<=9; a++)
  189. {
  190. if((available[a]==true)&&(used[a]==false))
  191. {
  192. vgrid[xblank[a], yblank[a]] = n;
  193. used[a] = true;
  194. }
  195. }
  196. }
  197. }
  198. }
  199.  
  200.  
  201.  
  202.  
  203. //заканчиваем редактирование
  204. change = 0;
  205. for (i = 0; i < 9; ++i) //проверяем массив на новизну
  206. for (j = 0; j < 9; ++j)
  207. {
  208. change = change + vgrid[i, j] - oldgrid[i, j];
  209. }
  210.  
  211. //} //конец редактирования
  212.  
  213.  
  214.  
  215. for (i = 0; i < 9; ++i) //запись массива в таблицу
  216. for (j = 0; j < 9; ++j)
  217. Grid.Rows[i].Cells[j].Value = vgrid[i, j];
  218.  
  219.  
  220. }
  221.  
  222. private void VertBtn_Click(object sender, EventArgs e)
  223. {
  224. var names = new List<string>();
  225. string[] lines = File.ReadAllLines(@"E:/table.txt", Encoding.Default);
  226. for (int i = 0; i < 9; i++)
  227. {
  228. string[] array = lines[i].Split(';');
  229. for (int n = 0; n < 9; n++)
  230. { Grid[n, i].Value = array[n]; }
  231. }
  232. }
  233.  
  234.  
  235. }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement