Cod sursa(job #3311441)

Utilizator corvinus2003Corvin Ghita corvinus2003 Data 22 septembrie 2025 13:39:43
Problema Jocul Flip Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <iostream>
#include <fstream>

using namespace std;

void helloWorld() {

    const auto lang = "C++";
    cout << "Hello and welcome to " << lang << "!\n";

    for (int i = 1; i <= 5; i++) {

        cout << "i = " << i << endl;
    }
}

/*
 *FLIP: inmulteste linii, coloane cu -1 asa incat sa ajungi la suma cea mai mare
 *
 *problema: pot strica toate nr de pe prima linie si sa reintorc semnu la primu numar daca inmultesc
 *coloana 1
 *
 *
*/

int n, m;
int a[17][17];

int main() {

    //ifstream fin("text.in");
    //ofstream fout("text.out");

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

    fin >> n >> m;

    int s_max = 0;

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            fin >> a[i][j];
            s_max += a[i][j];
        }
    }

    //schimb in dementa semnul pana cand suma a ramas identica

    int s = 0, schimb = 1;

    while (schimb) {

        s = schimb = 0;
        int s_temp = 0;
        //probez liniile
        for (int i = 1; i <= n; ++i) {
            s_temp = 0;
            for (int j = 1; j <= m; ++j) {
                s_temp += a[i][j];
            }
            if (s_temp < 0) {
                //fout << "linia " << i << "\n";
                for (int j = 1; j <= m; ++j) {
                    a[i][j] *= -1;
                }
            }
        }
        //probez coloanele
        for (int j = 1; j <= m; ++j) {
            s_temp = 0;
            for (int i = 1; i <= n; ++i) {
                s_temp += a[i][j];
            }
            if (s_temp < 0) {
                //fout << "coloana " << j << "\n";
                for (int i = 1; i <= n; ++i) {
                    a[i][j] *= -1;
                }
            }
        }
        //adun matricea noua
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                s += a[i][j];
            }
        }
        //verific daca am prins o suma mai buna
        if (s > s_max) {
            s_max = s;
            schimb = 1;
        }
    }

    fout << s_max << '\n';

    return 0;
}