Pagini recente » Cod sursa (job #3137653) | Cod sursa (job #2088412) | Cod sursa (job #1794120) | Cod sursa (job #241927) | Cod sursa (job #883790)
Cod sursa(job #883790)
#include <iostream>
#include <fstream>
#include <limits>
using namespace std;
ifstream f("flip.in");
ofstream g("flip.out");
int n,m,mato[18][18],st[18];
int smax=numeric_limits<int>::min();
void fsum() {
int i,j,stemp,s=0,matf[18][18];
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
matf[i][j]=mato[i][j];
for (i=1;i<=n;i++)
if (st[i]==2)
for (j=1;j<=m;j++)
matf[i][j]*=-1;
for (j=1;j<=m;j++) {
stemp=0;
for (i=1;i<=n;i++)
stemp+=matf[i][j];
stemp*=(stemp<0?-1:1);
s+=stemp;
}
if (s>smax)
smax=s;
for (i=1;i<=n;i++)
g<<st[i]<<" ";
g<<endl;
}
void back(int k) {
int t;
if (k==n+1)
fsum();
else if (k<=n) {
for (t=1;t<=2;t++) {
st[k]=t;
back(k+1);
}
}
}
int main ()
{
int i,j;
f>>n>>m;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
f>>mato[i][j];
back(1);
g<<smax;
}