Computer Programming Contest Preparation

ToolBox - Source for: 5/541/a.c



/home/toolbox/public_html/solutions/5/541/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 #define MAX_SIZE 101
   15 
   16 
   17 /* fprintf(stderr, "functionName: message", varslist); */
   18 
   19 /*
   20  *  Author:
   21  *    Date:
   22  * Purpose:
   23  * Problem:
   24  */
   25 
   26 /*
   27  * This template reads data a specified number of times.
   28  */
   29 
   30 int size;
   31 int mat[MAX_SIZE][MAX_SIZE];
   32 int testAry[3] = {0, 1, 0};
   33 
   34 void init()
   35 {
   36     /* FUNCTION init */
   37 } /* FUNCTION init */
   38 
   39 void dump()
   40 {
   41     /* FUNCTION dump */
   42     int i;
   43     int j;
   44 
   45     printf("\nsize = %d\n", size);
   46     for (i=0; i<size; i++)
   47         {
   48             /* read each row */
   49             for (j=0; j<size; j++)
   50                 {
   51                     /* read each col */
   52                     printf(" %d ", mat[i][j]);
   53                 } /* read each col */
   54             printf("\n");
   55         } /* read each row */
   56 } /* FUNCTION dump */
   57 
   58 void getInput()
   59 {
   60     /* FUNCTION getInput */
   61     int i;
   62     int j;
   63 
   64     scanf("%d ", &size);
   65     for (i=0; i<size; i++)
   66         {
   67             /* read each row */
   68             for (j=0; j<size; j++)
   69                 {
   70                     /* read each col */
   71                     scanf(" %d ", &mat[i][j]);
   72                 } /* read each col */
   73         } /* read each row */
   74 
   75 } /* FUNCTION getInput */
   76 
   77 int testCol(int col)
   78 {
   79     /* FUNCTION testCol */
   80     int i;
   81     int flag = 0;
   82 
   83     for (i=0; i<size; i++)
   84         {
   85             /* for i */
   86             flag = testAry[flag + mat[i][col]];
   87         } /* for i */
   88     return (0 == flag);
   89 } /* FUNCTION testCol */
   90 
   91 int testRow(int row)
   92 {
   93     /* FUNCTION testRow */
   94     int i;
   95     int flag = 0;
   96 
   97     for (i=0; i<size; i++)
   98         {
   99             /* for i */
  100             flag = testAry[flag + mat[row][i]];
  101         } /* for i */
  102     return (0 == flag);
  103 } /* FUNCTION testRow */
  104 
  105 void process()
  106 {
  107     /* FUNCTION process */
  108     int i;
  109     int row = 0;
  110     int col = 0;
  111     int badRow = -1;
  112     int badCol = -1;
  113 
  114     for (i=0; i<size; i++)
  115         {
  116             /* check each row/col */
  117             if (! testRow(i))
  118                 {
  119                     /* row bad */
  120                     row++;
  121                     badRow = i;
  122                 } /* row bad */
  123             if (! testCol(i))
  124                 {
  125                     /* col bad */
  126                     col++;
  127                     badCol = i;
  128                 } /* col bad */
  129         } /* check each row/col */
  130 
  131     if ((0 == row) && (0 == col))
  132         {
  133             /* print ok */
  134             printf("OK\n");
  135         } /* print ok */
  136     else
  137         {
  138             /* not OK */
  139             if ((1 == row) && (1 == col))
  140                 {
  141                     /* correctable */
  142                     printf("Change bit (%d,%d)\n", (badRow+1), (badCol+1));
  143                 } /* correctable */
  144             else
  145                 printf("Corrupt\n");
  146         } /* not OK */
  147 } /* FUNCTION process */
  148 
  149 int main ()
  150 {
  151     /* main */
  152     getInput();
  153     while (0 < size)
  154         {
  155             /* while */
  156             process();
  157             getInput();
  158         } /* while */
  159 
  160     return EXIT_SUCCESS;
  161 } /* main */
  162