Computer Programming Contest Preparation

ToolBox - Source for: 5/514/a.c



/home/toolbox/public_html/solutions/5/514/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: 2015-03-30
   18  * Purpose:
   19  * Problem: 514
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define STACK_SIZE 1100
   27 
   28 int stack[STACK_SIZE];
   29 int goal[STACK_SIZE];
   30 int top;
   31 
   32 int cars;
   33 
   34 void dump()
   35 {
   36     /* FUNCTION dump */
   37     int i;
   38 
   39     for (i=0; i<cars; i++)
   40         {
   41             printf(" %d", goal[i]);
   42         }
   43     printf("\n");
   44 } /* FUNCTION dump */
   45 
   46 int empty()
   47 {
   48     /* FUNCTION empty */
   49     return (0 == top);
   50 } /* FUNCTION empty */
   51 
   52 int inspect()
   53 {
   54     /* FUNCTION inspect */
   55     int toReturn;
   56 
   57     if (0 == top)
   58         {
   59             /* stack empty */
   60             toReturn = -1;
   61         } /* stack empty */
   62     else
   63         toReturn = stack[top];
   64     return toReturn;
   65 } /* FUNCTION inspect */
   66 
   67 int pop()
   68 {
   69     /* FUNCTION pop */
   70     return stack[top--];
   71 } /* FUNCTION pop */
   72 
   73 void push(int x)
   74 {
   75     /* FUNCTION push */
   76     stack[++top] = x;
   77 } /* FUNCTION push */
   78 
   79 int getInput()
   80 {
   81     /* FUNCTION getInput */
   82     int dataReadFlag;
   83 
   84     scanf(" %d ", &cars);
   85     DEBUG printf("cars = %d\n", cars);
   86     dataReadFlag = (0 != cars);
   87     return (dataReadFlag);
   88 } /* FUNCTION getInput */
   89 
   90 int getGoal()
   91 {
   92     /* FUNCTION getGoal */
   93     int dataReadFlag;
   94     int i;
   95 
   96     scanf(" %d ", &goal[0]);
   97     dataReadFlag = (0 != goal[0]);
   98     if (dataReadFlag)
   99         {
  100             /* read in desired order */
  101             for (i=1; i<cars; i++)
  102                 {
  103                     scanf(" %d ", &goal[i]);
  104                 }
  105         } /* read in desired order */
  106 } /* FUNCTION getGoal */
  107 
  108 void process()
  109 {
  110     /* FUNCTION process */
  111     int car;
  112     int g;
  113 
  114     while (getGoal())
  115         {
  116             /* process a goal */
  117             car = 1;
  118             top = 0;
  119             g = 0;
  120             push(car++);
  121             while (! empty())
  122                 {
  123                     /* more cars in stack */
  124                     DEBUG printf("car = %d  goal[%d] = %d stack[%d] = %d\n", car, g, goal[g], top, stack[top]);
  125                     while (inspect() == goal[g])
  126                         {
  127                             /* while stack has right car */
  128                             pop();
  129                             g++;
  130                         } /* while stack has right car */
  131                     if (cars >= car)
  132                         {
  133                             /* more cars left coming in */
  134                             push(car++);
  135                         } /* more cars left coming in */
  136                     else
  137                         {
  138                             /* no more cars coming in */
  139                             top = 0;
  140                         } /* no more cars coming in */
  141                 } /* more cars in stack */
  142             DEBUG printf("g = %d  cars = %d\n", g, cars) ;
  143             if (g == cars)
  144                 {
  145                     printf("Yes\n");
  146                 }
  147             else
  148                 {
  149                     printf("No\n");
  150                 }
  151         } /* process a goal */
  152     printf("\n");
  153 } /* FUNCTION process */
  154 
  155 int main()
  156 {
  157     /* main */
  158     int moreToDo;
  159 
  160     moreToDo = getInput();
  161     while (moreToDo)
  162         {
  163             /* while */
  164             process();
  165             moreToDo = getInput();
  166         } /* while */
  167 
  168     return EXIT_SUCCESS;
  169 } /* main */
  170 
  171 
  172 
  173 
  174