Cod sursa(job #1113886)

Utilizator danny794Dan Danaila danny794 Data 20 februarie 2014 23:48:04
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <cstdio>

using namespace std;

const int NMAX = 20;

int N, M, S = - (1 << 30);
int matrix[NMAX][NMAX];

void solve() {
  int x = 0, y, maxN = 1 << N, maxM = 1 << M, s = 0;
  while( x < maxN) {
    y = 0;
    while( y < maxM) {
      for(int i = 1; i <= N; i++)
        for(int j = 1; j <= M; j++)
          s+= matrix[i][j] * ( (x & (1 << i)) ? -1 : 1) * ( (y & (1 << j)) ? -1 : 1);
      if ( s > S )
        S = s;
      s = 0;
      y++;
    }
    x++;
  }
}

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", &matrix[i][j]);

  solve();
  printf("%d\n", S);
  return 0;
}