Cod sursa(job #606481)

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

void back2 (int x1, int x2)
{
    int l,i1,i2;
    if (x2>M)
    {
        unsigned long Ss,crt;
        //for(i1=1; i1<=N; i1++) printf("%d", n[i1]);
        //printf("\n");
        //for(i2=1; i2<=M; i2++) printf("%d", m[i2]);
        Ss=0;
        for (i1=1; i1<=N; i1++)
            for (i2=1; i2<=M; i2++)
                {
                    crt=mat[i1][i2];
                    if (n[i1]) crt*=-1;
                    if (m[i2]) crt*=-1;
                    Ss+=crt;
                }
        long a=Ss, b=S;
        if (a>b) S=Ss;
        //printf("\n");

    }
    else for (l=0; l<=1; l++)
    {
        m[x2]=l;
        back2(x1,x2+1);
    }
}

void back1 (int x)
{
    int l;
    if (x>N)
    {
        long Ss=0,crt;
        for (i=1; i<=N; i++)
            back2(x,1);
    }
    else for (l=0; l<=1; l++)
    {
        n[x]=l;
        back1(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]);
    back1(1);
    printf("%d", S);
    return 0;
}