Cod sursa(job #2253259)

Utilizator jason2013Andronache Riccardo jason2013 Data 3 octombrie 2018 20:17:40
Problema Jocul Flip Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.72 kb
#include<bits/stdc++.h>

using namespace std;

const int N_MAX = 19, M_MAX = 19;
int initialMat[N_MAX][M_MAX];

void changeSignRow(int row, int N)
{
    for(int i = 1; i <= N; i ++)
        initialMat[row][i] = (-1) * initialMat[row][i];
}

void changeSignColumn(int column, int M)
{
    for(int i = 1; i <= M; i ++)
        initialMat[i][column] = (-1) * initialMat[i][column];
}

void print(int N, int M)
{
    ofstream fout("flip.out");

    int best = 0;
    for(int i = 1; i <= N; i ++){
        for(int j = 1; j <= M; j ++)
            best += initialMat[i][j];
    }
    fout << best;
    fout.close();
}

int main()
{
    ifstream fin ("flip.in");

    int N, M;
    fin >> N >> M;
    for(int i = 1; i <= N; i ++)
        for(int j = 1; j <= M; j++)
            fin >> initialMat[i][j];
    int positiveValues, negativeValues;

    for(int i = 1; i <= N; i ++ )
    {
        positiveValues = negativeValues = 0;
        for(int j = 1; j <= M; j ++){
            if( initialMat[i][j] > 0 ) positiveValues += initialMat[i][j];
            else negativeValues += initialMat[i][j];
        }
        negativeValues = (-1)*negativeValues;
        if( negativeValues > positiveValues )
            changeSignRow( i, N );
    }

    for(int j = 1; j <= M; j ++)
    {
        positiveValues = negativeValues = 0;
        for(int i = 1; i <= N; i ++)
        {
            if( initialMat[i][j] > 0 ) positiveValues += initialMat[i][j];
            else negativeValues += initialMat[i][j];
        }
        negativeValues = (-1)*negativeValues;
        if( negativeValues > positiveValues )
            changeSignColumn( j, N );
    }

    print(N, M);

    fin.close();

    return 0;
}