Pagini recente » Cod sursa (job #1733927) | Cod sursa (job #865234) | Rating catrinescualex (catrinescualex) | Cod sursa (job #2923306) | Cod sursa (job #1957484)
#include <fstream>
#include <stdio.h>
#define maxim 16
using namespace std;
int v[maxim][maxim];
char lin[maxim+1];
char col[maxim+1];
void inc_lin ( int l ) {
int i = 0;
while ( i < l && lin[i] == 1 ) {
lin[i] = 0;
i++;
}
if ( i < l ) {
lin[i] = 1;
}
}
/*void inc_col ( int c ) {
int j = 0;
while ( j < c && col[j] == 1 ) {
col[j] = 0;
j++;
}
if ( j < c ) {
col[j] = 1;
}
}*/
long long getsum ( int l, int c ) {
long long s = 0;
int i, j;
for ( i = 0; i < l; i++ )
for ( j = 0; j < c; j++ )
if ( lin[i] + col[j] == 1 )
s -= v[i][j];
else
s += v[i][j];
return s;
}
long long _max ( int a, int b ) {
if ( a > b )
return a;
else
return b;
}
int main () {
ifstream fin ( "flip.in" );
ofstream fout ( "flip.out" );
int l, c;
fin>>l>>c;
int i, j;
for ( i = 0; i < l; i++ )
for ( j = 0; j < c; j++ )
fin>>v[i][j];
long long s = 0LL, smax = -1000000LL, cs, k;
for ( i = 0; i < 1<<l; i++, smax = max ( smax, getsum ( l, c ) ), inc_lin ( l ) ) {
//for ( k = 0; k < l; k++ )
//printf ( "%d ", lin[k] );
//printf ( "\n" );
for ( j = 0; j < c; j++ ) {
for ( k = 0, cs = 0; k < l; k++ )
if ( lin[k] == 0 )
cs += v[i][j];
else
cs -= v[i][j];
//printf ( "cs = %d pentru coloana %d\n", cs, j );
if ( cs < 0 )
col[j] = !col[j];
}
}
fout<<smax<<'\n';
fin.close ();
fout.close ();
return 0;
}