Computer Programming Contest Preparation

ToolBox - Source for: 108/10855/a.c



/home/toolbox/public_html/solutions/108/10855/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 #define MAX_SQUARE_SIZE 1002
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2012-08-23
   20  * Purpose: fun
   21  * Problem: 10855 - Rotated Squares
   22  */
   23 
   24 /*
   25  * This template reads data until a terminating value is reached.
   26  */
   27 
   28 int bDim;
   29 int sDim;
   30 char b[MAX_SQUARE_SIZE][MAX_SQUARE_SIZE];
   31 char s[MAX_SQUARE_SIZE][MAX_SQUARE_SIZE];
   32 char t[MAX_SQUARE_SIZE][MAX_SQUARE_SIZE];
   33 
   34 void init()
   35 {
   36     /* FUNCTION init */
   37 } /* FUNCTION init */
   38 
   39 void dump()
   40 {
   41     /* FUNCTION dump */
   42     int i;
   43     int j;
   44 
   45     printf("Large Square (%dx%d)\n", bDim,  bDim);
   46     for (i=0; i<bDim; i++)
   47         {
   48             /* for i */
   49             for (j=0; j<bDim; j++)
   50                 {
   51                     printf("%c", b[i][j]);
   52                 }
   53             printf("\n");
   54         } /* for i */
   55     printf("Small Square (%dx%d)\n", sDim,  sDim);
   56     for (i=0; i<sDim; i++)
   57         {
   58             /* for i */
   59             for (j=0; j<sDim; j++)
   60                 {
   61                     printf("%c", s[i][j]);
   62                 }
   63             printf("\n");
   64         } /* for i */
   65     printf("\n");
   66 } /* FUNCTION dump */
   67 
   68 int getInput()
   69 {
   70     /* FUNCTION getInput */
   71     int dataReadFlag = TRUE;
   72     int i;
   73 
   74     scanf(" %d %d ", &bDim, &sDim);
   75     if (0 != bDim)
   76         {
   77             /* load squares */
   78             for (i=0; i<bDim; i++)
   79                 {
   80                     /* load a row of b */
   81                     fgets(&b[i][0], MAX_SQUARE_SIZE, stdin);
   82                 } /* load a row of b */
   83             for (i=0; i<sDim; i++)
   84                 {
   85                     /* load a row of s */
   86                     fgets(&s[i][0], MAX_SQUARE_SIZE, stdin);
   87                 } /* load a row of s */
   88         } /* load squares */
   89     else
   90         {
   91             /* nothing to process */
   92             dataReadFlag = FALSE;
   93         } /* nothing to process */
   94     return (dataReadFlag);
   95 } /* FUNCTION getInput */
   96 
   97 int countMatches(int r, int c)
   98 {
   99     /* FUNCTION countMatches */
  100     int match = TRUE;
  101     int i;
  102     int j;
  103 
  104     for (i=0; ((match) && (sDim>i)); i++)
  105         {
  106             /* for each row */
  107             for (j=0; ((match) && (sDim>j)); j++)
  108                 {
  109                     /* for each column */
  110                     match = match && (s[i][j] == b[i+r][j+c]);
  111                     DEBUG if (match)
  112                         {
  113                             printf("(%d,%d)\n", i+r, j+c);
  114                         }
  115                 } /* for each column */
  116         } /* for each row */
  117     if (match)
  118         i = 1;
  119     else
  120         i = 0;
  121     DEBUG if (match)
  122         {
  123             printf("matched\n");
  124         }
  125     return i;
  126 } /* FUNCTION countMatches */
  127 
  128 void rotate()
  129 {
  130     /* FUNCTION rotate */
  131     int i;
  132     int j;
  133 
  134     /* make a copy of small */
  135     for (i=0; i<sDim; i++)
  136         {
  137             /* for i */
  138             for (j=0; j<sDim; j++)
  139                 {
  140                     t[i][j] = s[i][j];
  141                 }
  142         } /* for i */
  143 
  144     /* now put rotated copy into small */
  145     for (i=0; i<sDim; i++)
  146         {
  147             /* for i */
  148             for (j=0; j<sDim; j++)
  149                 {
  150                     /* for j */
  151                     s[j][sDim - i - 1] = t[i][j];
  152                 } /* for j */
  153         } /* for i */
  154 } /* FUNCTION rotate */
  155 
  156 int countAllMatches()
  157 {
  158     /* FUNCTION countAllMatches */
  159     int i;
  160     int j;
  161     int cnt = 0;
  162 
  163     for (i=0; (bDim-sDim+1)>i; i++)
  164         {
  165             /* loop for each row */
  166             for (j=0; (bDim-sDim+1)>j; j++)
  167                 {
  168                     /* loop for each column */
  169                     cnt = cnt + countMatches(i,j);
  170                 } /* loop for each column */
  171         } /* loop for each row */
  172     return cnt;
  173 } /* FUNCTION countAllMatches */
  174 
  175 void process()
  176 {
  177     /* FUNCTION process */
  178     int cnt0=0;
  179     int cnt1=0;
  180     int cnt2=0;
  181     int cnt3=0;
  182 
  183     DEBUG dump();
  184     cnt0 = countAllMatches();
  185     rotate();
  186     DEBUG dump();
  187     cnt1 = countAllMatches();
  188     rotate();
  189     DEBUG dump();
  190     cnt2 = countAllMatches();
  191     rotate();
  192     cnt3 = countAllMatches();
  193     DEBUG dump();
  194     printf("%d %d %d %d\n", cnt0, cnt1, cnt2, cnt3);
  195 } /* FUNCTION process */
  196 
  197 int main ()
  198 {
  199     /* main */
  200     int moreToDo;
  201 
  202     init();
  203     moreToDo = getInput();
  204     while (moreToDo)
  205         {
  206             /* while */
  207             process();
  208             moreToDo = getInput();
  209         } /* while */
  210 
  211     return EXIT_SUCCESS;
  212 } /* main */
  213