Cod sursa(job #798698)

Utilizator marcu.iulian13Iulian Marcu marcu.iulian13 Data 16 octombrie 2012 23:12:56
Problema Jocul Flip Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <iostream>
#include <fstream>
using namespace std;

int a[16][16], n, m;
long int max_a = 0;

ifstream in("flip.in");
ofstream out("flip.out");

void citire();
void scriere();

void calculate_max(int k, int semn)
{
    long int max_t = 0;
    if(k < m) {
        for(int y = 0; y < n; y++)
        {
            long int max_r = 0;
            for(int r = 0; r < 2; r++) {
                long int max_c = 0;
                for(int x = 0; x < m; x++)
                {
                    if(!r) a[y][x] *= 1;
                    else a[y][x] *= -1;

                    if(x == k) a[y][x] *= semn;

                    max_c += a[y][x];
                }
                if(max_c > max_r)
                    max_r = max_c;
            }
            max_t += max_r;
        }
        if(max_t > max_a)
            max_a = max_t;
        if(semn != -1)
        {
            calculate_max(k, -1);
            if(k + 1 < m)
                calculate_max(k + 1, 1);
        }
    }

}

int main()
{
    citire();
    calculate_max(0, 1);
    scriere();

    return 0;
}

void citire()
{
    in >> n >> m;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
        in >> a[i][j];
    in.close();
}

void scriere()
{
    out << max_a;
    out.close();
}