Cod sursa(job #1611967)

Utilizator razvandRazvan Dumitru razvand Data 24 februarie 2016 16:55:24
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int a[17];
int ma[17][17];
int n,m;
int d;
int mx;

void calc() {
    int stot = 0;
    int sum = 0;
	for(int i = 1; i <= n; i++) {
        sum = 0;
        for(int j = 1; j <= m; j++)
            sum += ma[i][j]*a[j];
        if(sum > 0)
            stot += sum;
        else
            stot -= sum;
	}
	mx = max(mx, stot);
}

void Back(int top) {
	if (top == d + 1) {
        calc();
        return;
	}
    a[top] = 1;
    Back(top+1);
    a[top] = -1;
    Back(top+1);
}

int main() {
    in >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            in >> ma[i][j];
    d = m;
	Back(1);
	out << mx;
	return 0 ;
}