Pagini recente » Cod sursa (job #1943578) | Cod sursa (job #2907208) | Cod sursa (job #1828276) | Cod sursa (job #1179000) | Cod sursa (job #1743726)
#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;
}