Cod sursa(job #3162708)

Utilizator wappy86Cristian Florea wappy86 Data 29 octombrie 2023 18:21:09
Problema Jocul Flip Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

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

int n, m;
long a[17][17], smax, x[17];

void printMatrice()
{
    long total, gtotal = 0;
    for (int i = 1; i <= n; i++)
    {
        total = 0;
        for (int j = 1; j <= m; j++)
        {
            cout << a[i][j] << "\t";
            total += a[i][j];
            gtotal += a[i][j];
        }
        cout << "Total: " << total << "\n";
    }

    cout << "********************"
         << "\n";
    for (int j = 1; j <= m; j++)
    {
        total = 0;
        for (int i = 1; i <= n; i++)
        {
            total += a[i][j];
        }

        cout << total << "\t";
    }

    cout << "\n"
         << "TOTAL:" << gtotal
         << "\n";
}

void rezolva()
{
    int S = 0;
    for (int i = 1; i <= n; ++i)
    {
        int s = 0;
        for (int j = 1; j <= m; ++j)
            if (x[j])
                s -= a[i][j];
            else
                s += a[i][j];
        if (s < 0)
            S -= s;
        else
            S += s;
    }
    if (S > smax)
        smax = S;
}

void back()
{
    int k = 1;
    x[k] = -1;
    do
    {
        while (x[k] < 1)
        {
            x[k]++;
            if (k == m)
                rezolva();
            else
                x[++k] = -1;
        }
        k--;
    } while (k);
}

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

    back();
    fout << smax;

    fin.close();
    fout.close();

    return 0;
}