Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/5/514/d.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 inspect()
   47 {
   48     /* FUNCTION inspect */
   49     int toReturn;
   50 
   51     if (0 == top)
   52         {
   53             /* stack empty */
   54             toReturn = -1;
   55         } /* stack empty */
   56     else
   57         toReturn = stack[top];
   58     return toReturn;
   59 } /* FUNCTION inspect */
   60 
   61 int pop()
   62 {
   63     /* FUNCTION pop */
   64     return stack[top--];
   65 } /* FUNCTION pop */
   66 
   67 void push(int x)
   68 {
   69     /* FUNCTION push */
   70     stack[top++] = x;
   71 } /* FUNCTION push */
   72 
   73 int getInput()
   74 {
   75     /* FUNCTION getInput */
   76     int dataReadFlag;
   77 
   78     scanf(" %d ", &cars);
   79     printf("cars = %d\n", cars);
   80     dataReadFlag = (0 != cars);
   81     return (dataReadFlag);
   82 } /* FUNCTION getInput */
   83 
   84 int getGoal()
   85 {
   86     /* FUNCTION getGoal */
   87     int dataReadFlag;
   88     int i;
   89 
   90     scanf(" %d ", &goal[0]);
   91     dataReadFlag = (0 != goal[0]);
   92     if (dataReadFlag)
   93         {
   94             /* read in desired order */
   95             for (i=1; i<cars; i++)
   96                 {
   97                     scanf(" %d ", &goal[i]);
   98                 }
   99         } /* read in desired order */
  100 } /* FUNCTION getGoal */
  101 
  102 void process()
  103 {
  104     /* FUNCTION process */
  105     int car = 1;
  106     int nxt;
  107     int success = TRUE;
  108     int g = 0;
  109 
  110     while (getGoal())
  111         {
  112             /* process this goal */
  113             dump();
  114             top = 0;
  115             g = 0;
  116             while ((g < cars) && (car < cars))
  117                 {
  118                     /* while more cars to deal with */
  119                     printf("g=%d  cars=%d  car=%d  goal[%d]=%d\n", g, cars, car, g, goal[g]);
  120                     if (goal[g] == car)
  121                         {
  122                             /* found car I want */
  123                             g++;
  124                             car++;
  125                         } /* found car I want */
  126                     else if (goal[g] == inspect())
  127                         {
  128                             /* car I want is in stack */
  129                             g++;
  130                             pop();
  131                         } /* car I want is in stack */
  132                     else
  133                         {
  134                             /* not the right car, save it */
  135                             push(car);
  136                             car++;
  137                         } /* not the right car, save it */
  138                 } /* while more cars to deal with */
  139 
  140             printf("g=%d  cars=%d  car=%d  goal[%d]=%d  top=%d\n", g, cars, car, g, goal[g], top);
  141             /* if ((-1 == inspect()) && (car == cars)) */
  142             if ((0 == top) && (car > cars))
  143                 {
  144                     printf("Yes");
  145                 }
  146             else
  147                 {
  148                     printf("No");
  149                 }
  150             printf("\n");
  151         } /* process this 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