Cod sursa(job #2639385)

Utilizator AnnieMyaAnamaria Ordace AnnieMya Data 1 august 2020 20:15:18
Problema Jocul Flip Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <fstream>
using namespace std;

long n,m, a[16][16], maximum=-2147483647;

void back (int i,int j);

long sum()
{
    int i,j;
    long s=0;
    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            s+=a[i][j];
    return s;
}

void next(int i,int j)
{
    if(i<n)
            back(i+1,j);
    if(i<j+1 and j<m)
            back(i,j+1);
}

void back (int i,int j)
{
    long s;
    int k;
    if(i>=n and j>=m)
    {
        s=sum();
        if (s>maximum)
            maximum=s;
    }
    else
    {
        next(i,j);
        for(k=0;k<m;k++)
            a[i][k]*=-1;
        next(i,j);
        for(k=0;k<n;k++)
            a[k][j]*=-1;
        next(i,j);
        for(k=0;k<m;k++)
            a[i][k]*=-1;
        next(i,j);
        for(k=0;k<n;k++)
            a[k][j]*=-1;
        
    }
        
}

int main()
{
    ifstream in("flip.in");
    int i,j;
    in >>n >>m;
    for (i=0;i<n;i++)
        for(j=0;j<m;j++)
            in >>a[i][j];
    in.close();
    back(0,0);
    ofstream out("flip.out");
    out <<maximum;
    out.close();
}