Computer Programming Contest Preparation

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



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