Computer Programming Contest Preparation

ToolBox - Source for: naq/2025/d.c



/home/toolbox/public_html/solutions/naq/2025/d.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: 2025-10-11
   19  * Purpose: fun
   20  * Problem: naq-d
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define MAXD 52
   28 #define R 0
   29 #define U 1
   30 #define L 2
   31 #define D 3
   32 #define OPEN 0
   33 #define X 0
   34 #define Y 1
   35 
   36 int m[MAXD][MAXD]; /* maze */
   37 int d[2]; /* maze dimension */
   38 int a[2]; /* Carl the ant */
   39 int g[2]; /* goal */
   40 int turn[4][2];
   41 int move[4][2];
   42 char direction[5];
   43 
   44 void init()
   45 {
   46     /* FUNCTION init */
   47     int i;
   48     int j;
   49 
   50     for (i=0; i<MAXD; i++)
   51         {
   52             /* for i */
   53             for (j=0; j<MAXD; j++)
   54                 {
   55                     /* for j */
   56                     m[i][j] = 1;
   57                 } /* for j */
   58         } /* for i */
   59 
   60     turn[R][X] =  0;
   61     turn[R][Y] = -1;
   62     turn[U][X] = -1;
   63     turn[U][Y] =  0;
   64     turn[L][X] =  0;
   65     turn[L][Y] =  1;
   66     turn[D][X] =  1;
   67     turn[D][Y] =  0;
   68 
   69     move[R][X] =  1;
   70     move[R][Y] =  0;
   71     move[U][X] =  0;
   72     move[U][Y] = -1;
   73     move[L][X] = -1;
   74     move[L][Y] =  0;
   75     move[D][X] =  0;
   76     move[D][Y] =  1;
   77 
   78     direction[R]='R';
   79     direction[U]='U';
   80     direction[L]='L';
   81     direction[D]='D';
   82 } /* FUNCTION init */
   83 
   84 void dump()
   85 {
   86     /* FUNCTION dump */
   87     int i;
   88     int j;
   89     for (i=0; i<(2+d[X]); i++)
   90         {
   91             /* read each row */
   92             printf("%d:", i);
   93             for (j=0; j<(2+d[Y]); j++)
   94                 {
   95                     /* read each col */
   96                     printf(" %d", m[i][j]);
   97                 } /* read each col */
   98             printf("\n");
   99         } /* read each row */
  100 } /* FUNCTION dump */
  101 
  102 void getInput()
  103 {
  104     /* FUNCTION getInput */
  105     int i;
  106     int j;
  107     char line[133];
  108 
  109     scanf(" %d %d ", &d[X], &d[Y]);
  110     scanf(" %d %d ", &a[X], &a[Y]);
  111     scanf(" %d %d ", &g[X], &g[Y]);
  112     for (i=1; i<=d[X]; i++)
  113         {
  114             /* read each row */
  115             scanf(" %s ", line);
  116             DEBUG printf("line [%s]\n", line);
  117             for (j=0; j<d[Y]; j++)
  118                 {
  119                     /* read each col */
  120                     m[i][j+1] = line[j] - '0';
  121                 } /* read each col */
  122         } /* read each row */
  123 } /* FUNCTION getInput */
  124 
  125 void process()
  126 {
  127     /* FUNCTION process */
  128     int i;
  129     int keepGoing = TRUE;
  130     int solved = FALSE;
  131     int d = R;  /* direction */
  132     int x;
  133     int y;
  134 
  135     solved = (a[X] == g[X]) && (a[Y] == g[Y]);
  136     while ((! solved) && (keepGoing))
  137         {
  138             /* solve maze */
  139             printf(" (%d, %d) %c ", a[X], a[Y], direction[d]);
  140             x = a[X] + turn[d][X];
  141             y = a[Y] + turn[d][Y];
  142             if (OPEN == m[x][y])
  143                 {
  144                     /* left turn okay */
  145                     d = (d + 1) % 4;
  146                     printf(" - turn left\n");
  147                 } /* left turn okay */
  148             else
  149                 {
  150                     /* try move */
  151                     m[a[X]][a[Y]] = 2;
  152                     x = a[X] + move[d][X];
  153                     y = a[Y] + move[d][Y];
  154                     if (OPEN == m[x][y])
  155                         {
  156                             /* make move */
  157                             printf(" - move forward\n");
  158                             m[a[X]][a[Y]] = 2;
  159                             a[X] = x;
  160                             a[Y] = y;
  161                         } /* make move */
  162                     else
  163                         {
  164                             /* turn right */
  165                             printf(" - turn right\n");
  166                             d = (d + 3) % 4;
  167                         } /* turn right */
  168                 } /* try move */
  169             solved = (a[X] == g[X]) && (a[Y] == g[Y]);
  170         } /* solve maze */
  171     if (solved)
  172         {
  173             printf("1\n");
  174         }
  175     else
  176         {
  177             printf("0\n");
  178         }
  179 
  180 } /* FUNCTION process */
  181 
  182 int main()
  183 {
  184     /* main */
  185     int moreToDo;
  186 
  187     init();
  188     getInput();
  189     process();
  190 
  191     return EXIT_SUCCESS;
  192 } /* main */
  193