Cod sursa(job #1743726)

Utilizator calin1Serban Calin calin1 Data 18 august 2016 16:57:23
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <iostream>
#include <cstdio>
#include <bitset>

using namespace std;

int mat[20][20],sum_l[20];
int n,m,sum_tot,max_mat;

bitset <20> viz;

void coloane(int &sum)
{
    for(int j = 1; j <= m ; ++j)
    {
        int sum_c = 0;
        for(int i = 1 ; i <= n ; ++i)
        {
            if(viz[i])
            {
                sum_c -= mat[i][j];
            }
            else
            {
                sum_c += mat[i][j];
            }
        }
        if(sum_c < 0)
        {
            sum += (sum_c*(-2));
        }
    }
}

void umplere()
{
    for(int i = 1 ; i < 20 ; ++i)
    {
        viz[i] = false;
    }
}

void prelucrare()
{
    for(int x = 0 ; x < (1<<n) ; ++x)
    {
        umplere();
        int tmp = sum_tot;

        for(int j = 0 ; j < n ; ++j)
        {
            if(x & (1<<j))
            {
                tmp -= (sum_l[j+1]*2);
                viz[j+1] = true;
            }
        }
        coloane(tmp);
        if(tmp > max_mat)
        {
            max_mat = tmp;
        }
    }
    printf("%d",max_mat);
}

void citire()
{
    scanf("%d %d\n",&n,&m);

    for(int i = 1; i <=n ; ++i)
    {
        int x = 0;
        for(int j = 1; j <= m ; ++j)
        {
            scanf("%d ",&mat[i][j]);
            sum_tot += mat[i][j];
            x += mat[i][j];
        }
        scanf("/n");
        sum_l[i] = x;
    }
    max_mat = sum_tot;

    prelucrare();
}

int main()
{
    freopen("flip.in","r",stdin);
    freopen("flip.out","w",stdout);

    citire();

    return 0;
}