Pagini recente » Borderou de evaluare (job #2912063) | Cod sursa (job #2524739) | Cod sursa (job #2757857) | Cod sursa (job #3254801) | Cod sursa (job #2325506)
#include <stdlib.h>
#include <stdint.h>
#include <string>
#include <vector>
#include <iostream>
#include <unordered_map>
#include <memory>
#include <algorithm>
#include <deque>
#include <iterator>
#include <numeric>
#include <assert.h>
#include <cstdlib>
#include <sstream>
#include <fstream>
#define INPUT_PATH ""
#define OUTPUT_PATH ""
void algorithm();
int main()
{
algorithm();
return 0;
}
using namespace std;
namespace
{
static int nrRows = 0;
static int nrCols = 0;
vector<int> matrix;
static int maxSum = 0;
void flipCol(int col)
{
for (int row = 0; row < nrRows; ++row)
{
matrix[row*nrCols + col] = -matrix[row*nrCols + col];
}
}
void tryFlippingRows(int sum, int row, bool sign)
{
for (int col = 0; col < nrCols; ++col)
{
sum += sign ? matrix[row*nrCols + col] : -matrix[row*nrCols + col];
}
maxSum = max(maxSum, sum);
if (row + 1 < nrRows)
{
tryFlippingRows(sum, row + 1, true);
tryFlippingRows(sum, row + 1, false);
}
}
int tryFlippingRows()
{
int sum = 0;
tryFlippingRows(sum, 0, true);
tryFlippingRows(sum, 0, false);
return sum;
}
void tryFlippingCols(int col, bool sign)
{
int sum = tryFlippingRows();
maxSum = max(sum, maxSum);
if (col + 1 < nrCols)
{
tryFlippingCols(col + 1, true);
flipCol(col + 1);
tryFlippingCols(col + 1, false);
}
}
void startFlipping()
{
tryFlippingCols(0, true);
tryFlippingCols(0, false);
}
}
////////////////////////////////////////////////////////////////////////////////
void algorithm()
{
ifstream inputFile(INPUT_PATH "flip.in");
ofstream outputFile(OUTPUT_PATH "flip.out");
//read data from file
inputFile >> nrRows;
inputFile >> nrCols;
matrix.reserve(nrRows*nrCols);
int readVal = 0;
while (inputFile >> readVal)
{
matrix.push_back(readVal);
}
//end read data from file
startFlipping();
outputFile << maxSum;
inputFile.close();
outputFile.close();
}