Cod sursa(job #363942)

Utilizator danvlnDan Vln danvln Data 15 noiembrie 2009 04:45:02
Problema Jocul Flip Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.15 kb
#include <stdlib.h>
#include <stdio.h>
//#include "Util.h"

int main()
{
    FILE * m_pFileIn, * m_pFileOut;
    m_pFileIn = fopen ("flip.in","r");
    if (m_pFileIn == NULL)
    {
        //goto cleanup;
    }
    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 *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);
    
    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);
    

    // Calculate the sum
    int s = 0;
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            s += m[i][j];
        }
    }
    
    m_pFileOut = fopen ("flip.out","w");
    fprintf(m_pFileOut, "%d", s);

/*  
cleanup :
    if(m_pFileIn != NULL)
    {
        fclose (m_pFileIn);
    }
    if(m_pFileOut != NULL)
    {
        fclose(m_pFileOut);
    }
*/    
    return 0;
}