Cod sursa(job #363952)

Utilizator danvlnDan Vln danvln Data 15 noiembrie 2009 06:02:33
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.12 kb
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include "Util.h"


int Sum(int **m, int row, int col)
{
    // Calculate the sum
    int s = 0;
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            s += m[i][j];
        }
    }
    return s;
}

void rec(int **m, int row, int col, int ii, int jj, int *s)
{
    int ss = 0;
    if(ii > row - 1) return;
    if(jj > col -1 ) return;
    ss = Sum(m, row, col);
    if( ss > *s ) 
        *s = ss;

    for(int i = 0; i < row; i++)
    {
        m[i][jj] *= -1;
    }
    rec(m, row, col, ii, ++jj, s);

    for(int j = 0; j < col; j++)
    {
        m[ii][j] *= -1;
    }
    rec(m, row, col, ++ii, jj, s);
}

int main()
{
    FILE * m_pFileIn, * m_pFileOut;
    m_pFileIn = fopen ("flip.in","r");
    int row = 0, col = 0;
    // read row and col 
    fscanf(m_pFileIn, "%d %d", &row, &col);
    // read the elements
    int **m = (int**) malloc(sizeof(int*) * row);
    for( int i = 0; i < row; i++ )
    {
        m[i] = (int*)malloc(sizeof(int*)*col);
        for(int j = 0; j < col; j++)
        {
            fscanf(m_pFileIn, "%d", &m[i][j]);
        }
    }
     
    int s = 0 ;  
    
    rec(m, row, col, 0, 0, &s);
    printf("%d",s);
    
    m_pFileOut = fopen ("flip.out","w");
    fprintf(m_pFileOut, "%d", s);
    
    return 0 ;
     //Util::Print(m, row, col);
    
    //int k = 0, s = 0, ss = 0 ;
    //while( k < 150000 ) 
    //{
    //    //cols 
    //    int *c1 = (int*)malloc(sizeof(int)*col);
    //    int *c2 = (int*)malloc(sizeof(int)*col);
    //    for(int j = 0; j < col; j++)
    //    {
    //        c1[j] = c2[j] = 0;
    //        for(int i = 0; i < row; i++)
    //        {
    //            c1[j] += m[i][j];
    //            c2[j] += (-1) * m[i][j];
    //        }
    //    }
    //    
    //    //Util::Print(m, row, col);
    //    for(int j = 0; j < col; j++)
    //    {
    //        if( c2[j] >= c1[j] )
    //        {
    //            for(int i = 0; i < row; i++)
    //            {
    //                m[i][j] *= -1;
    //            }
    //        }
    //    }
    //   // Util::Print(m, row, col);
    //    ss = Sum(m, row, col);
    //    if( ss > s ) s = ss;
    //    
    //
    //    //rows
    //    int *r1 = (int*)malloc(sizeof(int)*row);
    //    int *r2 = (int*)malloc(sizeof(int)*row);
    //    for(int i = 0; i < row; i++)
    //    {
    //        r1[i] = r2[i] = 0;
    //        for(int j = 0; j < col; j++)
    //        {
    //            r1[i] += m[i][j];
    //            r2[i] += (-1) * m[i][j];
    //        }
    //    }
    //    //Util::Print(m, row, col);
    //    for(int i = 0; i < row; i++)
    //    {
    //        if(r2[i] >= r1[i])
    //        {
    //            for(int j = 0; j < col; j++)
    //            {
    //                m[i][j] *= -1;
    //            }
    //        }
    //    }
    //    //Util::Print(m, row, col);
    //    ss = Sum(m, row, col);
    //    if( ss > s ) s = ss;

    //    k++;
    //}
    
    return 0;
}