Cod sursa(job #2642631)

Utilizator AnnieMyaAnamaria Ordace AnnieMya Data 16 august 2020 13:19:08
Problema Jocul Flip Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <fstream>
using namespace std;

short n,m,sol[16],i,j;
long a[16][16],maxim=-2147483648,s, col;

void sum();

void back(int k)
{
    if(k>=n)
        sum();
    else
    {
        back(k+1);
        sol[k]=1;
        back(k+1);
        sol[k]=0;
    }
}

void sum ()
{
    s=0;
    for(j=0;j<m;j++)
    {
        col=0;
        for(i=0;i<n;i++)
            if(sol[i])
                col-=a[i][j];
            else
                col+=a[i][j];
        if(col>0)
            s+=col;
        else
            s-=col;
    }
    if(s>maxim)
        maxim=s;
}


int main()
{
    ifstream in("flip.in");
    in >>n >>m;
    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            in >>a[i][j];
    in.close();
    back(0);
    ofstream out("flip.out");
    out <<maxim;
    out.close();
    return 0;
}