Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- //#include <conio.h>
- #include <time.h>
- //#include <disp.h>
- //
- #define MAX_ROW 8
- #define MAX_COL 8
- #define MAX_HORSE_STEPS 8
- #define HORSE_ROW 2
- #define HORSE_COL 1
- //
- int Board[MAX_ROW][MAX_COL] = {0};
- int maxPos = (MAX_ROW * MAX_COL);
- int AvalSteps[8][2];
- int currentPos = 0;
- long currentTry = 1;
- //
- void fn_show_current_board(void);
- void fn_next_step(int, int);
- int fn_check_closest_cells(void);
- int fn_next_cell(int *, int *);
- //
- /*--------------------------------------------------
- */
- int main()
- {
- time_t start_time;
- time_t end_time;
- int ir, firt_r = 0;
- int ic, firt_c = 0;
- char pkey;
- //
- // Возможные шаги коня
- AvalSteps[0][0] = -HORSE_ROW;
- AvalSteps[0][1] = +HORSE_COL;
- AvalSteps[1][0] = -HORSE_COL;
- AvalSteps[1][1] = +HORSE_ROW;
- AvalSteps[2][0] = +HORSE_COL;
- AvalSteps[2][1] = +HORSE_ROW;
- AvalSteps[3][0] = +HORSE_ROW;
- AvalSteps[3][1] = +HORSE_COL;
- AvalSteps[4][0] = +HORSE_ROW;
- AvalSteps[4][1] = -HORSE_COL;
- AvalSteps[5][0] = +HORSE_COL;
- AvalSteps[5][1] = -HORSE_ROW;
- AvalSteps[6][0] = -HORSE_COL;
- AvalSteps[6][1] = -HORSE_ROW;
- AvalSteps[7][0] = -HORSE_ROW;
- AvalSteps[7][1] = -HORSE_COL;
- //
- repeat_more:
- //disp_setmode(3);
- //disp_open();
- //disp_setattr(0x02);
- //
- time(&start_time);
- while (currentPos < maxPos)
- {
- fn_next_step(firt_r, firt_c);
- if (currentPos < maxPos)
- {
- if (!fn_next_cell(&firt_r, &firt_c))
- break;
- }
- }
- time(&end_time);
- //
- //disp_close();
- //
- printf("Ok!\n");
- printf("Start time: %d : %s", (long)start_time, ctime(&start_time));
- printf("End time: %d : %s", (long)end_time, ctime(&end_time));
- printf("Total seconds: %d\n", (long)(end_time - start_time));
- printf("Do it in next cell? (Y/N)");
- pkey = getchar();
- if (pkey == 'Y' || pkey == 'y')
- {
- for (ir = 0; ir < MAX_ROW; ir++)
- {
- for (ic = 0; ic < MAX_COL; ic++)
- {
- Board[ir][ic] = 0;
- }
- }
- currentPos = 0;
- currentTry = 1;
- if (fn_next_cell(&firt_r, &firt_c))
- {
- goto repeat_more;
- }
- else
- {
- printf("This is the end cell\n");
- getchar();
- }
- }
- return 0;
- }
- /*--------------------------------------------------
- */
- void fn_next_step(int init_r, int init_c)
- {
- int step;
- //
- currentTry++;
- // проверка выхода за края доски
- if ((init_r >= 0) && (init_c >= 0) && (init_r < MAX_ROW) && (init_c < MAX_COL))
- {
- // проверка свободного, еще не посещенного поля
- if (Board[init_r][init_c] == 0)
- {
- // проверка отсутствия запертых клеток (или предпоследнего шага)
- if (currentPos == (maxPos - 1) || fn_check_closest_cells() == 1)
- {
- // проверка количества пройденных клето
- if (currentPos < maxPos)
- {
- ++currentPos;
- Board[init_r][init_c] = currentPos;
- fn_show_current_board();
- for (step = 0; step < MAX_HORSE_STEPS; step++)
- {
- fn_next_step(init_r + AvalSteps[step][0], init_c + AvalSteps[step][1]);
- }
- if (currentPos < maxPos)
- {
- --currentPos;
- Board[init_r][init_c] = 0;
- }
- }
- }
- }
- }
- }
- /*--------------------------------------------------
- */
- void fn_show_current_board(void)
- {
- int ir, ic;
- if ((currentTry >= 1000) || (currentPos >= maxPos))
- {
- currentTry = 1;
- //disp_move(0, 0);
- //disp_printf(" > %d\n", currentPos);
- printf(" > %d\n", currentPos);
- for (ir = 0; ir < MAX_ROW; ir++)
- {
- for (ic = 0; ic < MAX_COL; ic++)
- {
- //disp_printf("| %2d ", Board[ir][ic]);
- printf("| %2d ", Board[ir][ic]);
- }
- //disp_printf("|\n");
- printf("|\n");
- for (ic = 0; ic < MAX_COL; ic++)
- {
- //disp_printf("-----");
- printf("-----");
- };
- //disp_printf("-\n");
- printf("-\n");
- }
- /*
- if (kbhit())
- {
- if (getch() == 'q')
- {
- currentPos = (maxPos + 1);
- }
- }
- */
- }
- }
- /*--------------------------------------------------
- */
- int fn_check_closest_cells(void)
- {
- int ir, ic, step, ch_r, ch_c, ok_flag;
- for (ir = 0; ir < MAX_ROW; ir++)
- {
- for (ic = 0; ic < MAX_COL; ic++)
- {
- if (Board[ir][ic] == 0)
- {
- ok_flag = 0;
- for (step = 0; step < MAX_HORSE_STEPS; step++)
- {
- ch_r = ir + AvalSteps[step][0];
- ch_c = ic + AvalSteps[step][1];
- if ((ch_r >= 0) && (ch_c >= 0) && (ch_r < MAX_ROW) && (ch_c < MAX_COL))
- {
- if (Board[ch_r][ch_c] == 0)
- {
- ok_flag = 1;
- break;
- }
- }
- }
- if (ok_flag == 0)
- {
- return 0;
- }
- }
- }
- }
- return 1;
- }
- /*--------------------------------------------------
- */
- int fn_next_cell(int *r, int *c)
- {
- (*r)++;
- if ((*r) > (MAX_ROW - 1))
- {
- (*r) = 0;
- (*c)++;
- if ((*c) > (MAX_COL - 1))
- {
- return 0;
- }
- }
- return 1;
- }
- /*--------------------------------------------------
- */
Advertisement
Add Comment
Please, Sign In to add comment