Pagini recente » Cod sursa (job #2683483) | Cod sursa (job #1717839) | Cod sursa (job #2182260) | Cod sursa (job #322143) | Cod sursa (job #3315042)
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstdlib>
#include <algorithm>
int main()
{
std::fstream f("flip.in");
assert(f.is_open());
int rows, cols;
f >> rows >> cols;
assert(rows >= 1);
assert(cols <= 16);
int *arr = (int*)malloc(sizeof(int) * rows * cols);
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < cols; ++j) {
int n;
f >> n;
assert(std::abs(n) < 1000000);
arr[i * rows + j] = n;
}
}
f.close();
auto flip = [&](int *const array, const int row, const int column) {
for(int i = 0; i < rows; ++i) {
arr[i * rows + column] *= -1;
}
for(int i = 0; i < cols; ++i) {
arr[row * rows + i] *= -1;
}
};
auto sum = [&](int *const array) -> int {
int sum{0};
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < cols; ++j) {
sum += arr[i * rows + j];
}
}
return sum;
};
int biggest = 0;
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < cols; ++j) {
flip(arr, i, j);
int total = sum(arr);
if(total > biggest) {
biggest = total;
}
flip(arr, i, j);
}
}
std::ofstream o("flip.out");
o << biggest;
o.close();
return 0;
}