Pagini recente » Cod sursa (job #2415093) | Cod sursa (job #2359757) | Cod sursa (job #268072) | Cod sursa (job #2230449) | Cod sursa (job #108735)
Cod sursa(job #108735)
#include <fstream.h>
#include <math.h>
ofstream g("flip.out");
ifstream f("flip.in");
int st[20],n,m;
long mat[17][17];
long long int Smax=0LL, S=0LL;
void binar(int a)
{
int i=0;
while (a!=0)
{
i++;
if (a>1)
st[i]=a%2;
a=a/2;
}
}
long mat1[17][17];
void matcpy()
{
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
mat1[i][j]=mat[i][j];
}
void flip()
{
int i,j;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
if (st[j]==1)
mat1[i][j]*=(-1);
}
void fliplinie()
{
int i,j;
for (i=1;i<=n;i++)
{
S=0;
for (j=1;j<=m;j++)
S=S+mat1[i][j];
if (S<0)
for (j=1;j<=m;j++)
mat1[i][j]*=(-1);
}
}
int main()
{
Smax=0;
int i,j,a,l;
f>>n>>m;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
f>>mat[i][j];
for (i=1;i<=pow(2,m+1)-1;i++)
{
a=i;
binar(a);
matcpy();
flip();
fliplinie();
S=0;
for (l=1;l<=n;l++)
for (j=1;j<=m;j++)
S=S+mat1[l][j];
if (S>Smax)
Smax=S;
}
g<<Smax;
}