Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/108/10823/x.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 #define DEBUG1 if (FALSE)
   15 
   16 #define MAX_OBJECTS 111
   17 #define SQUARE 'S'
   18 
   19 /*
   20  *  Author: Isaac Traxler
   21  *    Date: 20120730
   22  * Purpose: Fun
   23  * Problem: Of Circles and Squares (10823)
   24  */
   25 
   26 struct objectStruct
   27 {
   28     /* DEFINE objStruct */
   29     char kind[8];
   30     int px;
   31     int py;
   32     int lngth;
   33     int r;
   34     int g;
   35     int b;
   36     int r2;    /* radius (lngth) squared */
   37     int pxl;   /* px + lngth */
   38     int pyl;   /* py + lngth */
   39 }; /* DEFINE objStruct */
   40 
   41 int numberOfTimes;
   42 int numObjects;
   43 int numQueries;
   44 struct objectStruct obj[MAX_OBJECTS];
   45 int qx[MAX_OBJECTS];
   46 int qy[MAX_OBJECTS];
   47 int R;
   48 int G;
   49 int B;
   50 int cnt;
   51 
   52 void getInput()
   53 {
   54     /* FUNCTION getInput */
   55     int i;
   56     char kind[10];
   57 
   58     scanf(" %d %d ", &numObjects, &numQueries);
   59 
   60     /* get objects */
   61     for (i=0; i<numObjects; i++)
   62         {
   63             /* for each object */
   64             scanf("%s %d %d %d %d %d %d ", obj[i].kind, &obj[i].px, &obj[i].py, &obj[i].lngth, &obj[i].r, &obj[i].g, &obj[i].b);
   65             obj[i].r2 = obj[i].lngth * obj[i].lngth;
   66             obj[i].pxl = obj[i].px + obj[i].lngth;
   67             obj[i].pyl = obj[i].py + obj[i].lngth;
   68         } /* for each object */
   69 
   70     /* get query points -- this could be done one at a time in process */
   71     for (i=0; i<numQueries; i++)
   72         {
   73             /* load each query */
   74             scanf(" %d %d ", &qx[i], &qy[i]);
   75         } /* load each query */
   76 } /* FUNCTION getInput */
   77 
   78 int onSquareBorder(int idx, int x, int y)
   79 {
   80     /* FUNCTION onSquareBorder */
   81     /* check horizontals, then check verticals */
   82     return ((x >= obj[idx].px) &&
   83             (x <= obj[idx].pxl) &&
   84             ((y == obj[idx].py) || (y == obj[idx].pyl)))
   85            ||
   86            ((y >= obj[idx].py) &&
   87             (y <= obj[idx].pyl) &&
   88             ((x == obj[idx].px) || (x == obj[idx].pxl)));
   89 } /* FUNCTION onSquareBorder */
   90 
   91 int onCircleBorder(int idx, int x, int y)
   92 {
   93     /* FUNCTION onCircleBorder */
   94 
   95     /* do distance as squares to avoid squareroot */
   96     /* if radius^2 == (x-px)^2 + (y-py)^2 then it is on border */
   97     return ((x-obj[idx].px)*(x-obj[idx].px) + (y-obj[idx].py)*(y-obj[idx].py) == obj[idx].r2);
   98 } /* FUNCTION onCircleBorder */
   99 
  100 int inSquare(int idx, int x, int y)
  101 {
  102     /* FUNCTION inSquare */
  103     int toReturn;
  104 
  105     return (x > obj[idx].px) && (x < obj[idx].pxl) &&
  106            (y > obj[idx].py) && (y < obj[idx].pyl);
  107 } /* FUNCTION inSquare */
  108 
  109 int inCircle(int idx, int x, int y)
  110 {
  111     /* FUNCTION inCircle */
  112     int toReturn;
  113 
  114     /* do distance as squares to avoid squareroot */
  115     /* if radius^2 == (x-px)^2 + (y-py)^2 then it is on border */
  116     return (((x-obj[idx].px)*(x-obj[idx].px) + (y-obj[idx].py)*(y-obj[idx].py)) < obj[idx].r2);
  117 } /* FUNCTION inCircle */
  118 
  119 void process()
  120 {
  121     /* FUNCTION process */
  122     int i;
  123     int j;
  124     int boundary;
  125     int interior;
  126 
  127     for (i=0; i<numQueries; i++)
  128         {
  129             /* do each query */
  130             boundary = FALSE;
  131             for (j=0; (j<numObjects) && (!boundary); j++)
  132                 {
  133                     /* for each object */
  134                     if (SQUARE == obj[j].kind[0])
  135                         {
  136                             boundary = onSquareBorder(j, qx[i], qy[i]);
  137                         }
  138                     else
  139                         {
  140                             boundary = onCircleBorder(j, qx[i], qy[i]);
  141                         }
  142                 } /* for each object */
  143             if (boundary)
  144                 {
  145                     /* on boundary */
  146                     printf("(0, 0, 0)\n");
  147                 } /* on boundary */
  148             else
  149                 {
  150                     /* check interior */
  151                     R = G = B = cnt = 0;
  152                     for (j=0; j<numObjects; j++)
  153                         {
  154                             /* for each object */
  155                             interior = FALSE;
  156                             if (SQUARE == obj[j].kind[0])
  157                                 {
  158                                     /* check for inside SQUARE */
  159                                     interior = inSquare(j, qx[i], qy[i]);
  160                                 }  /* check for inside SQUARE */
  161                             else
  162                                 {
  163                                     /* must be inside CIRCLE */
  164                                     interior = inCircle(j, qx[i], qy[i]);
  165                                 } /* must be inside CIRCLE */
  166                             if (interior)
  167                                 {
  168                                     /* point is in interior of object */
  169                                     R += obj[j].r;
  170                                     G += obj[j].g;
  171                                     B += obj[j].b;
  172                                     cnt++;
  173                                 } /* point is in interior of object */
  174                         } /* for each object */
  175                     if (0 == cnt)
  176                         {
  177                             /* not inside any objects */
  178                             printf("(255, 255, 255)\n");
  179                         } /* not inside any objects */
  180                     else
  181                         {
  182                             /* inside 1 or more objects */
  183                             R = (2 * R + cnt) / (2 * cnt);
  184                             G = (2 * G + cnt) / (2 * cnt);
  185                             B = (2 * B + cnt) / (2 * cnt);
  186                             printf("(%d, %d, %d)\n", R, G, B);
  187                         } /* inside 1 or more objects */
  188                 } /* check interior */
  189         } /* do each query */
  190 } /* FUNCTION process */
  191 
  192 int main ()
  193 {
  194     /* main */
  195     int i;
  196 
  197     scanf("%d ", &numberOfTimes);
  198     for (i=1; i<=numberOfTimes; i++)
  199         {
  200             /* while */
  201             if (1 != i)
  202                 {
  203                     printf("\n");
  204                 }
  205             getInput();
  206             printf("Case %d:\n", i);
  207             process();
  208         } /* while */
  209 
  210     return EXIT_SUCCESS;
  211 } /* main */
  212 
  213