Computer Programming Contest Preparation

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



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