1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <string.h>
    4 
    5 int m[22][22];
    6 int di[4][2] =
    7 {
    8   { -1, 0 },
    9   { 0, 1},
   10   { 1, 0 },
   11   { 0, -1 }
   12 };
   13 
   14 int
   15 main(int argc, char **argv)
   16 {
   17   char ibuf[512], *s, obuf[400];
   18   int i, n, ir, ic, r, c, ch, cnt, dir, loop, idx;
   19 
   20   if (fgets(&(ibuf[0]), sizeof(ibuf), stdin) == NULL)
   21     {
   22       return(0);
   23     }
   24   n = atoi(&(ibuf[0]));
   25   for (i = 1; i <= n; i++)
   26     {
   27       if (fgets(&(ibuf[0]), sizeof(ibuf), stdin) == NULL)
   28         {
   29           printf("premature eof %d\n", i);
   30           break;
   31         }
   32       r = atoi(&(ibuf[0]));
   33       s = strchr(&(ibuf[0]), ' ');
   34       if (s == NULL)
   35         {
   36           printf("missing separator 1 (%d)\n", i);
   37           break;
   38         }
   39       s++;
   40       c = atoi(s);
   41       s = strchr(s, ' ');
   42       if (s == NULL)
   43         {
   44           printf("missing separator 2 (%d)\n", i);
   45           break;
   46         }
   47       s++;
   48       /* remainder of line is text */
   49       /* Fill in matrix with 0's and a border of -1*/
   50       for (ir = 1; ir <= r; ir++)
   51         {
   52           m[ir][0] = -1;
   53           for (ic = 1; ic <= c; ic++)
   54             {
   55               m[ir][ic] = 0;
   56             }
   57           /* Set up border */
   58           m[ir][ic] = -1;
   59         }
   60       /* Set up border */
   61       for (idx = 0; idx <= ic; idx++)
   62         {
   63           m[ir][idx] = -1;
   64           m[0][idx] = -1;
   65         }
   66       ir = 1;
   67       ic = 1;
   68       ch = 0;
   69       dir = 1;
   70       /* Populate matrix */
   71       for (cnt = 0;;)
   72         {
   73           if (cnt == 0)
   74             {
   75               /* Get next char */
   76               if (*s >= 'A' && *s <= 'Z')
   77                 {
   78                   ch = *s - 'A' + 1;
   79                 }
   80               else if (*s == ' ')
   81                 {
   82                   ch = 0;
   83                 }
   84               else
   85                 {
   86                   break;
   87                 }
   88               s++;
   89               cnt = 5;
   90             }
   91           cnt--;
   92           if (ch & (1 << cnt))
   93             {
   94               m[ir][ic] = 2;
   95             }
   96           else
   97             {
   98               m[ir][ic] = 1;
   99             }
  100           loop = 0;
  101           /* Next move taken? */
  102           while (m[ir+di[dir][0]][ic+di[dir][1]] != 0)
  103             {
  104               dir++;
  105               if (dir >= 4)
  106                 {
  107                   dir = 0;
  108                   if (loop++)
  109                     {
  110                       break;
  111                     }
  112                 }
  113             }
  114           if (loop > 1)
  115             {
  116               break;
  117             }
  118           ir += di[dir][0];
  119           ic += di[dir][1];
  120         }
  121       /* Matrix filled in now unwind */
  122       s = &(obuf[0]);
  123       for (ir = 1; ir <= r; ir++)
  124         {
  125           for (ic = 1; ic <= c; ic++)
  126             {
  127               if (m[ir][ic] == 2)
  128                 {
  129                   *s++ = '1';
  130                 }
  131               else
  132                 {
  133                   *s++ = '0';
  134                 }
  135             }
  136         }
  137       *s = '\0';
  138 #ifndef MKINPUT
  139       printf("%d %s\n", i, &(obuf[0]));
  140 #else
  141       printf("%d %d %s\n", r, c, &(obuf[0]));
  142 #endif
  143     }
  144   return 0;
  145 }
  146