Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define n_base 8
  4. #define n_offset 2
  5. #define N n_base + 2*n_offset
  6.  
  7. typedef enum {
  8.     false, true
  9. } bool;
  10.  
  11. typedef struct {
  12.     int x;
  13.     int y;
  14. } point_t;
  15.  
  16. void initMap(int map[N][N]);
  17. void searchPath(int map[N][N]);
  18. void princ_molt(int pos, int map[N][N], int val[8][2], int n_choise, bool *start, point_t *currPos, point_t *newPos);
  19. void delMove(int map[N][N], point_t *currPos, int *to_del);
  20. bool setMove(int map[N][N], point_t *currPos, point_t *newPos);
  21. bool checkMove(int map[N][N], point_t *currPos, point_t *newPos);
  22. bool isFree(int map[N][N], point_t *currPos, point_t *newPos);
  23.  
  24. int main() {
  25.     int map[N][N];
  26.  
  27.     /*init*/
  28.     initMap(map);
  29.  
  30.     searchPath(map);
  31.  
  32.     return 0;
  33. }
  34.  
  35. void initMap(int map[N][N]) {
  36.     int i, j;
  37.  
  38.     for (i = 0; i < N; i++)
  39.         for (j = 0; j < N; j++) {
  40.             if (j < n_offset || j >(n_base + 1) || i < n_offset || i > (n_base + 1))
  41.                 map[i][j] = -2;
  42.             else
  43.                 map[i][j] = 0;
  44.         }
  45. }
  46.  
  47. void searchPath(int map[N][N]) {
  48.     /*1st column: y move.
  49.       2nd column: x move.*/
  50.     int val[8][2] = {
  51.         {+2, +1},
  52.         {+2, -1},
  53.         {+1, +2},
  54.         {+1, -2},
  55.         {-2, +1},
  56.         {-2, -1},
  57.         {-1, +2},
  58.         {-1, -2} };
  59.     int n_choise = 8;
  60.     point_t currPos, newPos;
  61.     bool start = 1;
  62.  
  63.     currPos.x = 2;
  64.     currPos.y = 2;
  65.     newPos.x = 0;
  66.     newPos.y = 0;
  67.  
  68.     map[currPos.y][currPos.x] = 1;
  69.  
  70.     princ_molt(0, map, val, n_choise, &start, &currPos, &newPos);
  71. }
  72.  
  73. void princ_molt(int pos, int map[N][N], int val[8][2], int n_choise, bool *start, point_t *currPos, point_t *newPos) {
  74.     int i, j;
  75.  
  76.     if (pos >= 62)
  77.         printf( "%d ", pos);
  78.  
  79.     if (pos >= 63) {
  80.         printf("\n\n");
  81.         for (i = 2; i < 10; i++) {
  82.             for (j = 2; j < 10; j++)
  83.                 printf("[ %d ] ", map[i][j]);
  84.             printf("\n");
  85.         }
  86.         return;
  87.     }
  88.  
  89.     if (*start)
  90.         *start = false;
  91.  
  92.     for (i = 0; i < n_choise; i++) {
  93.         newPos->x = val[i][1];
  94.         newPos->y = val[i][0];
  95.         if (isFree(map, currPos, newPos)) {
  96.             if (setMove(map, currPos, newPos)) {
  97.                 princ_molt(pos + 1, map, val, n_choise, start, currPos, newPos);
  98.  
  99.                 delMove(map, currPos, val[i]); //backtrack
  100.             }
  101.         }
  102.     }
  103.  
  104. }
  105.  
  106. void delMove(int map[N][N], point_t *currPos, int *to_del) {
  107.    
  108.     map[currPos->y][currPos->x] = 0;
  109.     currPos->y -= to_del[0];
  110.     currPos->x -= to_del[1];
  111. }
  112.  
  113. bool setMove(int map[N][N], point_t *currPos, point_t *newPos) {
  114.  
  115.     if (checkMove(map, currPos, newPos)) {
  116.         map[currPos->y + newPos->y][currPos->x + newPos->x] = map[currPos->y][currPos->x] + 1;
  117.         currPos->y += newPos->y;
  118.         currPos->x += newPos->x;
  119.         return true;
  120.     }
  121.  
  122.     return false;
  123. }
  124.  
  125. bool checkMove(int map[N][N], point_t *currPos, point_t *newPos) {
  126.     return (map[currPos->y + newPos->y][currPos->x + newPos->x] == 0);
  127. }
  128.  
  129. bool isFree(int map[N][N], point_t *currPos, point_t *newPos) {
  130.     int x = currPos->x + newPos->x;
  131.     int y = currPos->y + newPos->y;
  132.  
  133.     if ((y < 0 + n_offset || y >= N - n_offset) || (x < 0 + n_offset || x >= N - n_offset))
  134.         return false;
  135.  
  136.     return true;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement