Cod sursa(job #964048)

Utilizator danlexDan Alexandru danlex Data 19 iunie 2013 23:18:56
Problema Jocul Flip Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <iostream>
#include <fstream>
using namespace std;

int n, m, st, x[100], game[16][16], maxGame;
bool DEBUG = false;

void print(){
    if(DEBUG){
        cout << "maxGame: " << maxGame << endl;
        for (int i = 0; i < n; i ++){
            for (int j = 0; j < m; j++){
                cout << game[i][j] << " ";
            }
            cout << endl;
        }
    }
}

void read(){
    ifstream f("flip.in");
    f >> n;
    f >> m;

    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            f >> game[i][j];
        }
    }

    if(DEBUG) cout << "n = " << n << endl;
    if(DEBUG) cout << "m = " << m << endl;

    if(DEBUG) print();

    f.close();
}

void write(){
    ofstream of("flip.out");
    of << maxGame;
    of.close();

}

bool cond(){
    if (st < n){
        for (int j = 0; j < m; j ++){
            game[st][j] *= -1;
        }
    }

    if (st >= n){
        for (int i =0; i < n; i ++){
            game[i][st - n] *= -1;
        }
    }

    return true;
}

int computeGame(){
    int sumGame = 0;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            sumGame += game[i][j];
        }
    }
    return sumGame;
}

void back(){
    st = 0;
    x[st] = -1;
    while (st >= 0) {
          if (x[st] < 1){
               x[st] ++;
               if (cond()){
                  if (st == n + m -1){
                      maxGame = max(maxGame, computeGame());
                      print();
                  } else {
                     st ++;
                     x[st] = -1;
                  }
               }
           } else {
               st --;
           }
    }
}

int main(void)
{
    read();
    back();
    write();
    return 0;
}