Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <windows.h>
  4.  
  5. int field1[64];
  6. int field2[64];
  7. int c = 0;
  8.  
  9.  
  10. void attack(int i)
  11. {
  12. int x = i / 8;
  13. int y = i % 8;
  14. if (++field2[i] == 1)
  15. c++;
  16.  
  17. if ((x - 2) >= 0)
  18. {
  19. if ((y - 1) >= 0)
  20. {
  21. if (++(*(field2 + 8*(x-2) + (y-1))) == 1)
  22. c++;
  23. }
  24.  
  25. if ((y + 1) <= 7)
  26. {
  27. if (++(*(field2 + 8*(x-2) + (y+1))) == 1)
  28. c++;
  29. }
  30. }
  31.  
  32. if ((x + 2) <= 7)
  33. {
  34. if ((y - 1) >= 0)
  35. {
  36. if (++(*(field2 + 8*(x+2) + (y-1))) == 1)
  37. c++;
  38. }
  39.  
  40. if ((y + 1) <= 7)
  41. {
  42. if (++(*(field2 + 8*(x+2) + (y+1))) == 1)
  43. c++;
  44. }
  45. }
  46.  
  47. if ((y - 2) >= 0)
  48. {
  49. if ((x - 1) >= 0)
  50. {
  51. if (++(*(field2 + (y-2) + 8*(x-1))) == 1)
  52. c++;
  53. }
  54.  
  55. if ((x + 1) <= 7)
  56. {
  57. if (++(*(field2 + (y-2) + 8*(x+1))) == 1)
  58. c++;
  59. }
  60. }
  61.  
  62. if ((y + 2) <= 7)
  63. {
  64. if ((x - 1) >= 0)
  65. {
  66. if (++(*(field2 + (y+2) + 8*(x-1))) == 1)
  67. c++;
  68. }
  69.  
  70. if ((x + 1) <= 7)
  71. {
  72. if (++(*(field2 + (y+2) + 8*(x+1))) == 1)
  73. c++;
  74. }
  75. }
  76. }
  77.  
  78. void unattack(int i)
  79. {
  80. int x = i / 8;
  81. int y = i % 8;
  82. if (--field2[i] == 0)
  83. c--;
  84.  
  85. if ((x - 2) >= 0)
  86. {
  87. if ((y - 1) >= 0)
  88. {
  89. if (--(*(field2 + 8*(x-2) + (y-1))) == 0)
  90. c--;
  91. }
  92.  
  93. if ((y + 1) <= 7)
  94. {
  95. if (--(*(field2 + 8*(x-2) + (y+1))) == 0)
  96. c--;
  97. }
  98. }
  99.  
  100. if ((x + 2) <= 7)
  101. {
  102. if ((y - 1) >= 0)
  103. {
  104. if (--(*(field2 + 8*(x+2) + (y-1))) == 0)
  105. c--;
  106. }
  107.  
  108. if ((y + 1) <= 7)
  109. {
  110. if (--(*(field2 + 8*(x+2) + (y+1))) == 0)
  111. c--;
  112. }
  113. }
  114.  
  115. if ((y - 2) >= 0)
  116. {
  117. if ((x - 1) >= 0)
  118. {
  119. if (--(*(field2 + (y-2) + 8*(x-1))) == 0)
  120. c--;
  121. }
  122.  
  123. if ((x + 1) <= 7)
  124. {
  125. if (--(*(field2 + (y-2) + 8*(x+1))) == 0)
  126. c--;
  127. }
  128. }
  129.  
  130. if ((y + 2) <= 7)
  131. {
  132. if ((x - 1) >= 0)
  133. {
  134. if (--(*(field2 + (y+2) + 8*(x-1))) == 0)
  135. c--;
  136. }
  137.  
  138. if ((x + 1) <= 7)
  139. {
  140. if (--(*(field2 + (y+2) + 8*(x+1))) == 0)
  141. c--;
  142. }
  143. }
  144. }
  145.  
  146. void prinfields()
  147. {
  148. for (int i = 0; i < 64; i++)
  149. {
  150. printf("%d ", field1[i]);
  151. if ((i+1) % 8 == 0)
  152. printf("\n");
  153. }
  154. printf("\n");
  155. for (int i = 0; i < 64; i++)
  156. {
  157. printf("%d ", field2[i]);
  158. if ((i+1) % 8 == 0)
  159. printf("\n");
  160. }
  161. printf("%d", c);
  162. printf("\n\n\n");
  163. }
  164.  
  165. bool proverka(int i)
  166. {
  167. bool p = 1;
  168. for (int j = i / 8 - 3; (j < i / 8 + 8) && p; j++)
  169. if (field2[j] == 0)
  170. p = 0;
  171. return p;
  172. }
  173.  
  174. void search(int k, int index)
  175. {
  176. if (k <= 12)
  177. {
  178. for (int i = index; i < 55 && c < 64; i++)
  179. {
  180. if (i % 8 == 7)
  181. i++;
  182. else
  183. if (proverka(i) || k < 12)
  184. {
  185. field1[i] = 1;
  186. attack(i);
  187. search(k+1, i+1);
  188. if (k == 12 && c == 64)
  189. prinfields();
  190. field1[i] = 0;
  191. unattack(i);
  192. }
  193. else
  194. break;
  195. }
  196. }
  197. }
  198.  
  199.  
  200. int _tmain(int argc, _TCHAR* argv[])
  201. {
  202. for (int i = 0; i < 64; i++)
  203. {
  204. field1[i] = 0;
  205. field2[i] = 0;
  206. }
  207.  
  208.  
  209. search(1, 9);
  210.  
  211. system("pause");
  212. return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement