Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifndef DEBUG
- #define DEBUG(...) printf(__VA_ARGS__)
- #endif
- #define MAXN 10000
- char dc[4] = "UDLR";
- int dir[2][4] = {{-1, 1, 0, 0}, // za up -1, down 1 ostali smjerovi po redovima 0
- { 0, 0, -1, 1}};// za up i down po stupcima 0 ,a left -1 , right 1
- int min(int a, int b) {
- return a < b ? a : b;
- }
- int max(int a, int b) {
- return a > b ? a : b;
- }
- void ispis(char **ans, int h, int w) {
- for (int i = 0; i < h; ++i) {
- for (int j = 0; j < w; ++j) {
- printf("%c", ans[i][j]);
- }
- printf("\n");
- }
- }
- int main() {
- int n, h, w, rmn = 0, rmx = 0, smn = 0, smx = 0, cur[2] = {}, tour[MAXN+1][2] = {};
- // deklarirati varijablu ans za dinamički alocirano 2d polje
- char **ans;
- scanf("%d", &n);
- for (int i = 1; i <= n; ++i) {
- char p[2];
- scanf(" %c%c", &p[0], &p[1]);
- for (int mv = 0; mv < 2; ++mv) {
- for (int di = 0; di < 4; ++di) {
- if (p[mv]==dc[di]) {
- for (int dim = 0; dim < 2; ++dim) {
- cur[dim] += (mv==0 ? 2 : 1)*dir[dim][di];
- }
- }
- }
- }
- tour[i][0] = cur[0];
- tour[i][1] = cur[1];
- rmn = min(rmn, cur[0]);
- rmx = max(rmx, cur[0]);
- smn = min(smn, cur[1]);
- smx = max(smx, cur[1]);
- }
- h = rmx-rmn+1;
- w = smx-smn+1;
- // alocirati polje dovoljnih dimenzija (h * w) i postaviti sva polja na '.'
- ans = malloc(h * sizeof(char *));
- for (int i = 0; i < h; ++i)
- {
- ans[i] = malloc(w * sizeof(char));
- memset(ans[i], '.', w * sizeof(char));
- }
- // unijeti pozicije
- for (int i = 0; i <= n; ++i)
- {
- ans[tour[i][0] - rmn][tour[i][1] - smn] = 'x'; //zasto -rmn i -smn ???????
- }
- // ispisati polje: pozvati gotovu funkciju ispis.
- ispis(ans, h, w);
- // osloboditi memoriju
- for (int i = 0; i < h; ++i)
- {
- free(ans[i]);
- }
- free(ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement