Cod sursa(job #2535949)

Utilizator matthriscuMatt . matthriscu Data 1 februarie 2020 13:08:18
Problema Jocul Flip Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>
#include <math.h>
using namespace std;

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

int n, m, P, a[17][17], v[33], s, maxx = 0;

void dtb(int x) {
    int i = n+m;
    while(x) {
        v[i] = x % 2;
        x /= 2;
        --i;
    }
}

int abs(int x) {
    if(x < 0)
        return -x;
    return x;
}

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

    P = pow(2, n+m);

    for(int p = 0; p < P; ++p) {
        s = 0;
        dtb(p);
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= m; ++j)
                if(v[i] & v[j+n])
                    s += a[i][j];
                else
                    s -= a[i][j];
        if(abs(s) > maxx)
            maxx = abs(s);
    }
    fout << maxx;
}