Computer Programming Contest Preparation

ToolBox - Source for: 115/11581/b.c



/home/toolbox/public_html/solutions/115/11581/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /* fprintf(stderr, "functionName: message", varslist); */
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2017-01-25
   20  * Purpose: fun
   21  * Problem: 11581 - Grid Successors
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 #define UNKNOWN -99
   28 
   29 int numberOfTimes;
   30 int grid[5][5];
   31 int tgrid[5][5];
   32 int marks[512];
   33 
   34 void init()
   35 {
   36     /* FUNCTION init */
   37     scanf("%d ", &numberOfTimes);
   38     grid[0][0] = 0;
   39     grid[0][1] = 0;
   40     grid[0][2] = 0;
   41     grid[0][3] = 0;
   42     grid[0][4] = 0;
   43     grid[1][0] = 0;
   44     grid[1][4] = 0;
   45     grid[2][0] = 0;
   46     grid[2][4] = 0;
   47     grid[3][0] = 0;
   48     grid[3][4] = 0;
   49     grid[4][0] = 0;
   50     grid[4][1] = 0;
   51     grid[4][2] = 0;
   52     grid[4][3] = 0;
   53     grid[4][4] = 0;
   54     tgrid[0][0] = 0;
   55     tgrid[0][1] = 0;
   56     tgrid[0][2] = 0;
   57     tgrid[0][3] = 0;
   58     tgrid[0][4] = 0;
   59     tgrid[1][0] = 0;
   60     tgrid[1][4] = 0;
   61     tgrid[2][0] = 0;
   62     tgrid[2][4] = 0;
   63     tgrid[3][0] = 0;
   64     tgrid[3][4] = 0;
   65     tgrid[4][0] = 0;
   66     tgrid[4][1] = 0;
   67     tgrid[4][2] = 0;
   68     tgrid[4][3] = 0;
   69     tgrid[4][4] = 0;
   70 } /* FUNCTION init */
   71 
   72 void dump()
   73 {
   74     /* FUNCTION dump */
   75 } /* FUNCTION dump */
   76 
   77 void getInput()
   78 {
   79     /* FUNCTION getInput */
   80     scanf(" %d ", &grid[1][1]);
   81     scanf(" %d ", &grid[1][2]);
   82     scanf(" %d ", &grid[1][3]);
   83     scanf(" %d ", &grid[2][1]);
   84     scanf(" %d ", &grid[2][2]);
   85     scanf(" %d ", &grid[2][3]);
   86     scanf(" %d ", &grid[3][1]);
   87     scanf(" %d ", &grid[3][2]);
   88     scanf(" %d ", &grid[3][3]);
   89 } /* FUNCTION getInput */
   90 
   91 void resetMarks()
   92 {
   93     /* FUNCTION resetMarks */
   94     int i;
   95 
   96     marks[0] = -1;
   97     for (i=1; 512>i; i++)
   98         {
   99             marks[i] = UNKNOWN;
  100         }
  101 } /* FUNCTION resetMarks */
  102 
  103 int convertGrid()
  104 {
  105     /* FUNCTION convertGrid */
  106     int ret;
  107 
  108     ret =       grid[1][1]  + (2   * grid[1][2]) + (4   * grid[1][3]) +
  109                 (8  * grid[2][1]) + (16  * grid[2][2]) + (32  * grid[2][3]) +
  110                 (64 * grid[3][1]) + (128 * grid[3][2]) + (256 * grid[3][3]);
  111     return ret;
  112 } /* FUNCTION convertGrid */
  113 
  114 int g()
  115 {
  116     /* FUNCTION g */
  117     tgrid[1][1] = grid[1][1];
  118     tgrid[1][2] = grid[1][2];
  119     tgrid[1][3] = grid[1][3];
  120     tgrid[2][1] = grid[2][1];
  121     tgrid[2][2] = grid[2][2];
  122     tgrid[2][3] = grid[2][3];
  123     tgrid[3][1] = grid[3][1];
  124     tgrid[3][2] = grid[3][2];
  125     tgrid[3][3] = grid[3][3];
  126     grid[1][1] = (tgrid[1][2] + tgrid[2][1]) % 2;
  127     grid[1][2] = (tgrid[1][1] + tgrid[1][3] + tgrid[2][2]) % 2;
  128     grid[1][3] = (tgrid[1][2] + tgrid[2][3]) % 2;
  129     grid[2][1] = (tgrid[1][1] + tgrid[2][2] + tgrid[3][1]) % 2;
  130     grid[2][2] = (tgrid[1][2] + tgrid[2][1] + tgrid[2][3] + tgid[3][2]) % 2;
  131     grid[2][3] = (tgrid[1][3] + tgrid[2][2] + tgrid[3][3]) % 2;
  132     grid[3][1] = (tgrid[2][1] + tgrid[3][2]) % 2;
  133     grid[3][2] = (tgrid[2][2] + tgrid[3][1] + tgrid[3][3]) % 2;
  134     grid[3][3] = (tgrid[2][3] + tgrid[3][2]) % 2;
  135 
  136 } /* FUNCTION g */
  137 
  138 int solve()
  139 {
  140     /* FUNCTION solveg */
  141     int num;
  142     int cnt = 0;
  143 
  144     num = convertGrid();
  145     if (UNKNOWN == marks[num])
  146         {
  147             /* if I do not know count */
  148             cnt = cnt + g();
  149             num = convertGrid();
  150             marks[num] = cnt;
  151         } /* if I do not know count */
  152     else
  153         {
  154         } /* FUNCTION solveg */
  155 
  156     int transform()
  157     {
  158         /* FUNCTION transform */
  159         int ret;
  160         int num;
  161 
  162         num = convertGrid();
  163         if (UNKNOWN == marks[num])
  164             {
  165                 /* we do not know this one yet */
  166                 marks[num] = solve();
  167             } /* we do not know this one yet */
  168         ret = marks[num];
  169         return ret;
  170     } /* FUNCTION transform */
  171 
  172     void process()
  173     {
  174         /* FUNCTION process */
  175         int iCnt = -1;
  176 
  177         while (nonZero())
  178             {
  179                 /* while */
  180                 iCnt++;
  181                 mark();
  182                 transform();
  183             } /* while */
  184         printf("%d\n", iCnt);
  185     } /* FUNCTION process */
  186 
  187     int main()
  188     {
  189         /* main */
  190         int i;
  191 
  192         init();
  193         for (i=0; i<numberOfTimes; i++)
  194             {
  195                 /* while */
  196                 getInput();
  197                 process();
  198             } /* while */
  199 
  200         return EXIT_SUCCESS;
  201     } /* main */
  202 
  203