Cod sursa(job #2325880)

Utilizator rares.amarandeiRares Amarandei rares.amarandei Data 23 ianuarie 2019 00:48:21
Problema Jocul Flip Scor 100
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <fstream>

#define INPUT_PATH ""
#define OUTPUT_PATH ""

void algorithm();

int main()
{
  algorithm();
  return 0;
}
using namespace std;

namespace
{
  const int maxRow = 16;
  const int maxCol = 16;
  int rows = 0;
  int cols = 0;
  int matrix[maxRow][maxCol];
  int colSign[maxCol] = { 1 };
  int solution = 0;
  void solve(int colNr)
  {
    if (colNr == cols)
    {
      int sum = 0;
      for (int row = 0; row < rows; ++row)
      {
        int sumRow = 0;
        for (int col = 0; col < cols; ++col)
          sumRow += colSign[col]*matrix[row][col];
        sum += sumRow > 0 ? sumRow : -sumRow;
      }
      solution = sum > solution ? sum : solution;
      return;
    } 
    colSign[colNr] = 1; solve(colNr + 1);
    colSign[colNr] = -1; solve(colNr + 1);
  }
}

////////////////////////////////////////////////////////////////////////////////
void algorithm()
{
  ifstream inputFile(INPUT_PATH "flip.in");
  ofstream outputFile(OUTPUT_PATH "flip.out");

  inputFile >> rows >> cols;
  for (int row = 0; row < rows; ++row)
    for (int col = 0; col < cols; ++col)
      inputFile >> matrix[row][col];

  solve(0);

  outputFile << solution;
  inputFile.close();
  outputFile.close();
}