Cod sursa(job #1779638)

Utilizator cristicretancristi cretan cristicretan Data 15 octombrie 2016 15:10:18
Problema Jocul Flip Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>
#define NMax 17
using namespace std;

ifstream f("flip.in");
ofstream g("flip.out");

int n, st[NMax],m, s, mx = -12343, p;
long a[NMax][NMax];

void afisare()
{
    p = 0;
    for(int i = 1; i <= n; ++i)
    {
        s = 0;
        for(int j = 1; j <= m; ++j)
                s += a[i][j];
        p +=  abs(s);
    }
    if(mx < p) mx = p;
}

void comutare(int k)
{
    for(int i = 1; i <= n; ++i)
        a[i][st[k]] = - a[i][st[k]];
}

void back(int k)
{
    for(int i = 1; i <= n; ++i)
    {
        st[i] = i;
            if(k == m) afisare();
                else
                {
                    back(k + 1);
                    comutare(k);
                }
    }
}
int main()
{
    f >> n >> m;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            f >> a[i][j];
    back(1);
    g << mx;
    return 0;
}