Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/100/10010/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 /* 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 i;
   55     int j;
   56     for (i=1; gridHeight>=i; i++)
   57         {
   58             /* for each row of the grid */
   59             for (j=1; gridWidth>=j; j++)
   60                 {
   61                     /* for each column */
   62                     printf("%c", grid[i][j]);
   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     printf("%s %c - %d %d %c\n", wrd, lt, r, c, grid[r][c]);
  107     match = (r>0) && (r<=gridWidth) &&
  108             (c>0) && (c<=gridHeight) &&
  109             (grid[r][c] == lt);
  110     return match;
  111 } /* FUNCTION checkLetter */
  112 
  113 int checkWord(int r, int c)
  114 {
  115     /* FUNCTION chechWord */
  116     /* returns False if found, true if not found */
  117     int found = TRUE;
  118     int w;
  119     int i;
  120     int cnt = 1;
  121     int tr;
  122     int tc;
  123 
  124     /* does first letter work */
  125     found = checkLetter(r, c, wrd[0]);
  126     if (found && (1 < wlen))
  127         {
  128             /* do I need to check more characters? */
  129             found = FALSE;
  130             for (i=0; (! found) && (8>i); i++)
  131                 {
  132                     /* advance in all 8 possible directions */
  133                     if (checkLetter(r + dr[i], c + dc[i], wrd[1]))
  134                         {
  135                             /* 2nd letter matches -- keep checking */
  136                             found = TRUE;
  137                             for (cnt=2; found && (wlen>cnt); cnt++)
  138                                 {
  139                                     /* process each letter of the word */
  140                                     found = checkLetter(r+(cnt*dr[i]), c+(cnt*dc[i]), wrd[cnt]);
  141                                 } /* process each letter of the word */
  142                             found = found && (cnt == wlen);
  143                         } /* 2nd letter matches -- keep checking */
  144                 } /* advance in all 8 possible directions */
  145         } /* do I need to check more characters? */
  146     return (! found);
  147 } /* FUNCTION chechWord */
  148 
  149 void findWord()
  150 {
  151     /* FUNCTION findWord */
  152     int i;
  153     int j;
  154     int notFound = TRUE;
  155 
  156     for (i=1; notFound && (gridHeight>=i); i++)
  157         {
  158             /* for each row */
  159             for (j=1; notFound && (gridWidth>=j); j++)
  160                 {
  161                     /* for each column */
  162                     printf("checking: %d %d\n", i, j);
  163                     notFound = checkWord(i,j);
  164                 } /* for each column */
  165         } /* for each row */
  166     /* will always be found */
  167     printf("found: %d %d\n", i, j);
  168     ansR = i;
  169     ansC = j;
  170 } /* FUNCTION findWord */
  171 
  172 void process()
  173 {
  174     /* FUNCTION process */
  175     int i;
  176 
  177     dump();
  178     /* process each word to earch for */
  179     for (i=0; wordCnt>i; i++)
  180         {
  181             /* for each word */
  182             scanf(" %s ", wrd);
  183             wlen = strlen(wrd);
  184             downCase();
  185             findWord();
  186             printf("%s\n", wrd);
  187             printf("%d %d\n", ansR, ansC);
  188         } /* for each word */
  189 } /* FUNCTION process */
  190 
  191 int main()
  192 {
  193     /* main */
  194     int i;
  195 
  196     init();
  197     for (i=0; i<numberOfTimes; i++)
  198         {
  199             /* while */
  200             getInput();
  201             process();
  202         } /* while */
  203 
  204     return EXIT_SUCCESS;
  205 } /* main */
  206 
  207