Pagini recente » Cod sursa (job #2676041) | Cod sursa (job #859657) | Cod sursa (job #1708509) | Cod sursa (job #1108855) | Cod sursa (job #147800)
Cod sursa(job #147800)
#include <stdio.h>
FILE *f=fopen("flip.in","r"), *g=fopen("flip.out","w");
int q,w,n,m,lin[20],col[20],x[20][20],y[20][20];
long s,max=0;
void init(int k)
{
lin[k]=-1;
}
void init2(int k2)
{
col[k2]=-1;
}
int succesor(int k)
{
if (lin[k]<1)
{
lin[k]++;
return 1;
}
else return 0;
}
int succesor2(int k2)
{
if (col[k2]<1)
{
col[k2]++;
return 1;
}
else return 0;
}
int solutie(int k)
{
return k==n+1;
}
int solutie2(int k2)
{
return k2==m+1;
}
void use()
{
for (q=1;q<=n;q++)
for (w=1;w<=m;w++)
y[q][w]=x[q][w];
s=0;
for (q=1;q<=n;q++)
for (w=1;w<=m;w++)
{
if (lin[q]) y[q][w]*=-1;
s+=y[q][w];
}
if (s>max) max=s;
s=0;
for (q=1;q<=m;q++)
for (w=1;w<=n;w++)
{
if (col[q]) y[w][q]*=-1;
s+=y[w][q];
}
if (s>max) max=s;
}
void back2(int k2)
{
if (solutie2(k2)) use();
else
{
init2(k2);
while (succesor2(k2))
back2(k2+1);
}
}
void back(int k)
{
if (solutie(k)) {
back2(1);
}
else
{
init(k);
while (succesor(k))
back(k+1);
}
}
int main()
{
int i,j,k,p,s,min;
fscanf(f,"%d %d",&n,&m);
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
fscanf(f,"%d",&x[i][j]);
back(1);
fprintf(g,"%ld\n",max);
fclose(g);
return 0;
}