Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/118/11831/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  *  Author: Isaac Traxler
   18  *    Date: 2012-12-4
   19  * Purpose: fun
   20  * Problem: 11831 -  Sticker Collector Robots
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define ARENA_MAX 105
   28 #define MOVE_MAX 100005
   29 #define PILLAR '#'
   30 #define SPACE '.'
   31 #define STICKER '*'
   32 #define NORTH 'N'
   33 #define SOUTH 'S'
   34 #define EAST 'L'
   35 #define WEST 'O'
   36 #define North 0
   37 #define East 1
   38 #define South 2
   39 #define West 3
   40 #define RIGHT 'D'
   41 #define LEFT 'E'
   42 #define FORWARD 'F'
   43 #define Right 1
   44 #define Left 3
   45 
   46 char moves[MOVE_MAX];
   47 int arena[ARENA_MAX][ARENA_MAX];
   48 int m; /* number of columns */
   49 int n; /* number of rows */
   50 int s; /* number of moves */
   51 int curR;  /* current row */
   52 int curC;  /* current column */
   53 int curD;  /* current Direction */
   54 int stickers;
   55 /*            N   E   S   W */
   56 int dr[4] = {-1,  0,  1,  0};
   57 int dc[4] = { 0,  1,  0, -1};
   58 
   59 
   60 /* arena top left is NW corner, I will pretend that top left is 0,0
   61  * areana will be bounded by pillars to prevent exiting (done by me, not promised from description
   62  * arean indices will go from 1 to m or 1 to n
   63  */
   64 
   65 
   66 
   67 void init()
   68 {
   69     /* FUNCTION init */
   70 } /* FUNCTION init */
   71 
   72 void dump()
   73 {
   74     /* FUNCTION dump */
   75     int r;
   76     int c;
   77 
   78     printf("DD rows %d x cols %d\n", n, m);
   79     for (r=0; (1+n)>=r; r++)
   80         {
   81             /* for each row */
   82             printf("DD %3d ", r);
   83             for (c=0; (1+m)>=c; c++)
   84                 {
   85                     /* for each column */
   86                     printf("%c", arena[r][c]);
   87                 } /* for each column */
   88             printf(" %3d", r);
   89             printf("\n");
   90         } /* for each row */
   91 } /* FUNCTION dump */
   92 
   93 int getInput()
   94 {
   95     /* FUNCTION getInput */
   96     int dataReadFlag;
   97     int r;
   98     int c;
   99 
  100     scanf(" %d %d %d ", &n, &m, &s);
  101     dataReadFlag = (0 != n) && (0 != m) && (0 != s);
  102     if (dataReadFlag)
  103         {
  104             /* load rest of data */
  105             DEBUG printf("DD (m %d) (n %d) (s %d)\n", m, n, s);
  106             for (r=1; n>=r; r++)
  107                 {
  108                     /* for each row */
  109                     arena[r][0] = PILLAR;
  110                     arena[r][m+1] = PILLAR;
  111                     for (c=1; m>=c; c++)
  112                         {
  113                             /* for each column */
  114                             scanf(" %c ", &arena[r][c]);
  115                             if ((SPACE != arena[r][c]) && (PILLAR != arena[r][c]) && (STICKER != arena[r][c]))
  116                                 {
  117                                     /* found starting point */
  118                                     curR = r;
  119                                     curC = c;
  120                                     if (NORTH == arena[r][c])
  121                                         {
  122                                             curD = North;
  123                                         }
  124                                     else if (EAST == arena[r][c])
  125                                         {
  126                                             curD = East;
  127                                         }
  128                                     else if (SOUTH == arena[r][c])
  129                                         {
  130                                             curD = South;
  131                                         }
  132                                     else if (WEST == arena[r][c])
  133                                         {
  134                                             curD = West;
  135                                         }
  136                                 } /* found starting point */
  137                         } /* for each column */
  138                 } /* for each row */
  139             for (c=0; (1+m)>=c; c++)
  140                 {
  141                     /* do bottom and top guard rows */
  142                     arena[0][c] = PILLAR;
  143                     arena[n+1][c] = PILLAR;
  144                 } /* do bottom and top guard rows */
  145             scanf(" %s ", moves);
  146         } /* load rest of data */
  147     return (dataReadFlag);
  148 } /* FUNCTION getInput */
  149 
  150 void move()
  151 {
  152     /* FUNCTION move */
  153     int r;
  154     int c;
  155 
  156     r = curR + dr[curD];
  157     c = curC + dc[curD];
  158     switch (arena[r][c])
  159         {
  160         /* switch */
  161         case STICKER:
  162             stickers++;
  163             arena[r][c] = SPACE;
  164         case SPACE:
  165             curR = r;
  166             curC = c;
  167             break;
  168         case PILLAR:
  169             /* stay where you are */
  170             break;
  171         } /* switch */
  172 } /* FUNCTION move */
  173 
  174 void process()
  175 {
  176     /* FUNCTION process */
  177     int i;
  178 
  179     DEBUG dump();
  180     arena[curR][curC] = SPACE;
  181     stickers = 0;
  182     i = 0;
  183     DEBUG printf("DD bot is at %d %d facing %d with %d stickers\n", curR, curC, curD, stickers);
  184     while (s > i)
  185         {
  186             /* do each move */
  187             switch (moves[i])
  188                 {
  189                 /* switch */
  190                 case RIGHT:
  191                     curD = (curD + Right) % 4;
  192                     break;
  193                 case LEFT:
  194                     curD = (curD + Left) % 4;
  195                     break;
  196                 case FORWARD:
  197                     move();
  198                     break;
  199                 } /* switch */
  200             DEBUG printf("DD Move is %c (%d of %d), bot is at %d %d facing %d with %d stickers\n", moves[i], i, s, curR, curC, curD, stickers);
  201             DEBUG dump();
  202             i++;
  203         } /* do each move */
  204     printf("%d\n", stickers);
  205 } /* FUNCTION process */
  206 
  207 int main()
  208 {
  209     /* main */
  210     int moreToDo;
  211 
  212     init();
  213     moreToDo = getInput();
  214     while (moreToDo)
  215         {
  216             /* while */
  217             process();
  218             moreToDo = getInput();
  219         } /* while */
  220 
  221     return EXIT_SUCCESS;
  222 } /* main */
  223