Computer Programming Contest Preparation

ToolBox - Source for: 100/10010/a.c



/home/toolbox/public_html/solutions/100/10010/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 <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 /* fprintf(stderr, "functionName: message", varslist); */
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2020-02-07
   20  * Purpose: fun
   21  * Problem: 10010
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define MAX_GRID 52
   29 
   30 int numberOfTimes;
   31 char grid[MAX_GRID][MAX_GRID];
   32 char wrd[MAX_GRID];
   33 int  wlen;
   34 int  gridHeight;
   35 int  gridWidth;
   36 int  wordCnt;
   37 int  ansR;
   38 int  ansC;
   39 /*    123   *
   40  *    4X5   *
   41  *    678   */
   42 int dr[8] = {-1, -1, -1,  0,  0,  1,  1,  1};
   43 int dc[8] = {-1,  0,  1, -1,  1, -1,  0,  1};
   44 
   45 void init()
   46 {
   47     /* FUNCTION init */
   48     scanf("%d ", &numberOfTimes);
   49 } /* FUNCTION init */
   50 
   51 void dump()
   52 {
   53     /* FUNCTION dump */
   54     int r;
   55     int c;
   56     for (r=1; gridHeight>=r; r++)
   57         {
   58             /* for each row of the grid */
   59             for (c=1; gridWidth>=c; c++)
   60                 {
   61                     /* for each column */
   62                     printf("%c", grid[r][c]);
   63                 } /* for each column */
   64             printf("\n");
   65         } /* for each row of the grid */
   66 } /* FUNCTION dump */
   67 
   68 void getInput()
   69 {
   70     /* FUNCTION getInput */
   71     int i;
   72     int j;
   73     char x;
   74 
   75     scanf(" %d %d ", &gridHeight, &gridWidth);
   76     for (i=1; gridHeight>=i; i++)
   77         {
   78             /* for each row of the grid */
   79             for (j=1; gridWidth>=j; j++)
   80                 {
   81                     /* for each column */
   82                     scanf("%c ", &x);
   83                     grid[i][j] = tolower(x);
   84                 } /* for each column */
   85         } /* for each row of the grid */
   86     scanf(" %d ", &wordCnt);
   87 } /* FUNCTION getInput */
   88 
   89 void downCase()
   90 {
   91     /* FUNCTION downCase */
   92     int i;
   93 
   94     for (i=0; strlen(wrd)>i; i++)
   95         {
   96             /* for each letter */
   97             wrd[i] = tolower(wrd[i]);
   98         } /* for each letter */
   99 } /* FUNCTION downCase */
  100 
  101 int checkLetter(int r, int c, char lt)
  102 {
  103     /* FUNCTION checkLetter */
  104     int match;
  105 
  106     DEBUG printf("%c in %s (%d) - %d/%d %d/%d %c\n", lt, wrd, wlen, r, gridHeight, c, gridWidth,  grid[r][c]);
  107     match = (r>0) && (r<=gridHeight) &&
  108             (c>0) && (c<=gridWidth) &&
  109             (grid[r][c] == lt);
  110     return match;
  111 } /* FUNCTION checkLetter */
  112 
  113 int checkDirection(int dir, int r, int c)
  114 {
  115     /* FUNCTION checkDirection */
  116     int match = TRUE;
  117     int i;
  118 
  119     /* first letter has already been checked to get here */
  120     for (i=1; match && (wlen>i); i++)
  121         {
  122             /* process each letter of the word */
  123             match = checkLetter(r + (dr[dir]*i), c + (dc[dir]*i), wrd[i]);
  124         } /* process each letter of the word */
  125     return (match);
  126 } /* FUNCTION checkDirection */
  127 
  128 int checkWord(int r, int c)
  129 {
  130     /* FUNCTION checkWord */
  131     /* returns False if found, true if not found */
  132     int found;
  133     int i;
  134     int cnt = 1;
  135 
  136     /* does first letter work */
  137     found = checkLetter(r, c, wrd[0]);
  138     if (found && (1 < wlen))
  139         {
  140             /* do I need to check more characters? */
  141             found = FALSE;
  142             for (i=0; (! found) && (8>i); i++)
  143                 {
  144                     /* advance in all 8 possible directions */
  145                     found = checkDirection(i, r, c);
  146                 } /* advance in all 8 possible directions */
  147         } /* do I need to check more characters? */
  148     return (! found);
  149 } /* FUNCTION checkWord */
  150 
  151 void findWord()
  152 {
  153     /* FUNCTION findWord */
  154     int r;
  155     int c;
  156     int notFound = TRUE;
  157 
  158     for (r=1; notFound && (gridHeight>=r); r++)
  159         {
  160             /* for each row */
  161             for (c=1; notFound && (gridWidth>=c); c++)
  162                 {
  163                     /* for each column */
  164                     DEBUG printf("checking: %d %d\n", r, c);
  165                     notFound = checkWord(r,c);
  166                 } /* for each column */
  167         } /* for each row */
  168     /* will always be found */
  169     DEBUG printf("found: %d %d\n", r, c);
  170     ansR = r - 1;
  171     ansC = c - 1;
  172 } /* FUNCTION findWord */
  173 
  174 void process()
  175 {
  176     /* FUNCTION process */
  177     int i;
  178 
  179     DEBUG dump();
  180     /* process each word to earch for */
  181     for (i=0; wordCnt>i; i++)
  182         {
  183             /* for each word */
  184             scanf(" %s ", wrd);
  185             wlen = strlen(wrd);
  186             downCase();
  187             findWord();
  188             DEBUG printf("%s\n", wrd);
  189             printf("%d %d\n", ansR, ansC);
  190         } /* for each word */
  191 } /* FUNCTION process */
  192 
  193 int main()
  194 {
  195     /* main */
  196     int i;
  197 
  198     init();
  199     for (i=0; i<numberOfTimes; i++)
  200         {
  201             /* while */
  202             getInput();
  203             if (0 != i)
  204                 {
  205                     printf("\n");
  206                 }
  207             process();
  208         } /* while */
  209 
  210     return EXIT_SUCCESS;
  211 } /* main */
  212 
  213