SHARE
TWEET

Knight on tour

KgCro May 29th, 2020 521 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6.  
  7. #ifndef DEBUG
  8. #define DEBUG(...) printf(__VA_ARGS__)
  9. #endif
  10.  
  11. #define MAXN 10000
  12.  
  13. char dc[4] = "UDLR";
  14. int dir[2][4] = {{-1,  1,  0,  0}, // za up -1, down 1 ostali smjerovi po redovima 0
  15.                  { 0,  0, -1,  1}};// za up i down po stupcima 0 ,a left -1 , right 1
  16.  
  17. int min(int a, int b) {
  18.   return a < b ? a : b;
  19. }
  20.  
  21. int max(int a, int b) {
  22.   return a > b ? a : b;
  23. }
  24.  
  25. void ispis(char **ans, int h, int w) {
  26.   for (int i = 0; i < h; ++i) {
  27.     for (int j = 0; j < w; ++j) {
  28.       printf("%c", ans[i][j]);
  29.     }
  30.     printf("\n");
  31.   }
  32. }
  33.  
  34. int main() {
  35.   int n, h, w, rmn = 0, rmx = 0, smn = 0, smx = 0, cur[2] = {}, tour[MAXN+1][2] = {};
  36.   // deklarirati varijablu ans za dinamički alocirano 2d polje
  37.   char **ans;
  38.  
  39.   scanf("%d", &n);
  40.   for (int i = 1; i <= n; ++i) {
  41.     char p[2];
  42.     scanf(" %c%c", &p[0], &p[1]);
  43.     for (int mv = 0; mv < 2; ++mv) {
  44.       for (int di = 0; di < 4; ++di) {
  45.         if (p[mv]==dc[di]) {
  46.           for (int dim = 0; dim < 2; ++dim) {
  47.             cur[dim] += (mv==0 ? 2 : 1)*dir[dim][di];
  48.           }
  49.         }
  50.       }
  51.     }
  52.     tour[i][0] = cur[0];
  53.     tour[i][1] = cur[1];
  54.     rmn = min(rmn, cur[0]);
  55.     rmx = max(rmx, cur[0]);
  56.     smn = min(smn, cur[1]);
  57.     smx = max(smx, cur[1]);
  58.   }
  59.   h = rmx-rmn+1;
  60.   w = smx-smn+1;
  61.   // alocirati polje dovoljnih dimenzija (h * w) i postaviti sva polja na '.'
  62.   ans = malloc(h * sizeof(char *));
  63.   for (int i = 0; i < h; ++i)
  64.   {
  65.     ans[i] = malloc(w * sizeof(char));
  66.     memset(ans[i], '.', w * sizeof(char));
  67.   }
  68.   // unijeti pozicije
  69.   for (int i = 0; i <= n; ++i)
  70.   {
  71.     ans[tour[i][0] - rmn][tour[i][1] - smn] = 'x'; //zasto -rmn i -smn ???????
  72.   }
  73.  
  74.   // ispisati polje: pozvati gotovu funkciju ispis.
  75.   ispis(ans, h, w);
  76.   // osloboditi memoriju
  77.   for (int i = 0; i < h; ++i)
  78.   {
  79.     free(ans[i]);
  80.   }
  81.   free(ans);
  82.   return 0;
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top