Cod sursa(job #1468806)

Utilizator FairPlay94George Cioroiu FairPlay94 Data 7 august 2015 00:30:18
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <climits>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <map>
#include <algorithm>

using namespace std;

int ans = -100000000;

void bt(int** t, int n, int m, int* p, int poz) {
    if (poz > n) {
        int** aux = new int[n + 1];
        for (int i = 0; i <= n; i++)
            aux[i] = new int[m + 1];
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                aux[i][j] = p[i] * t[i][j];
        int total = 0;
        for (int j = 1; j <= m; j++) {
            int s = 0;
            for (int i = 1; i <= n; i++) {
                s += aux[i][j];
            }
            if (s < 0)
                s = -s;
            total += s;
        }
        if (total > ans)
            ans = total;
        return;
    }
    p[poz] = 1;
    bt(t, n, m, p, poz + 1);
    p[poz] = -1;
    bt(t, n, m, p, poz + 1);
}

int main() {
    freopen("flip.in", "r", stdin);
    freopen("flip.out", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;
    int** t = new int*[n + 1];
    for (int i = 0; i <= n; i++)
        t[i] = new int[m + 1];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> t[i][j];
    int* p = new int[n + 1];
    bt(t, n, m, p, 1);
    cout << ans;

    return 0;
}