Cod sursa(job #3315042)

Utilizator tudorbconstantinBordei Tudor-Constantin tudorbconstantin Data 11 octombrie 2025 22:54:16
Problema Jocul Flip Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#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;
}