Computer Programming Contest Preparation

ToolBox - Source for: 108/10823/b.c



/home/toolbox/public_html/solutions/108/10823/b.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 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 #define MAX_OBJECTS 101
   16 #define RR 0
   17 #define GG 1
   18 #define BB 2
   19 #define CNT 3
   20 #define SQUARE 1
   21 #define CIRCLE 2
   22 
   23 /* fprintf(stderr, "functionName: message", varslist); */
   24 
   25 /*
   26  *  Author: Isaac Traxler
   27  *    Date: 20120730
   28  * Purpose: Fun
   29  * Problem: Of Circles and Squares (10823)
   30  */
   31 
   32 /*
   33  * This template reads data a specified number of times.
   34  */
   35 
   36 struct objectStruct
   37 {
   38     /* DEFINE objStruct */
   39     int knd;  /* 0 undefined, 1 square, 2 circle */
   40     int px;
   41     int py;
   42     int lngth;
   43     int R;
   44     int G;
   45     int B;
   46 }; /* DEFINE objStruct */
   47 
   48 int numberOfTimes;
   49 int numObjects;
   50 int numQueries;
   51 struct objectStruct obj[MAX_OBJECTS];
   52 int qx[MAX_OBJECTS];
   53 int qy[MAX_OBJECTS];
   54 int color[4];
   55 
   56 void init()
   57 {
   58     /* FUNCTION init */
   59     scanf("%d ", &numberOfTimes);
   60 } /* FUNCTION init */
   61 
   62 void dump()
   63 {
   64     /* FUNCTION dump */
   65     int i;
   66 
   67     for (i=0; i<numObjects; i++)
   68         {
   69             /* for each object */
   70             printf("%3d: ", i);
   71             if (0 == obj[i].knd)
   72                 {
   73                     printf("Unknown");
   74                 }
   75             if (1 == obj[i].knd)
   76                 {
   77                     printf("Square ");
   78                 }
   79             if (2 == obj[i].knd)
   80                 {
   81                     printf("Circle ");
   82                 }
   83             printf(" (%d,%d) length: %d (%d, %d, %d)\n", obj[i].px, obj[i].py, obj[i].lngth, obj[i].R, obj[i].G, obj[i].B);
   84         } /* for each object */
   85 
   86     printf("\n");
   87 
   88     for (i=0; i<numQueries; i++)
   89         {
   90             /* load each query */
   91             printf("point %3d: (%d %d)\n", i, qx[i], qy[i]);
   92         } /* load each query */
   93 
   94     printf("\n");
   95     printf("\n");
   96 
   97 } /* FUNCTION dump */
   98 
   99 void getInput()
  100 {
  101     /* FUNCTION getInput */
  102     int i;
  103     char kind[10];
  104 
  105     scanf(" %d %d ", &numObjects, &numQueries);
  106 
  107     /* get objects */
  108     for (i=0; i<numObjects; i++)
  109         {
  110             /* for each object */
  111             obj[i].knd = 0;
  112             scanf("%s %d %d %d %d %d %d ", kind, &obj[i].px, &obj[i].py, &obj[i].lngth, &obj[i].R, &obj[i].G, &obj[i].B);
  113             if ('S' == kind[0])
  114                 {
  115                     obj[i].knd = 1;
  116                 }
  117             if ('C' == kind[0])
  118                 {
  119                     obj[i].knd = 2;
  120                 }
  121         } /* for each object */
  122 
  123     /* get query points -- this could be done one at a time in process */
  124     for (i=0; i<numQueries; i++)
  125         {
  126             /* load each query */
  127             scanf(" %d %d ", &qx[i], &qy[i]);
  128         } /* load each query */
  129 } /* FUNCTION getInput */
  130 
  131 int onSquareBorder(int idx, int x, int y)
  132 {
  133     /* FUNCTION onSquareBorder */
  134     int toReturn;
  135     /* check horizontals, then check verticals */
  136     DEBUG printf("square=%d  px=%d  py=%d  lngth=%d  x=%d  y=%d\n", idx, obj[idx].px, obj[idx].py, obj[idx].lngth, x , y);
  137     toReturn = ((x >= obj[idx].px) && (x <= (obj[idx].px + obj[idx].lngth)) &&
  138                 ((y == obj[idx].py) || (y == (obj[idx].py + obj[idx].lngth)))) ||
  139                ((y >= obj[idx].py) && (y <= (obj[idx].py + obj[idx].lngth)) &&
  140                 ((x == obj[idx].px) || (x == (obj[idx].px + obj[idx].lngth))));
  141     return toReturn;
  142 } /* FUNCTION onSquareBorder */
  143 
  144 int onCircleBorder(int idx, int x, int y)
  145 {
  146     /* FUNCTION onCircleBorder */
  147     int toReturn;
  148     int d2;
  149 
  150     /* do distance as squares to avoid squareroot */
  151     d2 = obj[idx].lngth * obj[idx].lngth;
  152     /* if radius^2 == (x-px)^2 + (y-py)^2 then it is on border */
  153     toReturn = ((x-obj[idx].px)*(x-obj[idx].px) + (y-obj[idx].py)*(y-obj[idx].py) == d2);
  154 } /* FUNCTION onCircleBorder */
  155 
  156 int inSquare(int idx, int x, int y)
  157 {
  158     /* FUNCTION inSquare */
  159     int toReturn;
  160 
  161     toReturn = (x > obj[idx].px) && (x < (obj[idx].px + obj[idx].lngth)) &&
  162                (y > obj[idx].py) && (y < (obj[idx].py + obj[idx].lngth));
  163     return toReturn;
  164 } /* FUNCTION inSquare */
  165 
  166 int inCircle(int idx, int x, int y)
  167 {
  168     /* FUNCTION inCircle */
  169     int toReturn;
  170     int d2;
  171 
  172     /* do distance as squares to avoid squareroot */
  173     d2 = obj[idx].lngth * obj[idx].lngth;
  174     /* if radius^2 == (x-px)^2 + (y-py)^2 then it is on border */
  175     toReturn = (((x-obj[idx].px)*(x-obj[idx].px) + (y-obj[idx].py)*(y-obj[idx].py)) < d2);
  176 } /* FUNCTION inCircle */
  177 
  178 void process()
  179 {
  180     /* FUNCTION process */
  181     int i;
  182     int j;
  183     int boundary;
  184     int interior;
  185 
  186     DEBUG dump();
  187     for (i=0; i<numQueries; i++)
  188         {
  189             /* do each query */
  190             boundary = FALSE;
  191             for (j=0; (j<numObjects) && (!boundary); j++)
  192                 {
  193                     /* for each object */
  194                     if (SQUARE == obj[j].knd)
  195                         {
  196                             boundary = onSquareBorder(j, qx[i], qy[i]);
  197                         }
  198                     if (CIRCLE == obj[j].knd)
  199                         {
  200                             boundary = onCircleBorder(j, qx[i], qy[i]);
  201                         }
  202                 } /* for each object */
  203             if (boundary)
  204                 {
  205                     /* on boundary */
  206                     printf("(0, 0, 0)\n");
  207                 } /* on boundary */
  208             else
  209                 {
  210                     /* check interior */
  211                     color[RR] = 0;
  212                     color[GG] = 0;
  213                     color[BB] = 0;
  214                     color[CNT] = 0;
  215                     for (j=0; j<numObjects; j++)
  216                         {
  217                             /* for each object */
  218                             interior = FALSE;
  219                             if (SQUARE == obj[j].knd)
  220                                 {
  221                                     /* check for inside SQUARE */
  222                                     interior = inSquare(j, qx[i], qy[i]);
  223                                 }  /* check for inside SQUARE */
  224                             if (CIRCLE == obj[j].knd)
  225                                 {
  226                                     /* check for inside CIRCLE */
  227                                     interior = inCircle(j, qx[i], qy[i]);
  228                                 } /* check for inside CIRCLE */
  229                             if (interior)
  230                                 {
  231                                     /* point is in interior of object */
  232                                     color[RR] = color[RR] + obj[j].R;
  233                                     color[GG] = color[GG] + obj[j].G;
  234                                     color[BB] = color[BB] + obj[j].B;
  235                                     color[CNT]++;
  236                                 } /* point is in interior of object */
  237                         } /* for each object */
  238                     if (0 == color[CNT])
  239                         {
  240                             /* not inside any objects */
  241                             printf("(255, 255, 255)\n");
  242                         } /* not inside any objects */
  243                     else
  244                         {
  245                             /* inside 1 or more objects */
  246                             color[RR] = (color[RR] + color[CNT] - 1) / color[CNT];
  247                             color[GG] = (color[GG] + color[CNT] - 1) / color[CNT];
  248                             color[BB] = (color[BB] + color[CNT] - 1) / color[CNT];
  249                             printf("(%d, %d, %d)\n", color[RR], color[GG], color[BB]);
  250                         } /* inside 1 or more objects */
  251                 } /* check interior */
  252         } /* do each query */
  253 } /* FUNCTION process */
  254 
  255 int main ()
  256 {
  257     /* main */
  258     int i;
  259 
  260     init();
  261     for (i=1; i<=numberOfTimes; i++)
  262         {
  263             /* while */
  264             if (1 != i)
  265                 {
  266                     printf("\n");
  267                 }
  268             getInput();
  269             printf("Case %d:\n", i);
  270             process();
  271         } /* while */
  272 
  273     return EXIT_SUCCESS;
  274 } /* main */
  275 
  276