Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/115/11581/a.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 } /* FUNCTION init */
   39 
   40 void dump()
   41 {
   42     /* FUNCTION dump */
   43     int tmp;
   44 
   45     printf("%d %d %d\n", grid[1][1], grid[1][2], grid[1][3]);
   46     printf("%d %d %d\n", grid[2][1], grid[2][2], grid[2][3]);
   47     printf("%d %d %d\n", grid[3][1], grid[3][2], grid[3][3]);
   48     tmp = convertGrid();
   49     printf("%d %X\n", tmp, tmp);
   50     for (tmp=0; 512>tmp; tmp++)
   51         {
   52             if (UNKNOWN != marks[tmp])
   53                 {
   54                     printf("marks[%d] = %d\n", tmp, marks[tmp]);
   55                 }
   56         }
   57 } /* FUNCTION dump */
   58 
   59 void getInput()
   60 {
   61     /* FUNCTION getInput */
   62     int tmp;
   63 
   64     scanf(" %d ", &tmp);
   65     grid[1][3] = tmp % 10;
   66     tmp = tmp / 10;
   67     grid[1][2] = tmp % 10;
   68     grid[1][1] = tmp / 10;
   69 
   70     scanf(" %d ", &tmp);
   71     grid[2][3] = tmp % 10;
   72     tmp = tmp / 10;
   73     grid[2][2] = tmp % 10;
   74     grid[2][1] = tmp / 10;
   75 
   76     scanf(" %d ", &tmp);
   77     grid[3][3] = tmp % 10;
   78     tmp = tmp / 10;
   79     grid[3][2] = tmp % 10;
   80     grid[3][1] = tmp / 10;
   81 } /* FUNCTION getInput */
   82 
   83 void resetMarks()
   84 {
   85     /* FUNCTION resetMarks */
   86     int i;
   87 
   88     marks[0] = -1;
   89     for (i=1; 512>i; i++)
   90         {
   91             marks[i] = UNKNOWN;
   92         }
   93 } /* FUNCTION resetMarks */
   94 
   95 int convertGrid()
   96 {
   97     /* FUNCTION convertGrid */
   98     int ret;
   99 
  100     ret = (256 * grid[1][1]) + (128 * grid[1][2]) + (64  * grid[1][3]) +
  101           (32  * grid[2][1]) + (16  * grid[2][2]) + (8   * grid[2][3]) +
  102           (4   * grid[3][1]) + (2   * grid[3][2]) + (1   * grid[3][3]);
  103     return ret;
  104 } /* FUNCTION convertGrid */
  105 
  106 int g()
  107 {
  108     /* FUNCTION g */
  109     tgrid[1][1] = grid[1][1];
  110     tgrid[1][2] = grid[1][2];
  111     tgrid[1][3] = grid[1][3];
  112     tgrid[2][1] = grid[2][1];
  113     tgrid[2][2] = grid[2][2];
  114     tgrid[2][3] = grid[2][3];
  115     tgrid[3][1] = grid[3][1];
  116     tgrid[3][2] = grid[3][2];
  117     tgrid[3][3] = grid[3][3];
  118     grid[1][1] = (tgrid[1][2] + tgrid[2][1]) % 2;
  119     grid[1][2] = (tgrid[1][1] + tgrid[1][3] + tgrid[2][2]) % 2;
  120     grid[1][3] = (tgrid[1][2] + tgrid[2][3]) % 2;
  121     grid[2][1] = (tgrid[1][1] + tgrid[2][2] + tgrid[3][1]) % 2;
  122     grid[2][2] = (tgrid[1][2] + tgrid[2][1] + tgrid[2][3] + tgrid[3][2]) % 2;
  123     grid[2][3] = (tgrid[1][3] + tgrid[2][2] + tgrid[3][3]) % 2;
  124     grid[3][1] = (tgrid[2][1] + tgrid[3][2]) % 2;
  125     grid[3][2] = (tgrid[2][2] + tgrid[3][1] + tgrid[3][3]) % 2;
  126     grid[3][3] = (tgrid[2][3] + tgrid[3][2]) % 2;
  127 } /* FUNCTION g */
  128 
  129 int solve(int cnt)
  130 {
  131     /* FUNCTION solve */
  132     int num;
  133 
  134     num = convertGrid();
  135     DEBUG    printf("cnt = %d   ", cnt);
  136     DEBUG    printf("num = %d\n", num);
  137     DEBUG dump();
  138     if (UNKNOWN == marks[num])
  139         {
  140             /* if I do not know count */
  141             g();
  142             cnt = 1 + solve(cnt);
  143             DEBUG    printf(".cnt = %d   ", cnt);
  144             DEBUG    printf(".num = %d\n", num);
  145             marks[num] = cnt;
  146         } /* if I do not know count */
  147     else
  148         {
  149             /* already solved */
  150             cnt = cnt + marks[num];
  151         } /* already solved */
  152     DEBUG dump();
  153     return cnt;
  154 } /* FUNCTION solve */
  155 
  156 void process()
  157 {
  158     /* FUNCTION process */
  159     int num;
  160 
  161     DEBUG dump();
  162     num = convertGrid();
  163     if (UNKNOWN == marks[num])
  164         {
  165             /* we do not know this one yet */
  166             marks[num] = solve(0);
  167         } /* we do not know this one yet */
  168     printf("%d\n", marks[num]);
  169 } /* FUNCTION process */
  170 
  171 int main()
  172 {
  173     /* main */
  174     int i;
  175 
  176     init();
  177     resetMarks();
  178     for (i=0; i<numberOfTimes; i++)
  179         {
  180             /* while */
  181             getInput();
  182             process();
  183         } /* while */
  184 
  185     return EXIT_SUCCESS;
  186 } /* main */
  187 
  188