Cod sursa(job #2221659)

Utilizator AlexPop28Pop Alex-Nicolae AlexPop28 Data 15 iulie 2018 13:19:41
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back

using namespace std;

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

const int NMAX = 16;
int n, m, ans;
int a[NMAX][NMAX];
int flipCol[NMAX];

void getSum() {
    int sum = 0;
    for (int i = 0; i < n; ++i) {
        int sumRow = 0;
        for (int j = 0; j < m; ++j) {
            sumRow += flipCol[j] * a[i][j];
        }
        sumRow *= (sumRow < 0) ? (-1) : 1;
        sum += sumRow;
    }
    ans = max (ans, sum);
}

void backtr (int col = 0) {
    if (col == m) {
        getSum();
        return;
    }
    flipCol[col] = -1;
    backtr (col + 1);
    flipCol[col] = 1;
    backtr (col + 1);
}

int main() {
    f >> n >> m;
    for (int i = 0; i < n; ++i) {
        for (int j = 0 ; j < m; ++j) {
            f >> a[i][j];
        }
    }
    ans = -(1 << 30);
    backtr();
    g << ans << '\n';

    f.close();
    g.close();
    return 0;
}