Cod sursa(job #606492)

Utilizator dspMihaiDespotovici Mihai dspMihai Data 4 august 2011 16:23:10
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <iostream>
#include <fstream>
using namespace std;
int M,N,i,j;
long mat[17][17],S;
char n[17];

long modul(long numar)
{
    if (numar<0) return (-1)*numar;
        else return numar;
}

void back (int x)
{
    int l;
    if (x>N)
    {
        long Ss=0,Scoloana,crt;
        for (j=1; j<=M; j++)
            {
                Scoloana=0;
                for (i=1; i<=N; i++)
                    if (n[i]) Scoloana+=mat[i][j];
                        else Scoloana+=mat[i][j]*(-1);
                Ss+=modul(Scoloana);
            }
        if (Ss>S) S=Ss;

    }
    else for (l=0; l<=1; l++)
    {
        n[x]=l;
        back(x+1);
    }
}

int main()
{
    freopen("flip.in", "r", stdin);
    freopen("flip.out", "w", stdout);
    scanf("%d %d", &N, &M);
    for (i=1; i<=N; i++)
        for (j=1;j<=M ;j++) scanf("%d", &mat[i][j]);
    back(1);
    printf("%d\n", S);
    return 0;
}