Cod sursa(job #3241129)

Utilizator doodadooda dooda dooda Data 26 august 2024 19:18:16
Problema Jocul Flip Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.94 kb
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> IntVector;
typedef vector<vector<int>> IntMatrix;
ifstream fin("flip.in");
ofstream fout("flip.out");

IntMatrix genMatrix(int n, int m) {
  IntMatrix a;
  for (int i = 0; i < n; i++) {
    IntVector v;
    for (int j = 0; j < m; j++) {
      int x;
      fin >> x;
      v.push_back(x);
    }
    a.push_back(v);
  }
  return a;
}

void showMatrix(IntMatrix a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j] << " ";
    }
    cout << "\n";
  }
}

bool nextBin(string &aux) {
  int i = aux.size() - 1;
  while (i >= 0 && aux[i] == '1') {
    aux[i] = '0';
    i--;
  }
  if (i >= 0) {
    aux[i] = '1';
    return true;
  } else {
    return false;
  }
}

int sumCalc(IntMatrix a) {
  int sum = 0;
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      sum += a[i][j];
    }
  }
  return sum;
}

void changeRow(IntMatrix &a, int temp) {
  for (int i = 0; i < a[temp].size(); i++) {
    a[temp][i] *= -1;
  }
}

void changeCol(IntMatrix &a, int temp) {
  for (int i = 0; i < a.size(); i++) {
    a[i][temp] *= -1;
  }
}

void finalSumVec(IntMatrix &a, IntMatrix b, string aux, int N, int M, int &max) {
  IntVector sums;
  bool temp = true;
  while (temp) {
    temp = nextBin(aux);
    a = b;
    for (int i = 0; i < N; i++) {
      if (aux[i] == '1') {
        changeRow(a, i);
      }
    }
    for (int i = 0; i < M; i++) {
      if (aux[N + i] == '1') {
        changeCol(a, i);
      }
    }
    int c = sumCalc(a);
    if (c >= max) {
        max = c;
    }
  }
}

int main() {
  int N, M, max = 0;
  fin >> N >> M;
  IntMatrix a = genMatrix(N, M);
  IntMatrix b = a;
  showMatrix(a);
  string aux = "";
  for (int i = 0; i < (M + N); i++) {
    aux += '0';
  }
  finalSumVec(a, b, aux, N, M, max);
  fout << max;
}