Computer Programming Contest Preparation

ToolBox - Source for: 123/12356/brute.c



/home/toolbox/public_html/solutions/123/12356/brute.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: 2017-01-23
   18  * Purpose: fun
   19  * Problem: 12356 - Army Buddies
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_SOLDIERS 100002
   27 #define ALIVE 1
   28 #define DEAD 2
   29 #define UNBORN 86
   30 
   31 int soldiers[MAX_SOLDIERS];
   32 int S;  /* how many soldiers */
   33 int B;  /* number of loss reports */
   34 
   35 void init(int s)
   36 {
   37     /* FUNCTION init */
   38     int i;
   39 
   40     for (i=1; i<=s; i++)
   41         {
   42             /* for */
   43             soldiers[i] = ALIVE;
   44         } /* for */
   45     soldiers[0] = UNBORN;
   46     soldiers[s+1] = UNBORN;
   47 } /* FUNCTION init */
   48 
   49 void dump()
   50 {
   51     /* FUNCTION dump */
   52 } /* FUNCTION dump */
   53 
   54 int getInput()
   55 {
   56     /* FUNCTION getInput */
   57     int dataReadFlag;
   58 
   59     scanf(" %d %d ", &S, &B);
   60     dataReadFlag = (0 != S) || (0 != B);
   61     return (dataReadFlag);
   62 } /* FUNCTION getInput */
   63 
   64 void kill(int s1, int s2)
   65 {
   66     /* FUNCTION kill */
   67     int i;
   68 
   69     for (i=s1; i<=s2; i++)
   70         {
   71             /* for */
   72             soldiers[i] = DEAD;
   73         } /* for */
   74 } /* FUNCTION kill */
   75 
   76 void printLeft(int x)
   77 {
   78     /* FUNCTION printLeft */
   79     int i;
   80     int found = FALSE;
   81 
   82     for (i=(x-1); 0<i; i--)
   83         {
   84             /* for */
   85             if (ALIVE == soldiers[i])
   86                 {
   87                     /* found a live one */
   88                     printf("%d", i);
   89                     found = TRUE;
   90                     i = 0;
   91                 } /* found a live one */
   92         } /* for */
   93     if (! found)
   94         {
   95             printf("*");
   96         }
   97 } /* FUNCTION printLeft */
   98 
   99 void printRight(int x)
  100 {
  101     /* FUNCTION printRight */
  102     int i;
  103     int found = FALSE;
  104 
  105     for (i=x; i<=S; i++)
  106         {
  107             /* for */
  108             if (ALIVE == soldiers[i])
  109                 {
  110                     /* found a live one */
  111                     printf("%d", i);
  112                     found = TRUE;
  113                     i = S + 1;
  114                 } /* found a live one */
  115         } /* for */
  116     if (! found)
  117         {
  118             printf("*");
  119         }
  120 } /* FUNCTION printRight */
  121 
  122 void process()
  123 {
  124     /* FUNCTION process */
  125     int i;
  126     int d1;
  127     int d2;
  128 
  129     init(S);
  130     for (i=0; i<B; i++)
  131         {
  132             /* for each loss report */
  133             scanf(" %d %d ", &d1, &d2);
  134             kill(d1, d2);
  135             printLeft(d1);
  136             printf(" ");
  137             printRight(d2);
  138             printf("\n");
  139         } /* for each loss report */
  140     printf("-\n");
  141 } /* FUNCTION process */
  142 
  143 int main()
  144 {
  145     /* main */
  146     int moreToDo;
  147 
  148     moreToDo = getInput();
  149     while (moreToDo)
  150         {
  151             /* while */
  152             process();
  153             moreToDo = getInput();
  154         } /* while */
  155 
  156     return EXIT_SUCCESS;
  157 } /* main */
  158