Pagini recente » Cod sursa (job #1681120) | Cod sursa (job #1854024) | Cod sursa (job #352572) | Cod sursa (job #1632166) | Cod sursa (job #356146)
Cod sursa(job #356146)
#include <stdio.h>
int n,m,max=0;
int mat[17][17];
bool col[17],lin[17];
void back(int);
void back2(int);
int suma();
bool abs(int);
int main ()
{
freopen ("flip.in","r",stdin);
freopen ("flip.out","w",stdout);
scanf ("%d%d",&n,&m);
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j)
scanf ("%d",&mat[i][j]);
back(1);
printf ("%d",max);
return 0;
}
void back(int p)
{
if (p>m)
{
back2(1);
return;
}
col[p]=0;
back(p+1);
col[p]=1;
back(p+1);
}
void back2(int p)
{
if (p>n)
{
int x=suma();
if (x>max)
max=x;
return;
}
if (abs(p))
{
lin[p]=1;
back2(p+1);
}
else
{
lin[p]=0;
back2(p+1);
}
}
int suma()
{
int s=0;
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j)
s+=mat[i][j]*(col[j]*2-1)*(lin[i]*2-1);
return s;
}
bool abs(int i)
{
int s=0;
for (int j=1;j<=m;++j)
s+=mat[i][j]*(2*col[j]-1);
if (s>0)
return true;
return false;
}