Cod sursa(job #1194183)

Utilizator rumorsofmydemisehavebeengreatlyexaggerated rumorsofmydemise Data 3 iunie 2014 03:57:06
Problema Jocul Flip Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1 kb
#include <stdio.h>
int n, m, max;
int a[16][16];
int x[16];

void back(int i)
{
    int j;

    if(i < n) {
        // test direct
        for(j=0; j<m; j++)
            x[j] += a[i][j];
        back(i+1);
        // test reverse
        for(j=0; j<m; j++)
            x[j] -= 2 * a[i][j];
        back(i+1);
        // restore previous state
        for(j=0; j<m; j++)
            x[j] += a[i][j];
    }
    else {
        // end of the line, determine sum
        int sum = 0;
        for(j=0; j<m; j++)
            if(x[j] < 0)
                sum -= x[j];
            else
                sum += x[j];
        // and compare to current best
        if(sum > max)
            max = sum;
    }
}

int main(int argc, char **argv)
{
    FILE *f = fopen("flip.in", "rb");
    fscanf(f, "%d %d", &n, &m);
    int i, j;
    for(i=0; i<n; i++)
        for(j=0; j<m; j++)
            fscanf(f, "%d", &a[i][j]);

    fclose(f);
    back(0);

    f = fopen("flip.out", "wb");
    fprintf(f, "%d\n", max);
    fclose(f);
    return 0;
}