Pagini recente » Cod sursa (job #1560767) | Cod sursa (job #1998115) | Cod sursa (job #1961958) | Cod sursa (job #2184785) | Cod sursa (job #2616943)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("flip.in");
ofstream fout ("flip.out");
int mat[16][16], m, n, smax=-100000000;
bool operatie[16];
int calc_sum_tot()
{
int i, j, s_col, s_totala=0;
for(j=0;j<m;j++)
{
s_col=0;
for(i=0;i<n;i++)
{
if(operatie[i]==1)
s_col-=mat[i][j];
else
s_col+=mat[i][j];
}
if(s_col<0)
s_col=-s_col;
s_totala+=s_col;
}
return s_totala;
}
void bkt_flip(int pos)
{
if(pos==n)
{
int s=calc_sum_tot();
if(s>smax)
smax=s;
}
else
{
operatie[pos]=1;
bkt_flip(pos+1);
operatie[pos]=0;
bkt_flip(pos+1);
}
}
int main()
{
int l, c;
fin >> n >> m;
for(l=0;l<n;l++)
for(c=0;c<m;c++)
fin >> mat[l][c];
bkt_flip(0);
fout << smax;
return 0;
}