Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/118/11831/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 <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 (TRUE)
   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 10005
   29 #define PILLAR '#'
   30 #define SPACE '.'
   31 #define STICKER '*'
   32 #define VISITED 'X'
   33 #define NORTH 'N'
   34 #define SOUTH 'S'
   35 #define EAST 'L'
   36 #define WEST 'O'
   37 #define North 0
   38 #define East 1
   39 #define South 2
   40 #define West 3
   41 #define RIGHT 'D'
   42 #define LEFT 'E'
   43 #define FORWARD 'F'
   44 #define Right 1
   45 #define Left 3
   46 
   47 char moves[MOVE_MAX];
   48 int arena[ARENA_MAX][ARENA_MAX];
   49 int m; /* number of rows */
   50 int n; /* number of columns */
   51 int s; /* number of moves */
   52 int curR;  /* current row */
   53 int curC;  /* current column */
   54 int curD;  /* current Direction */
   55 int stickers;
   56 /*            N   E   S   W */
   57 int dr[4] = {-1,  0,  1,  0};
   58 int dc[4] = { 0,  1,  0, -1};
   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     for (r=0; (1+m)>=r; r++)
   79         {
   80             /* for each row */
   81             printf("%3d ", r);
   82             for (c=0; (1+n)>=c; c++)
   83                 {
   84                     /* for each column */
   85                     printf("%c", arena[r][c]);
   86                 } /* for each column */
   87             printf(" %3d", r);
   88             printf("\n");
   89         } /* for each row */
   90 
   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             for (r=1; m>=r; r++)
  106                 {
  107                     /* for each row */
  108                     arena[r][0] = PILLAR;
  109                     arena[r][n+1] = PILLAR;
  110                     for (c=1; n>=c; c++)
  111                         {
  112                             /* for each column */
  113                             scanf(" %c ", &arena[r][c]);
  114                             if ((SPACE != arena[r][c]) && (PILLAR != arena[r][c]) && (STICKER != arena[r][c]))
  115                                 {
  116                                     /* found starting point */
  117                                     curR = r;
  118                                     curC = c;
  119                                     if (NORTH == arena[r][c])
  120                                         {
  121                                             curD = North;
  122                                         }
  123                                     else if (EAST == arena[r][c])
  124                                         {
  125                                             curD = East;
  126                                         }
  127                                     else if (SOUTH == arena[r][c])
  128                                         {
  129                                             curD = South;
  130                                         }
  131                                     else if (WEST == arena[r][c])
  132                                         {
  133                                             curD = West;
  134                                         }
  135                                 } /* found starting point */
  136                         } /* for each column */
  137                 } /* for each row */
  138             for (c=0; (1+n)>=c; c++)
  139                 {
  140                     /* do bottom and top guard rows */
  141                     arena[0][c] = PILLAR;
  142                     arena[m+1][c] = PILLAR;
  143                 } /* do bottom and top guard rows */
  144             scanf(" %s ", moves);
  145         } /* load rest of data */
  146     return (dataReadFlag);
  147 } /* FUNCTION getInput */
  148 
  149 void move()
  150 {
  151     /* FUNCTION move */
  152     int r;
  153     int c;
  154 
  155     r = curR + dr[curD];
  156     c = curC + dc[curD];
  157     switch (arena[r][c])
  158         {
  159         /* switch */
  160         case STICKER:
  161             stickers++;
  162             arena[r][c] = VISITED;
  163         case VISITED:
  164         case SPACE:
  165             curR = r;
  166             curC = c;
  167             arena[r][c] = VISITED;
  168             break;
  169         case PILLAR:
  170             /* stay where you are */
  171             break;
  172         } /* switch */
  173 } /* FUNCTION move */
  174 
  175 void process()
  176 {
  177     /* FUNCTION process */
  178     int i;
  179 
  180     DEBUG dump();
  181     arena[curR][curC] = VISITED;
  182     stickers = 0;
  183     i = 0;
  184     DEBUG printf("bot is at %d %d facing %d with %d stickers\n", curR, curC, curD, stickers);
  185     while (s > i)
  186         {
  187             /* do each move */
  188             switch (moves[i])
  189                 {
  190                 /* switch */
  191                 case RIGHT:
  192                     curD = (curD + Right) % 4;
  193                     break;
  194                 case LEFT:
  195                     curD = (curD + Left) % 4;
  196                     break;
  197                 case FORWARD:
  198                     move();
  199                     break;
  200                 } /* switch */
  201             DEBUG printf("Move is %c, bot is at %d %d facing %d with %d stickers\n", moves[i], curR, curC, curD, stickers);
  202             dump();
  203             i++;
  204         } /* do each move */
  205     printf("%d\n", stickers);
  206 } /* FUNCTION process */
  207 
  208 int main()
  209 {
  210     /* main */
  211     int moreToDo;
  212 
  213     init();
  214     moreToDo = getInput();
  215     while (moreToDo)
  216         {
  217             /* while */
  218             process();
  219             moreToDo = getInput();
  220         } /* while */
  221 
  222     return EXIT_SUCCESS;
  223 } /* main */
  224