Cod sursa(job #1694559)

Utilizator Vele_GeorgeVele George Vele_George Data 25 aprilie 2016 16:43:34
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <iostream>
#include <fstream>
#define nmax 50
#define inf (1<<30)
using namespace std;

short v[nmax];
int a[17][17], mx = -inf, n, m;

void verif()
{
    int sum = 0, s_pozitiv = 0, s_negativ = 0;

    for(int j=1; j<=m; j++)
    {
        s_pozitiv = 0;
        s_negativ = 0;
        for(int i=1; i<=n; i++)
        {
            s_pozitiv += (a[i][j] * v[i]);
            s_negativ += (a[i][j] * v[i] * -1);
        }
        sum += max(s_pozitiv, s_negativ);
    }
    if (sum > mx) mx = sum;
    return;
}

void back(int poz)
{

    if (poz == n)
    {
        v[poz] = 1;
        verif();
        v[poz] = -1;
        verif();
        return;
    }

    v[poz] = 1;
    back(poz + 1);
    v[poz] = -1;
    back(poz + 1);

    return;
}

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

    f >> n >> m;

    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            f >> a[i][j];

   back(1);
    g << mx;

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