Advertisement
Guest User

Lundi 7

a guest
Jan 15th, 2015
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4.  
  5. #undef uint
  6. #define uint unsigned int
  7.  
  8. static inline void zero_buf(uint **buf, uint w, uint h) {
  9.  for(uint i=0; i<h; i++)
  10.   memset(buf[i], '\0', w * sizeof(**buf));
  11. }
  12.  
  13. static inline void free_buf(void *_buf, uint h) {
  14.  char **buf = (char **)_buf;
  15.  if(!buf) return;
  16.  for(uint i=0; i<h; i++)
  17.   free(buf[i]);
  18.  free(buf);
  19. }
  20.  
  21. static inline void *alloc_zero_buf(uint w, uint h, uint elem_len) {
  22.  char **buf;
  23.  
  24.  buf = malloc(h * sizeof(void *));
  25.  if(!buf) return NULL;
  26.  
  27.  for(uint i=0; i<h; i++) {
  28.   buf[i] = malloc(w * elem_len);
  29.   if(!buf[i]) goto fail_free;
  30.   memset(buf[i], '\0', w * elem_len);
  31.  }
  32.  return buf;
  33.  
  34. fail_free:
  35.  for(uint i=0; buf[i] && i<h; i++)
  36.   free(buf[i]);
  37.  free(buf);
  38.  return NULL;
  39. }
  40.  
  41. static inline char **read_input_buf(uint w, uint h) {
  42.  char **buf;
  43.  
  44.  buf = alloc_zero_buf(w+2, h, sizeof(**buf)); /* w+2 for "\n\0" */
  45.  if(!buf) {
  46.   fprintf(stderr, "E: Alloc input buffer.\n");
  47.   return NULL;
  48.  }
  49.  
  50.  for(uint i=0; i<h; i++) {
  51.   if(feof(stdin) || ferror(stdin)) {
  52.    fprintf(stderr, "E: Incomplete buffer.\n");
  53.    goto fail_free;
  54.   }
  55.   fgets(buf[i], w+2, stdin);
  56.   if(strlen(buf[i]) != w+1) {
  57.    fprintf(stderr, "E: Incomplete buffer line.\n");
  58.    goto fail_free;
  59.   }
  60.  }
  61.  return buf;
  62.  
  63. fail_free:
  64.  free_buf(buf, h);
  65.  return NULL;
  66. }
  67.  
  68. static inline uint draw(char **data_buf, uint **draw_buf, uint **depth_buf, uint y, uint x, uint w, uint h) {
  69.  uint count = 1;
  70.  
  71.  if(depth_buf[y][x]) return 0;
  72.  zero_buf(draw_buf, w, h);
  73.  
  74.  while(!depth_buf[y][x]) {
  75.   depth_buf[y][x] = 1;
  76.   draw_buf[y][x] = count++;
  77.  
  78.   switch(data_buf[y][x]) {
  79.    case '<': x = (!x) ? w-1 : x-1; break;
  80.    case '^': y = (!y) ? h-1 : y-1; break;
  81.    case '>': x = (x == w-1) ? 0 : x+1; break;
  82.    case 'v': y = (y == h-1) ? 0 : y+1; break;
  83.    default:
  84.     depth_buf[y][x] = 0;
  85.     fprintf(stderr, "E: Invalid character at (%u,%u): '%c'.\n", x, y, data_buf[y][x]);
  86.     return -1;
  87.   }
  88.  }
  89.  if(!draw_buf[y][x]) return 0;
  90.  return count-draw_buf[y][x];
  91. }
  92.  
  93. int main(int argc, char **argv)
  94. {
  95.  int res = -1;
  96.  uint w = 0, h = 0;
  97.  uint max_len = 0;
  98.  char **data_buf = NULL;
  99.  uint **draw_buf = NULL;
  100.  uint **depth_buf = NULL;
  101.  
  102.  if(fscanf(stdin, "%u %u", &w, &h) != 2 || !w || !h) {
  103.   fprintf(stderr, "E: Invalid size specified.\n");
  104.   return -1;
  105.  }
  106.  fgetc(stdin); /* Trailing \n */
  107.  
  108.  data_buf = read_input_buf(w, h);
  109.  if(!data_buf) {
  110.   fprintf(stderr, "E: Read input buffer.\n");
  111.   goto free_exit;
  112.  }
  113.  
  114.  draw_buf = alloc_zero_buf(w, h, sizeof(**draw_buf));
  115.  if(!draw_buf) {
  116.   fprintf(stderr, "E: Alloc draw buffer.\n");
  117.   goto free_exit;
  118.  }
  119.  
  120.  depth_buf = alloc_zero_buf(w, h, sizeof(**depth_buf));
  121.  if(!depth_buf) {
  122.   fprintf(stderr, "E: Alloc depth buffer.\n");
  123.   goto free_exit;
  124.  }
  125.  
  126.  for(uint i=0; i<h; i++) {
  127.   for(uint j=0; j<w; j++) {
  128.    uint new_len = 0;
  129.    new_len = draw(data_buf, draw_buf, depth_buf, i, j, w, h);
  130.    if(new_len < 0)
  131.     goto free_exit;
  132.    if(new_len > max_len)
  133.     max_len = new_len;
  134.   }
  135.  }
  136.  fprintf(stdout, "Max loop: %u.\n", max_len);
  137.  
  138.  res = 0;
  139.  
  140. free_exit:
  141.  free_buf(data_buf, h);
  142.  free_buf(draw_buf, h);
  143.  free_buf(depth_buf, h);
  144.  return res;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement