Computer Programming Contest Preparation

ToolBox - Source for: 116/11661/a.c



/home/toolbox/public_html/solutions/116/11661/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 <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2020-09-10
   18  * Purpose: fun
   19  * Problem: 11661
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_ROAD 2000005
   27 #define EMPTY      '.'
   28 #define RESTAURANT 'R'
   29 #define DRUGSTORE  'D'
   30 #define BOTH       'Z'
   31 
   32 int roadLength;
   33 
   34 void init()
   35 {
   36     /* FUNCTION init */
   37 } /* FUNCTION init */
   38 
   39 void dump()
   40 {
   41     /* FUNCTION dump */
   42 } /* FUNCTION dump */
   43 
   44 int getInput()
   45 {
   46     /* FUNCTION getInput */
   47     int dataReadFlag;
   48 
   49     scanf(" %d ", &roadLength);
   50     dataReadFlag = (0 != roadLength);
   51     return (dataReadFlag);
   52 } /* FUNCTION getInput */
   53 
   54 void process()
   55 {
   56     /* FUNCTION process */
   57     int i;
   58     int prevDrug = MAX_ROAD;
   59     int prevRest = MAX_ROAD;
   60     int closest = MAX_ROAD;
   61     int restFound = FALSE;
   62     int drugFound = FALSE;
   63     char cur;
   64     int tmp;
   65 
   66     for (i=0; i<roadLength; i++)
   67         {
   68             /* go down road */
   69             scanf("%c", &cur);
   70             switch (cur)
   71                 {
   72                 /* switch */
   73                 case EMPTY:
   74                     break;
   75                 case RESTAURANT:
   76                     restFound = TRUE;
   77                     prevRest = i;
   78                     break;
   79                 case DRUGSTORE:
   80                     drugFound = TRUE;
   81                     prevDrug = i;
   82                     break;
   83                 case BOTH:
   84                     drugFound = TRUE;
   85                     restFound = TRUE;
   86                     closest = 0;
   87                     break;
   88                 } /* switch */
   89             if (drugFound && restFound)
   90                 {
   91                     /* possible better answer */
   92                     tmp = abs(prevDrug-prevRest);
   93                     closest = (closest > tmp) ? tmp : closest;
   94                 } /* possible better answer */
   95         } /* go down road */
   96     printf("%d\n", closest);
   97 } /* FUNCTION process */
   98 
   99 int main()
  100 {
  101     /* main */
  102     int moreToDo;
  103 
  104     init();
  105     moreToDo = getInput();
  106     while (moreToDo)
  107         {
  108             /* while */
  109             process();
  110             moreToDo = getInput();
  111         } /* while */
  112 
  113     return EXIT_SUCCESS;
  114 } /* main */
  115