Computer Programming Contest Preparation

ToolBox - Source for: 1/118/a.c



/home/toolbox/public_html/solutions/1/118/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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2021-12-2
   20  * Purpose: fun
   21  * Problem: 118 - Mutant Flatworld Explorers
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 #define MAX_LINE 257
   29 #define X 0
   30 #define Y 1
   31 #define MAX_SCENTS 200
   32 
   33 char line[MAX_LINE];
   34 int xCnt;
   35 int yCnt;
   36 int scent[MAX_SCENTS][2];
   37 int scentCnt;
   38 int xCur;
   39 int yCur;
   40 int xNext;
   41 int yNext;
   42 int direction;
   43 int dx[4] = {0, 1, 0, -1};
   44 int dy[4] = {1, 0, -1, 0};
   45 char prnt[5] = "NESW";
   46 int right[4] = {1, 2, 3, 0};
   47 int left[4] = {3, 0, 1, 2};
   48 
   49 void init()
   50 {
   51     /* FUNCTION init */
   52     scanf(" %d %d ", &xCnt, &yCnt);
   53     scentCnt = 0;
   54 } /* FUNCTION init */
   55 
   56 void dump()
   57 {
   58     /* FUNCTION dump */
   59 } /* FUNCTION dump */
   60 
   61 int way(char c)
   62 {
   63     /* FUNCTION way */
   64     int toReturn = -1;
   65 
   66     switch (c)
   67         {
   68             /* switch */
   69         case 'N' :
   70             toReturn = 0;
   71             break;
   72         case 'E' :
   73             toReturn = 1;
   74             break;
   75         case 'S' :
   76             toReturn = 2;
   77             break;
   78         case 'W' :
   79             toReturn = 3;
   80             break;
   81         } /* switch */
   82     return toReturn;
   83 } /* FUNCTION way */
   84 
   85 int getInput()
   86 {
   87     /* FUNCTION getInput */
   88     int dataReadFlag;
   89 
   90     dataReadFlag = (3 == scanf(" %d %d %s ", &xCur, &yCur, line));
   91     direction = way(line[0]);
   92     if (dataReadFlag)
   93         {
   94             /* something to read */
   95             scanf(" %s ", line);
   96         } /* something to read */
   97     return (dataReadFlag);
   98 } /* FUNCTION getInput */
   99 
  100 int findScent(int x, int y)
  101 {
  102     /* FUNCTION findScent */
  103     int found = FALSE;
  104     int i;
  105 
  106     for (i=0; (! found) && (scentCnt >= i); i++)
  107         {
  108             /* for each possible scent */
  109             found = ((x == scent[i][X]) && (y == scent[i][Y]));
  110         } /* for each possible scent */
  111     return found;
  112 } /* FUNCTION findScent */
  113 
  114 int safe()
  115 {
  116     /* FUNCTION safe */
  117     int mv = TRUE;
  118 
  119     xNext = xCur + dx[direction];
  120     yNext = yCur + dy[direction];
  121     DEBUG printf("grid test  (x %d > %d > 0) (y %d > %d > 0)\n", xCnt, xCur, yCnt, yCur);
  122     if ((0 > xNext) || (0 > yNext) ||
  123             (xCnt < xNext) || (yCnt < yNext))
  124         {
  125             /* move is off grid */
  126             DEBUG printf("off grid (cnt %d, %d) (cur %d, %d) (next %d, %d) (direction %d)\n", xCnt, yCnt, xCur, yCur, xNext, yNext, direction);
  127             if (findScent(xCur, yCur))
  128                 {
  129                     /* scented spot -- no move */
  130                     xNext = xCur;
  131                     yNext = yCur;
  132                 } /* scented spot -- no move */
  133             else
  134                 {
  135                     /* robot lost */
  136                     mv = FALSE;
  137                     scentCnt++;
  138                     scent[scentCnt][X] = xCur;
  139                     scent[scentCnt][Y] = yCur;
  140                 } /* robot lost */
  141         } /* move is off grid */
  142     return mv;
  143 } /* FUNCTION safe */
  144 
  145 void process()
  146 {
  147     /* FUNCTION process */
  148     int i;
  149     int slen;
  150     int lost = FALSE;
  151 
  152     DEBUG printf(" (%d, %d) %c [%s]\n", xCur, yCur, prnt[direction], line);
  153     slen = strlen(line);
  154     for (i=0; slen>i; i++)
  155         {
  156             /* try each move */
  157             DEBUG printf("before (%d, %d) %c (cmd %c) [%s]\n", xCur, yCur, prnt[direction], line[i], line);
  158             if ('R' == line[i])
  159                 {
  160                     /* turn right */
  161                     direction = right[direction];
  162                 } /* turn right */
  163             else if ('L' == line[i])
  164                 {
  165                     /* turn left */
  166                     direction = left[direction];
  167                 }/* turn left */
  168             else if (safe())
  169                 {
  170                     /* move is safe to make */
  171                     xCur = xNext;
  172                     yCur = yNext;
  173                 } /* move is safe to make */
  174             else
  175                 {
  176                     /* robot lost */
  177                     lost = TRUE;
  178                     i = slen;
  179                 } /* robot lost */
  180             DEBUG printf("after (%d, %d) %c (cmd %c) [%s]\n", xCur, yCur, prnt[direction], line[i], line);
  181         } /* try each move */
  182     printf("%d %d %c", xCur, yCur, prnt[direction]);
  183     if (lost)
  184         {
  185             printf(" LOST");
  186         }
  187     printf("\n");
  188 } /* FUNCTION process */
  189 
  190 int main()
  191 {
  192     /* main */
  193     int moreToDo;
  194 
  195     init();
  196     moreToDo = getInput();
  197     while (moreToDo)
  198         {
  199             /* while */
  200             process();
  201             moreToDo = getInput();
  202         } /* while */
  203 
  204     return EXIT_SUCCESS;
  205 } /* main */
  206