Computer Programming Contest Preparation

ToolBox - Source for: 3/394/a.c



/home/toolbox/public_html/solutions/3/394/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 
   15 /* fprintf(stderr, "functionName: message", varslist); */
   16 
   17 #define MAX_NAME_LENGTH 15
   18 #define MAX_NUMBER_DIMENSIONS 15
   19 #define MAX_ARRAYS 10000
   20 
   21 /*
   22  *  Author: Isaac Traxler
   23  *    Date: 2021-08-26
   24  * Purpose: fun
   25  * Problem: 394 - Mapmaker
   26  */
   27 
   28 
   29 /*
   30  * This template reads data a specified number of times.
   31  */
   32 
   33 typedef struct ARRAY_STRUCT
   34 {
   35     char name[MAX_NAME_LENGTH];
   36     int base;
   37     int size;
   38     int dimCnt;
   39     int frst[MAX_NUMBER_DIMENSIONS];
   40     int last[MAX_NUMBER_DIMENSIONS];
   41     int diff[MAX_NUMBER_DIMENSIONS];
   42 } ARRAY_STRUCT;
   43 
   44 
   45 
   46 int N;
   47 int R;
   48 ARRAY_STRUCT a[MAX_ARRAYS];
   49 char query[MAX_NAME_LENGTH];
   50 int indicies[MAX_NUMBER_DIMENSIONS];
   51 int qIdx;
   52 
   53 void init()
   54 {
   55     /* FUNCTION init */
   56     scanf(" %d %d ", &N, &R);
   57 } /* FUNCTION init */
   58 
   59 void dumpArrays()
   60 {
   61     /* FUNCTION dumpArrays */
   62     int i;
   63     int j;
   64 
   65     for (i=0; N>i; i++)
   66         {
   67             /* for each array */
   68             printf("%8d(%1d): %-11s[(%d..%d[%d])", a[i].base, a[i].size, a[i].name, a[i].frst[1], a[i].last[1], a[i].diff[1]);
   69             for (j=2; a[i].dimCnt>=j; j++)
   70                 {
   71                     /* for each additional dimension */
   72                     printf(", (%d..%d[%d])", a[i].frst[j], a[i].last[j], a[i].diff[j]);
   73                 } /* for each additional dimension */
   74             printf("]\n");
   75         } /* for each array */
   76 } /* FUNCTION dumpArrays */
   77 
   78 void dumpQuery()
   79 {
   80     /* FUNCTION dumpQuery */
   81     int i;
   82 
   83     printf("query: %s[%d", query, indicies[1]);
   84     for (i=2; a[qIdx].dimCnt>=i; i++)
   85         {
   86             /* for 2 thru last index of query */
   87             printf(", %d", indicies[i]);
   88         } /* for 2 thru last index of query */
   89     printf("]\n");
   90 } /* FUNCTION dumpQuery */
   91 
   92 void getArrayDefinitions()
   93 {
   94     /* FUNCTION getArrayDefinitions */
   95     int i;
   96     int j;
   97 
   98     for (i=0; N>i; i++)
   99         {
  100             /* for each array definitions */
  101             scanf(" %s ", a[i].name);
  102             scanf(" %d ", &a[i].base);
  103             scanf(" %d ", &a[i].size);
  104             scanf(" %d ", &a[i].dimCnt);
  105             for (j=1; j<=a[i].dimCnt; j++)
  106                 {
  107                     /* for each dimension */
  108                     scanf(" %d %d ", &a[i].frst[j], &a[i].last[j]);
  109                     a[i].diff[j] = a[i].last[j] - a[i].frst[j] + 1;
  110                 } /* for each dimension */
  111         } /* for each array definitions */
  112 } /* FUNCTION getArrayDefinitions */
  113 
  114 int findArrayName(char name[])
  115 {
  116     /* FUNCTION findArrayName */
  117     int i;
  118     int fnd=-1;
  119 
  120     for (i=0; (N>i) && (0 != fnd); i++)
  121         {
  122             /* hunt for matching name */
  123             fnd = strcmp(name, a[i].name);
  124         } /* hunt for matching name */
  125     return (i - 1);
  126 } /* FUNCTION findArrayName */
  127 
  128 void getReference()
  129 {
  130     /* FUNCTION getReference */
  131     int i;
  132 
  133     scanf(" %s ", query);
  134     qIdx = findArrayName(query);
  135     for (i=1; a[qIdx].dimCnt>=i; i++)
  136         {
  137             /* get each index */
  138             scanf(" %d ", &indicies[i]);
  139         } /* get each index */
  140 } /* FUNCTION getReference */
  141 
  142 void process()
  143 {
  144     /* FUNCTION process */
  145     /* qIDX says which array is being queried */
  146     /* indicies are the indicies to use for the query */
  147     int address;
  148     int i;
  149     int j;
  150     int idx;
  151     int tmp;
  152 
  153     idx = a[qIdx].dimCnt;
  154     /* last index just needs to subtract start address */
  155     address = indicies[idx] - a[qIdx].frst[idx];
  156     idx--;
  157     for (i=idx; 0<i; i--)
  158         {
  159             /* for each index */
  160             tmp = 1;
  161             for (j=i; j<a[qIdx].dimCnt; j++)
  162                 {
  163                     /* for each remaining index */
  164                     tmp = tmp * a[qIdx].diff[j+1];
  165                 } /* for each remaining index */
  166             address = address + (tmp * (indicies[i] - a[qIdx].frst[i]));
  167         } /* for each index */
  168     address = (address * a[qIdx].size) + a[qIdx].base;
  169     /* actually output answer */
  170     printf("%s[%d", query, indicies[1]);
  171     for (i=2; i<=a[qIdx].dimCnt; i++)
  172         {
  173             /* print 2..N indicies */
  174             printf(", %d", indicies[i]);
  175         } /* print 2..N indicies */
  176     printf("] = %d\n", address);
  177 } /* FUNCTION process */
  178 
  179 int main()
  180 {
  181     /* main */
  182     int i;
  183 
  184     init();
  185     getArrayDefinitions();
  186     for (i=0; i<R; i++)
  187         {
  188             /* while */
  189             getReference();
  190             process();
  191         } /* while */
  192 
  193     return EXIT_SUCCESS;
  194 } /* main */
  195 
  196