Cod sursa(job #1268395)

Utilizator jul123Iulia Duta jul123 Data 20 noiembrie 2014 21:52:17
Problema Jocul Flip Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include<bits/stdc++.h>

using namespace std;

int n, m, a[17][17];
long long maxim = -1000000;
int takec[17], takel[17];
long long calculez() {
    long long s = 0;
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < m; ++j)
            s += ((long long)a[i][j] * takec[j] * takel[i]);
   // cout << s << " ";
    return s;
}
void bktrc(int x) {
    if(x == m) {
       if( calculez() > maxim )
            maxim = calculez();
    } else {
        takec[x] = 1;
        bktrc(x + 1);
        takec[x] = -1;
        bktrc(x + 1);
    }
}
void bktrl(int x) {
    if(x == n) {
        bktrc(0);
    } else {
    takel[x] = 1;
    bktrl(x + 1);
    takel[x] = -1;
    bktrl(x + 1);
    }
}
int main()
{
    FILE *fin = fopen("flip.in", "r");
    FILE *fout = fopen("flip.out", "w");

    fscanf(fin, "%d %d", &n, &m);

    for(int i = 0; i < n; ++i)
        for(int j = 0; j < m; ++j)
            fscanf(fin, "%d", &a[i][j]);
    bktrl(0);
    fprintf(fout, "%lld", maxim);

}