Cod sursa(job #1422870)

Utilizator oana.fercheoana maria oana.ferche Data 20 aprilie 2015 07:49:24
Problema Jocul Flip Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <iostream>
#include <fstream>

using namespace std;
int maxsum = 0;
int lines, cols;
int **data = new int*[16];

int getSum(int** mat){
    int sum = 0;
    for (int i = 0; i < lines; i ++)
        for (int j = 0; j < cols; j++)
            sum += mat[i][j];
    return sum;
}

int ** flip (int** mat, int no, bool line) {
    if (line)
        for (int i = 0; i < cols; i++)
            mat[no][i] *= -1;
    else
        for (int i = 0; i < lines; i++)
            mat[i][no] *= -1;
    return mat;
}

void readFile()
{
    ifstream in ("flip.in");
    if (in.is_open()){
        in >> lines;
        in >> cols;
        //cout << "lines is: " << lines << " cols is: " << cols;
        for (int i = 0; i < lines; i ++)
        {
            data[i] = new int[16];
            for (int j = 0; j < cols; j ++)
                in >> data[i][j];
        }
        in.close();
    }
}

void writeFile()
{
    ofstream out ("flip.out");
    out << maxsum;
    out.close();
}

void process(int** mat, int no)
{
    if (no > lines + cols - 1) return;
    int csum = getSum(mat);
    if (csum > maxsum)
        maxsum = csum;
    if (no > lines - 1)
    {
        process(mat, no + 1);
        process(flip(mat, no - lines, false), no + 1);
    } else
    {
        process(mat, no + 1);
        process(flip(mat, no, true), no+1);
    }
}

int main()
{
    readFile();
    process(data, 0);
    writeFile();
    return 0;
}