Cod sursa(job #2478202)

Utilizator iustin948Homoranu Iustin iustin948 Data 21 octombrie 2019 19:12:52
Problema Jocul Flip Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
/// BRUT

#include <iostream>
#include <fstream>
using namespace std;

ifstream fin("flip.in");
ofstream fout("flip.out");
int n, m ,a[20][20], stn[105], stm[105], b[20][20], nr, summax = -1E9;

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

void sum()
{
    int sum = 0;
     for(int i=1; i <= n; i++)
        for(int j = 1; j <= m; j++)
                sum += b[i][j];

    summax = max(sum, summax);
}

void Afisare(int top)
{

    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++)
        b[i][j] = a[i][j];

    for(int i = 1; i <= top; i++)
        for(int j = 1; j <= m; j++)
            b[stm[i]][j] *= -1;

    for(int i = 1; i <= nr; i++)
        for(int j = 1; j <= n; j++)
            b[j][stn[i]] *= -1;

    sum();
}

BackM(int top)
{
    if(top > 1)
        {
            Afisare(top-1);
        }
    for(int i = stm[top-1]+1; i <= m; i++)
    {
        stm[top] = i;
        BackM(top + 1);
    }
}

void BackN(int top)
{
    if(top > 1)
        {
            nr = top-1;;
            BackM(1);
        }
    for(int i = stn[top-1]+1; i <= n; i++)
    {
        stn[top] = i;
        BackN(top + 1);
    }
}

int main()
{
    Read();
    BackN(1);
    fout << summax << "\n";
    return 0;
}