Cod sursa(job #1651328)

Utilizator Andreiii500Andrei Puiu Andreiii500 Data 13 martie 2016 00:19:02
Problema Jocul Flip Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include<iostream>
#include<fstream>
using namespace std;

ifstream in("flip.in");
ofstream out("flip.out");

const int INF = 256000000;
const int N = 16;
const int M = 16;

int mat[N][M];
int n,m,s_max;

int perm_lin, perm_col;

int suma()
{
    int i,j,s,aux;

    s = 0;
    for(i=0; i<n; ++i)
        for(j=0; j<m; ++j)
        {
            aux = mat[i][j];
            if(((1<<i) & perm_lin) > 0) aux *= -1;
            if(((1<<j) & perm_col) > 0) aux *= -1;

            s += aux;
        }
    return s;
}

void afis_binar(int nr, int lung)
{
    if(nr == 0)
    {
        int i;
        for(i=0; i<lung; ++i) out<<"0";
    }
    else
    {
        int p;

        p = 1<<(lung-1);
        while(p)
        {
            if( (p&nr) > 0) out<<"1";
            else out<<"0";
            p >>= 1;
        }
    }
}

int main()
{
    int i,j,perm_lin_max,perm_col_max,s;

    in>>n>>m;
    for(i=0; i<n; ++i)
        for(j=0; j<m; ++j)
            in>>mat[i][j];

    perm_lin_max = 1<<n;
    perm_col_max = 1<<m;

    s_max = -INF;
    for(perm_lin = 0; perm_lin < perm_lin_max; ++perm_lin)
        for(perm_col = 0; perm_col < perm_col_max; ++perm_col)
        {
            s_max = max(s_max, suma());
            ///out<<perm_lin<<" "<<perm_col<<"\n";afis_binar(perm_lin, n);out<<" ";afis_binar(perm_col, m);out<<"\n";
        }
    out<<s_max;

    return 0;
}