Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/102/10285/b.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 (FALSE)
   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];
   36 int ans[MAX_SIZE][MAX_SIZE];
   37 int r;
   38 int c;
   39 
   40 void init()
   41 {
   42     /* FUNCTION init */
   43     scanf("%d ", &numberOfTimes);
   44 } /* FUNCTION init */
   45 
   46 void dump()
   47 {
   48     /* FUNCTION dump */
   49 } /* FUNCTION dump */
   50 
   51 void getInput()
   52 {
   53     /* FUNCTION getInput */
   54     int i;
   55     int j;
   56 
   57     scanf("%s %d %d ", name, &r, &c);
   58     for (i=0; i<r; i++)
   59         {
   60             /* each row */
   61             for (j=0; j<c; j++)
   62                 {
   63                     /* fpor each column */
   64                     scanf(" %d ", &ary[i][j]);
   65                     ans[i][j]=0;
   66                 } /* fpor each column */
   67         } /* each row */
   68 } /* FUNCTION getInput */
   69 
   70 int check(int row, int col, int high)
   71 {
   72     /* FUNCTION check */
   73     int toReturn = 0;
   74     int tmp;
   75 
   76     printf("check %d/%d %d/%d (high %d) (ary %d) (ans %d)\n", row, r, col, c, high, ary[row][col], ans[row][col]);
   77     if (high <= ary[row][col])
   78         {
   79             /* invalid move - return 0 */
   80             toReturn = 0;
   81         } /* invalid move - return 0 */
   82     else if (0 < ans[row][col])
   83         {
   84             /* found an answer  */
   85             toReturn = ans[row][col];
   86         } /* found an answer  */
   87     else
   88         {
   89             /* check the four arouond */
   90             if (0 < row)
   91                 {
   92                     /* check row above */
   93                     if (high > ary[row - 1][col])
   94                         {
   95                             /* hit end of ride */
   96                             tmp = 1;
   97                         } /* hit end of ride */
   98                     else
   99                         {
  100                             /* can go down */
  101                             tmp = 1 + check(row - 1, col, ary[row][col]);
  102                         } /* can go down */
  103                     toReturn = (tmp > toReturn) ? tmp : toReturn; /* get biggest number */
  104                 } /* check col above */
  105             if (0 < col)
  106                 {
  107                     /* check col to left */
  108                     if (high > ary[row][col - 1])
  109                         {
  110                             /* hit end of ride */
  111                             tmp = 1;
  112                         } /* hit end of ride */
  113                     else
  114                         {
  115                             /* can go down */
  116                             tmp = 1 + check(row, col - 1, ary[row][col]);
  117                         } /* can go down */
  118                     toReturn = (tmp > toReturn) ? tmp : toReturn; /* get biggest number */
  119                 } /* check row to left */
  120             if (r > row)
  121                 {
  122                     /* check row below */
  123                     if (high > ary[row + 1][col])
  124                         {
  125                             /* hit end of ride */
  126                             tmp = 1;
  127                         } /* hit end of ride */
  128                     else
  129                         {
  130                             /* can go down */
  131                             tmp = 1 + check(row + 1, col, ary[row][col]);
  132                         } /* can go down */
  133                     toReturn = (tmp > toReturn) ? tmp : toReturn; /* get biggest number */
  134                 } /* check row below */
  135             if (c > col)
  136                 {
  137                     /* check col to right */
  138                     if (high > ary[row][col + 1])
  139                         {
  140                             /* hit end of ride */
  141                             tmp = 1;
  142                         } /* hit end of ride */
  143                     else
  144                         {
  145                             /* can go down */
  146                             tmp = 1 + check(row, col + 1, ary[row][col]);
  147                         } /* can go down */
  148                     toReturn = (tmp > toReturn) ? tmp : toReturn; /* get biggest number */
  149                 } /* check row to right */
  150         } /* check the four arouond */
  151     ans[row][col] = toReturn;
  152     return toReturn;
  153 } /* FUNCTION check */
  154 
  155 void process()
  156 {
  157     /* FUNCTION process */
  158     /* i,r  j,c */
  159     int i;
  160     int j;
  161     int tmp;
  162     int maxx = 0;
  163 
  164     printf("process %s %d %d\n", name, r, c);
  165     for (i=0; i<r; i++)
  166         {
  167             /* each row */
  168             for (j=0; j<c; j++)
  169                 {
  170                     /* for each column */
  171                     if (0 == ans[i][j])
  172                         {
  173                             /* uncalculated value found */
  174                             printf("Calling check: %d %d\n", i, j);
  175                             tmp = check(i, j, MAX_HEIGHT);
  176                             if (tmp > maxx)
  177                                 {
  178                                     maxx = tmp;
  179                                 }
  180                         } /* uncalculated value found */
  181                 } /* for each column */
  182         } /* each row */
  183     printf("%s: %d\n", name, maxx);
  184 } /* FUNCTION process */
  185 
  186 int main()
  187 {
  188     /* main */
  189     int i;
  190 
  191     init();
  192     for (i=0; i<numberOfTimes; i++)
  193         {
  194             /* while */
  195             getInput();
  196             process();
  197         } /* while */
  198 
  199     return EXIT_SUCCESS;
  200 } /* main */
  201 
  202