Cod sursa(job #1904490)

Utilizator paul.satmareanPaul Satmarean paul.satmarean Data 5 martie 2017 16:24:31
Problema Jocul Flip Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <stdio.h>


int mat[16][16];
int rows;
int cols;

int s[16];
int d[2];

int max(int a,int b){
    if(a>b)
        return a;
    else
        return b;
}


int max_sum = 0;
void back(int x){
    if(x==cols+1)
        return;
    else{
        for (int i=0;i<2;i++){
            int total = 0;
            s[x]=d[i];
            if(x==cols){
                for(int y=0;y<rows;y++){
                    int sum=0;
                    for(int x=0;x<cols;x++){
                        sum+=mat[y][x]*s[x];
                    }
                    if(sum<0){
                        total+=-sum;
                    }else{
                        total+=sum;
                    }
                }
                max_sum = max(max_sum,total);
            }
            back(x+1);
        }
    }
}







int main(){
    d[0]=1;
    d[1]=-1;
    
    FILE * fd = fopen("flip.in","r");
    fscanf(fd,"%d %d\n",&rows,&cols);
    for(int i=0;i<rows;i++){
        for(int j=0;j<cols;j++){
            fscanf(fd,"%d ",&mat[i][j]);
        }
    }
    fclose(fd);
    back(0);
    fd = fopen("flip.out","w");
    fprintf(fd,"%d",max_sum);
    fclose(fd);

    return 0;
}