Cod sursa(job #1234002)

Utilizator borcanirobertBorcani Robert borcanirobert Data 26 septembrie 2014 16:06:54
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
using namespace std;

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

const int MAXNM = 20;
const int INF = 0x3f3f3f3f;
int a[MAXNM][MAXNM];
int s[MAXNM];
int N, M;
int ms, v;

int ABS( int a );
int Suma();
void Back( int x );

int main()
{
    int i, j;

    fin >> N >> M;
    for ( i = 1; i <= N; i++ )
        for ( j = 1; j <= M; j++ )
            fin >> a[i][j];

    Back(1);

    fout << ms << '\n';

    fin.close();
    fout.close();
    return 0;
}

int ABS( int a )
{
    if ( a > 0 )
        return a;
    return -a;
}

int Suma()
{
    int sum, su; su = sum = 0;
    int i, j;

    for ( i = 1; i <= N; i++ )
    {
        su = 0;
        for ( j = 1; j <= M; j++ )
            su += a[i][j] * s[j];
        sum += ABS( su );
    }

    return sum;
}

void Back( int x )
{
    if ( x == M + 1 )
    {
        v = Suma();
        if ( ms < v )
            ms = v;
    }
    else
    {
        s[x] = 1; Back( x + 1 );
        s[x] = -1; Back( x + 1 );
    }
}