Guest User

Untitled

a guest
Apr 3rd, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. #include <stdio.h>
  2. //#include <conio.h>
  3. #include <time.h>
  4. //#include <disp.h>
  5. //
  6. #define MAX_ROW 8
  7. #define MAX_COL 8
  8. #define MAX_HORSE_STEPS 8
  9. #define HORSE_ROW 2
  10. #define HORSE_COL 1
  11. //
  12. int Board[MAX_ROW][MAX_COL] = {0};
  13. int maxPos = (MAX_ROW * MAX_COL);
  14. int AvalSteps[8][2];
  15. int currentPos = 0;
  16. long currentTry = 1;
  17. //
  18. void fn_show_current_board(void);
  19. void fn_next_step(int, int);
  20. int fn_check_closest_cells(void);
  21. int fn_next_cell(int *, int *);
  22. //
  23. /*--------------------------------------------------
  24. */
  25. int main()
  26. {
  27. time_t start_time;
  28. time_t end_time;
  29. int ir, firt_r = 0;
  30. int ic, firt_c = 0;
  31. char pkey;
  32. //
  33. // Возможные шаги коня
  34. AvalSteps[0][0] = -HORSE_ROW;
  35. AvalSteps[0][1] = +HORSE_COL;
  36. AvalSteps[1][0] = -HORSE_COL;
  37. AvalSteps[1][1] = +HORSE_ROW;
  38. AvalSteps[2][0] = +HORSE_COL;
  39. AvalSteps[2][1] = +HORSE_ROW;
  40. AvalSteps[3][0] = +HORSE_ROW;
  41. AvalSteps[3][1] = +HORSE_COL;
  42. AvalSteps[4][0] = +HORSE_ROW;
  43. AvalSteps[4][1] = -HORSE_COL;
  44. AvalSteps[5][0] = +HORSE_COL;
  45. AvalSteps[5][1] = -HORSE_ROW;
  46. AvalSteps[6][0] = -HORSE_COL;
  47. AvalSteps[6][1] = -HORSE_ROW;
  48. AvalSteps[7][0] = -HORSE_ROW;
  49. AvalSteps[7][1] = -HORSE_COL;
  50. //
  51. repeat_more:
  52. //disp_setmode(3);
  53. //disp_open();
  54. //disp_setattr(0x02);
  55. //
  56. time(&start_time);
  57. while (currentPos < maxPos)
  58. {
  59. fn_next_step(firt_r, firt_c);
  60. if (currentPos < maxPos)
  61. {
  62. if (!fn_next_cell(&firt_r, &firt_c))
  63. break;
  64. }
  65. }
  66. time(&end_time);
  67. //
  68. //disp_close();
  69. //
  70. printf("Ok!\n");
  71. printf("Start time: %d : %s", (long)start_time, ctime(&start_time));
  72. printf("End time: %d : %s", (long)end_time, ctime(&end_time));
  73. printf("Total seconds: %d\n", (long)(end_time - start_time));
  74. printf("Do it in next cell? (Y/N)");
  75. pkey = getchar();
  76. if (pkey == 'Y' || pkey == 'y')
  77. {
  78. for (ir = 0; ir < MAX_ROW; ir++)
  79. {
  80. for (ic = 0; ic < MAX_COL; ic++)
  81. {
  82. Board[ir][ic] = 0;
  83. }
  84. }
  85. currentPos = 0;
  86. currentTry = 1;
  87. if (fn_next_cell(&firt_r, &firt_c))
  88. {
  89. goto repeat_more;
  90. }
  91. else
  92. {
  93. printf("This is the end cell\n");
  94. getchar();
  95. }
  96. }
  97. return 0;
  98. }
  99. /*--------------------------------------------------
  100. */
  101. void fn_next_step(int init_r, int init_c)
  102. {
  103. int step;
  104. //
  105. currentTry++;
  106. // проверка выхода за края доски
  107. if ((init_r >= 0) && (init_c >= 0) && (init_r < MAX_ROW) && (init_c < MAX_COL))
  108. {
  109. // проверка свободного, еще не посещенного поля
  110. if (Board[init_r][init_c] == 0)
  111. {
  112. // проверка отсутствия запертых клеток (или предпоследнего шага)
  113. if (currentPos == (maxPos - 1) || fn_check_closest_cells() == 1)
  114. {
  115. // проверка количества пройденных клето
  116. if (currentPos < maxPos)
  117. {
  118. ++currentPos;
  119. Board[init_r][init_c] = currentPos;
  120. fn_show_current_board();
  121. for (step = 0; step < MAX_HORSE_STEPS; step++)
  122. {
  123. fn_next_step(init_r + AvalSteps[step][0], init_c + AvalSteps[step][1]);
  124. }
  125. if (currentPos < maxPos)
  126. {
  127. --currentPos;
  128. Board[init_r][init_c] = 0;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. /*--------------------------------------------------
  136. */
  137. void fn_show_current_board(void)
  138. {
  139. int ir, ic;
  140. if ((currentTry >= 1000) || (currentPos >= maxPos))
  141. {
  142. currentTry = 1;
  143. //disp_move(0, 0);
  144. //disp_printf(" > %d\n", currentPos);
  145. printf(" > %d\n", currentPos);
  146. for (ir = 0; ir < MAX_ROW; ir++)
  147. {
  148. for (ic = 0; ic < MAX_COL; ic++)
  149. {
  150. //disp_printf("| %2d ", Board[ir][ic]);
  151. printf("| %2d ", Board[ir][ic]);
  152. }
  153. //disp_printf("|\n");
  154. printf("|\n");
  155. for (ic = 0; ic < MAX_COL; ic++)
  156. {
  157. //disp_printf("-----");
  158. printf("-----");
  159. };
  160. //disp_printf("-\n");
  161. printf("-\n");
  162. }
  163. /*
  164. if (kbhit())
  165. {
  166. if (getch() == 'q')
  167. {
  168. currentPos = (maxPos + 1);
  169. }
  170. }
  171. */
  172. }
  173. }
  174. /*--------------------------------------------------
  175. */
  176. int fn_check_closest_cells(void)
  177. {
  178. int ir, ic, step, ch_r, ch_c, ok_flag;
  179. for (ir = 0; ir < MAX_ROW; ir++)
  180. {
  181. for (ic = 0; ic < MAX_COL; ic++)
  182. {
  183. if (Board[ir][ic] == 0)
  184. {
  185. ok_flag = 0;
  186. for (step = 0; step < MAX_HORSE_STEPS; step++)
  187. {
  188. ch_r = ir + AvalSteps[step][0];
  189. ch_c = ic + AvalSteps[step][1];
  190. if ((ch_r >= 0) && (ch_c >= 0) && (ch_r < MAX_ROW) && (ch_c < MAX_COL))
  191. {
  192. if (Board[ch_r][ch_c] == 0)
  193. {
  194. ok_flag = 1;
  195. break;
  196. }
  197. }
  198. }
  199. if (ok_flag == 0)
  200. {
  201. return 0;
  202. }
  203. }
  204. }
  205. }
  206. return 1;
  207. }
  208. /*--------------------------------------------------
  209. */
  210. int fn_next_cell(int *r, int *c)
  211. {
  212. (*r)++;
  213. if ((*r) > (MAX_ROW - 1))
  214. {
  215. (*r) = 0;
  216. (*c)++;
  217. if ((*c) > (MAX_COL - 1))
  218. {
  219. return 0;
  220. }
  221. }
  222. return 1;
  223. }
  224. /*--------------------------------------------------
  225. */
  226.  
Advertisement
Add Comment
Please, Sign In to add comment