Advertisement
Lexolordan

Untitled

Dec 8th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <sys/types.h>
  3. #include <sys/mman.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <stdint.h>
  10. #include <unistd.h>
  11. #include <sys/types.h>
  12. #include <stdlib.h>
  13.  
  14. int main(int argc, char *argv[]) {
  15.     char *file_name = argv[1];
  16.     int32_t N = strtol(argv[2], NULL, 10);
  17.     int32_t W = strtol(argv[3], NULL, 10);
  18.  
  19.     size_t length = N * N * W + N;
  20.     int fd = open(file_name, O_RDWR | O_CREAT, 0777);
  21.     ftruncate(fd, length);
  22.  
  23.     struct stat st;
  24.     fstat(fd, &st);
  25.     char *contents = mmap(NULL,
  26.                           st.st_size,
  27.                           PROT_READ | PROT_WRITE,
  28.                           MAP_SHARED,
  29.                           fd,
  30.                           0);
  31. memset(contents, ' ', st.st_size);
  32.     int *arr;
  33.     arr = (int*)malloc(N * N * sizeof(int));
  34.     for (int i = 0; i < N; i++) {
  35.         for (int j = 0; j < N; j++) {
  36.             arr[i * N + j] = 0;
  37.         }
  38.     }
  39.     int count = 1;
  40.     int i = 0;
  41.     int j = 0;
  42.     int d = 3;
  43.  
  44.     while(count != N * N + 1) {
  45.         if (i != N && i != -1 && j != N && j != -1 && arr[i * N + j] == 0) {
  46.             char *cur = contents + i * N * W + j * W + i;
  47.             arr[i * N + j] = 1;
  48.             char *strs = malloc(100 * sizeof(char));
  49.             snprintf(strs, W + 1, "%d", count);
  50.             sprintf(cur + W - strlen(strs), "%s", strs);
  51.         free(strs);
  52.             count++;
  53.             if (d == 0) {
  54.                 if (i + 1 == N || arr[(i + 1) * N + j] == 1) {
  55.                     d = 1;
  56.                     j--;
  57.                 } else {
  58.                     i++;
  59.                 }
  60.             } else if (d == 1) {
  61.                 if (j - 1 == -1 || arr[i * N + j - 1] == 1) {
  62.                     d = 2;
  63.                     i--;
  64.                 } else {
  65.                     j--;
  66.                 }
  67.             } else if (d == 2) {
  68.                 if (i - 1 == -1 || arr[(i - 1) * N + j] == 1) {
  69.                     d = 3;
  70.                     j++;
  71.                 } else {
  72.                     i--;
  73.                 }
  74.             } else if (d == 3) {
  75.                 if (j + 1 == N || arr[i * N + j + 1] == 1) {
  76.                     d = 0;
  77.                     i++;
  78.                 } else {
  79.                     j++;
  80.                 };
  81.             }
  82.         }
  83.     }
  84.     for (int i = 0; i < N; ++i) {
  85.     *(contents + N * W + i * N * W + i) = '\n';
  86.     }
  87.     free(arr);
  88. finally:
  89.     munmap(contents, st.st_size);
  90.     close(fd);
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement