Cod sursa(job #1587647)

Utilizator bogdanardeleanBogdan Ardelean bogdanardelean Data 2 februarie 2016 13:57:20
Problema Jocul Flip Scor 40
Compilator c Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <stdio.h>
#include <stdlib.h>

int N,M;
int mat[17][17];

FILE *f, *g;

void readData()
{
	f = fopen("flip.in","r");
    g = fopen("flip.out","w");
    
    fscanf(f,"%d %d\n",&N,&M);
    int i,j;
    for(i = 0; i < N; ++i)
        for(j = 0; j < M; ++j)
            fscanf(f,"%d",&mat[i][j]);
        
    fclose(f);
    return;
}

int main()
{
    readData();
    
    int activeRow = 0;
    int activeCol = 0;
    int i = 0;
    int j = 0;
    
    int sum = 0;
    int max = 0x80000000;
    
    for(activeRow = 0; activeRow < 1 << N; ++activeRow)
        for(activeCol = 0; activeCol < 1 << M; ++activeCol)
        {
            sum = 0;
            for(i = 0; i < N; ++i)
                for(j = 0; j < M; ++j)
                {
                    if(((1<<i)&activeRow) && ((1<<j)&activeCol))
                        sum += mat[i][j];
                    else
                    if(((1<<i)&activeRow) || ((1<<j)&activeCol))
                        sum += -mat[i][j];
                    else
                        sum += mat[i][j];
                    
                }
            if(sum > max)
                max = sum;
        }
    
    fprintf(g,"%d\n",max);
    fclose(g);
    return 0;
}