Pagini recente » Cod sursa (job #2832373) | Cod sursa (job #2533785) | Cod sursa (job #1091822) | Cod sursa (job #619695) | Cod sursa (job #2478202)
/// BRUT
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("flip.in");
ofstream fout("flip.out");
int n, m ,a[20][20], stn[105], stm[105], b[20][20], nr, summax = -1E9;
void Read()
{
fin >> n >> m;
for(int i=1; i <= n; i++)
for(int j = 1; j <= m; j++)
fin >> a[i][j];
}
void sum()
{
int sum = 0;
for(int i=1; i <= n; i++)
for(int j = 1; j <= m; j++)
sum += b[i][j];
summax = max(sum, summax);
}
void Afisare(int top)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
b[i][j] = a[i][j];
for(int i = 1; i <= top; i++)
for(int j = 1; j <= m; j++)
b[stm[i]][j] *= -1;
for(int i = 1; i <= nr; i++)
for(int j = 1; j <= n; j++)
b[j][stn[i]] *= -1;
sum();
}
BackM(int top)
{
if(top > 1)
{
Afisare(top-1);
}
for(int i = stm[top-1]+1; i <= m; i++)
{
stm[top] = i;
BackM(top + 1);
}
}
void BackN(int top)
{
if(top > 1)
{
nr = top-1;;
BackM(1);
}
for(int i = stn[top-1]+1; i <= n; i++)
{
stn[top] = i;
BackN(top + 1);
}
}
int main()
{
Read();
BackN(1);
fout << summax << "\n";
return 0;
}