Computer Programming Contest Preparation

ToolBox - Source for: 1/108/d.c



/home/toolbox/public_html/solutions/1/108/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 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 #define MAX_SIZE 105
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2014-10-29
   20  * Purpose: fun
   21  * Problem: 108 - Maximum Sum  - more to be done
   22  */
   23 
   24 /*
   25  * This template reads data until a terminating value is reached.
   26  */
   27 
   28 int dim;
   29 int a[MAX_SIZE][MAX_SIZE];
   30 
   31 void init()
   32 {
   33     /* FUNCTION init */
   34 } /* FUNCTION init */
   35 
   36 void dump()
   37 {
   38     /* FUNCTION dump */
   39 } /* FUNCTION dump */
   40 
   41 int getInput()
   42 {
   43     /* FUNCTION getInput */
   44     int i;
   45     int j;
   46     int moreToDo = FALSE;
   47 
   48     i = scanf(" %d ", &dim);
   49     if (0 < i)
   50         {
   51             /* Read in a dimension */
   52             moreToDo = TRUE;
   53             for (i=0; i<dim; i++)
   54                 for (j=0; j<dim; j++)
   55                     {
   56                         /* get each value */
   57                         scanf(" %d ", &a[i][j]);
   58                     } /* get each value */
   59         } /* Read in a dimension */
   60     return moreToDo;
   61 } /* FUNCTION getInput */
   62 
   63 int findMax(int x1, int x2, int y1, int y2)
   64 {
   65     /* FUNCTION findMax */
   66     int i;
   67     int j;
   68     int tot = 0;
   69 
   70     for (i=x1; i<=x2; i++)
   71         for (j=y1; j<=y2; j++)
   72             {
   73                 /* each cell */
   74                 tot = tot + a[i][j];
   75             } /* each cell */
   76     return tot;
   77 } /* FUNCTION findMax */
   78 
   79 void process()
   80 {
   81     /* FUNCTION process */
   82     int mx;
   83     int i1, i2;
   84     int j1, j2;
   85     int tmp;
   86 
   87     mx = a[0][0];
   88     for (i1=0; i1<dim; i1++)
   89         {
   90             /* brute force i start */
   91             for (i2=i1; i2<dim; i2++)
   92                 {
   93                     /* brute force i end */
   94                     for (j1=0; j1<dim; j1++)
   95                         {
   96                             /* j start */
   97                             for (j2=j1; j2<dim; j2++)
   98                                 {
   99                                     /* end point of j */
  100                                     tmp = findMax(i1, i2, j1, j2);
  101                                     printf("[%d-%d,%d-%d] = %d\n", i1, i2, j1, j2, tmp);
  102                                     if (tmp > mx)
  103                                         {
  104                                             mx = tmp;
  105                                         }
  106                                 } /* end point of j */
  107                         } /* j start */
  108                 } /* brute force i end */
  109         } /* brute force i start */
  110 
  111     printf("%d\n", mx);
  112 } /* FUNCTION process */
  113 
  114 int main ()
  115 {
  116     /* main */
  117     int moreToDo;
  118 
  119     init();
  120     moreToDo = getInput();
  121     while (moreToDo)
  122         {
  123             /* process as long as input continues */
  124             process();
  125             moreToDo = getInput();
  126         } /* process as long as input continues */
  127 
  128     return EXIT_SUCCESS;
  129 } /* main */
  130