Cod sursa(job #2407426)

Utilizator BossBobsterRobert Alexandru Costin BossBobster Data 16 aprilie 2019 21:05:36
Problema Jocul Flip Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin("flip.in");
ofstream fout("flip.out");

int coltot[16];
int rowtot[16];
int matrix[16][16];
int n, m, total = 0;
bool switched = false;

void flip()
{
    switched = false;
    for(int i = 0; i < n; i ++)
    {
        if(rowtot[i] < 0)
        {
            switched = true;
            rowtot[i] = 0;
            for(int j = 0; j < m; j ++)
            {
                matrix[i][j] = matrix[i][j] * -1;
                rowtot[i] += matrix[i][j];
            }
        }
    }
    for(int i = 0; i < m; i ++)
    {
        if(coltot[i] < 0)
        {
            switched = true;
            coltot[i] = 0;
            for(int j = 0; j < n; j ++)
            {
                matrix[j][i] = matrix[j][i] * -1;
                coltot[i] += matrix[j][i];
            }
        }
    }
    if(!switched)
        flip();
}

int main()
{
    fin >> n >> m;
    //cin >> n >> m;
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < m; j ++)
        {
            //cin >> matrix[i][j];
            fin >> matrix[i][j];
            coltot[j] += matrix[i][j];
            rowtot[i] += matrix[i][j];
        }
    }
    flip();
    total = 0;
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < m; j ++)
        {
            total += matrix[i][j];
        }
    }
    fout << total << endl;
    //cout << total;
}