Computer Programming Contest Preparation

ToolBox - Source for: 102/10285/a.c



/home/toolbox/public_html/solutions/102/10285/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (TRUE)
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2023-11-14
   21  * Purpose: fun
   22  * Problem: 10285
   23  */
   24 
   25 /*
   26  * This template reads data a specified number of times.
   27  */
   28 
   29 #define MAX_SIZE 102
   30 #define MAX_STRING 1024
   31 #define MAX_HEIGHT 200
   32 
   33 int numberOfTimes;
   34 char name[MAX_STRING];
   35 int ary[MAX_SIZE][MAX_SIZE]; /* where heights are */
   36 int ans[MAX_SIZE][MAX_SIZE]; /* answers for length of ride */
   37 int ip[MAX_SIZE][MAX_SIZE];  /* mark places being visited */
   38 int r;
   39 int c;
   40 
   41 void init()
   42 {
   43     /* FUNCTION init */
   44     scanf("%d ", &numberOfTimes);
   45 } /* FUNCTION init */
   46 
   47 void dump()
   48 {
   49     /* FUNCTION dump */
   50 } /* FUNCTION dump */
   51 
   52 void getInput()
   53 {
   54     /* FUNCTION getInput */
   55     int i;
   56     int j;
   57 
   58     scanf("%s %d %d ", name, &r, &c);
   59     for (i=0; i<r; i++)
   60         {
   61             /* each row */
   62             for (j=0; j<c; j++)
   63                 {
   64                     /* fpor each column */
   65                     scanf(" %d ", &ary[i][j]);
   66                     ans[i][j] = 0;
   67                     ip[i][j] = 0;
   68                 } /* fpor each column */
   69         } /* each row */
   70 } /* FUNCTION getInput */
   71 
   72 int check(int row, int col, int high)
   73 {
   74     /* FUNCTION check */
   75     int toReturn = 0;
   76     int tmp;
   77 
   78     if ((0 > row) || (r <= row) || (0 > col) || (c <= col))
   79         {
   80             /* invlaid index */
   81             toReturn = 0;
   82             DEBUG printf("check %d/%d %d/%d (high %d) invalid index\n", row, r, col, c, high);
   83         } /* invlaid index */
   84     else if (0 < ip[row][col])
   85         {
   86             /* already being/been checked */
   87             toReturn = 0;
   88             DEBUG printf("check %d/%d %d/%d (high %d) (ary %d) (ans %d) (ip %d) already visited\n", row, r, col, c, high, ary[row][col], ans[row][col], ip[row][col]);
   89         } /* already being/been checked */
   90     else
   91         {
   92             /* check here */
   93             DEBUG printf("check %d/%d %d/%d (high %d) (ary %d) (ans %d) (ip %d)\n", row, r, col, c, high, ary[row][col], ans[row][col], ip[row][col]);
   94             if (high <= ary[row][col])
   95                 {
   96                     /* invalid move - return 0 */
   97                     toReturn = 0;
   98                 } /* invalid move - return 0 */
   99             else if (0 < ans[row][col])
  100                 {
  101                     /* found an answer  */
  102                     toReturn = ans[row][col];
  103                 } /* found an answer  */
  104             else
  105                 {
  106                     /* check the four arouond */
  107                     ip[row][col] = 1;
  108                     /* check above */
  109                     tmp = 1 + check(row - 1, col, ary[row][col]);
  110                     toReturn = (tmp > toReturn) ? tmp : toReturn; /* get biggest number */
  111                     /* check column to left */
  112                     tmp = 1 + check(row, col - 1, ary[row][col]);
  113                     toReturn = (tmp > toReturn) ? tmp : toReturn; /* get biggest number */
  114                     /* check row below */
  115                     tmp = 1 + check(row + 1, col, ary[row][col]);
  116                     toReturn = (tmp > toReturn) ? tmp : toReturn; /* get biggest number */
  117                     /* check column to right */
  118                     tmp = 1 + check(row, col + 1, ary[row][col]);
  119                     toReturn = (tmp > toReturn) ? tmp : toReturn; /* get biggest number */
  120                 } /* check the four arouond */
  121         } /* check here */
  122     if (toReturn > ans[row][col])
  123         {
  124             /* found a bigger answeer */
  125             ans[row][col] = toReturn;
  126             DEBUG printf("Setting ans[%d][%d] to %d\n", row, col, toReturn);
  127         } /* found a bigger answeer */
  128     else
  129         DEBUG printf("bailing on %d %d\n", row, col);
  130     return toReturn;
  131 } /* FUNCTION check */
  132 
  133 void process()
  134 {
  135     /* FUNCTION process */
  136     /* i,r  j,c */
  137     int i;
  138     int j;
  139     int tmp;
  140     int maxx = 0;
  141 
  142     DEBUG printf("process %s %d %d\n", name, r, c);
  143     for (i=0; i<r; i++)
  144         {
  145             /* each row */
  146             for (j=0; j<c; j++)
  147                 {
  148                     /* for each column */
  149                     DEBUG printf("Calling check: %d %d\n", i, j);
  150                     tmp = check(i, j, MAX_HEIGHT);
  151                     if (tmp > maxx)
  152                         {
  153                             maxx = tmp;
  154                         }
  155                 } /* for each column */
  156         } /* each row */
  157     printf("%s: %d\n", name, maxx);
  158 } /* FUNCTION process */
  159 
  160 int main()
  161 {
  162     /* main */
  163     int i;
  164 
  165     init();
  166     for (i=0; i<numberOfTimes; i++)
  167         {
  168             /* while */
  169             getInput();
  170             process();
  171         } /* while */
  172 
  173     return EXIT_SUCCESS;
  174 } /* main */
  175 
  176