Cod sursa(job #2841963)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 30 ianuarie 2022 19:47:15
Problema Jocul Flip Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <bits/stdc++.h>
#define NMAX 18

using namespace std;

/*******************************/
// INPUT / OUTPUT
ifstream f("flip.in");
ofstream g("flip.out");
/*******************************/
/// GLOBAL DECLARATIONS

int N, M;
long long ans;

int grid[NMAX][NMAX];
bool flipCol[NMAX], flipRow[NMAX];
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    f >> N >> M;

    for (int i = 1 ; i <= N ; ++ i)
    {
        for (int j = 1 ; j <= M ; ++ j)
        {
            f >> grid[i][j];
        }
    }
}
///-------------------------------------
void CheckGrid()
{
    long long sum = 0;
    for (int i = 1 ; i <= N ; ++ i)
    {
        for (int j = 1 ; j <= M ; ++ j)
        {
            int mult = 1;
            if (flipCol[j]) mult = -mult;
            if (flipRow[i]) mult = -mult;

            sum += 1LL * grid[i][j] * mult;
        }
    }
    ans = max(ans, sum);
}
///-------------------------------------
void Back(int k)
{
    if (k > N && k > M)
    {
        CheckGrid();
        return;
    }

    // Flip col and flip row
    if (k <= N && k <= M)
    {
        flipRow[k] = true;
        flipCol[k] = true;
        Back(k + 1);
        flipRow[k] = false;
        flipCol[k] = false;
    }

    // Flip only col
    if (k <= M)
    {
        flipCol[k] = true;
        Back(k + 1);
        flipCol[k] = false;
    }

    // Flip only row
    if (k <= N)
    {
        flipRow[k] = true;
        Back(k + 1);
        flipRow[k] = true;
    }

    // Don't flip
    Back(k + 1);
}
///-------------------------------------
inline void Solution()
{
    Back(1);
}
///-------------------------------------
inline void Output()
{
    g << ans;
}
///-------------------------------------
int main()
{
    ReadInput();
    Solution();
    Output();
    return 0;
}