Cod sursa(job #494642)

Utilizator zooppAccount deletion pending zoopp Data 22 octombrie 2010 14:04:37
Problema Jocul Flip Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <cstdio>

void back(int (*matr)[16], int &lines, int &rows, bool *truthTable, int &solution, int index)
{
    if (index == lines + rows) {
        //print(truthTable, lines + rows);
        int sum = 0;
        for (int i = 0; i < lines; i++) {
            for (int j = 0; j < rows; j++) {
                if (truthTable[i]^truthTable[lines + j]) {
                    sum -= matr[i][j];
                } else {
                    sum += matr[i][j];
                }
            }
        }
        if (sum > solution) {
            solution = sum;
        }
        return;
    }
    
    truthTable[index] = false;
    back(matr, lines, rows, truthTable, solution, index + 1);
    truthTable[index] = true;
    back(matr, lines, rows, truthTable, solution, index + 1);
}
//problem: the way the truth table is interpreted
int main()
{
    int lines;
    int rows;
    int solution = 0;
    bool truthTable[32];
    int matr[16][16];
    
    FILE *input = fopen("flip.in", "r");
    FILE *output = fopen("flip.out", "w");
    
    if (input != NULL) {
        fscanf(input, "%d %d", &lines, &rows);
        
        for (int i = 0; i < lines; i++) {
            for (int j = 0; j < rows; j++) {
                fscanf(input, "%d", &matr[i][j]);
            }
        }
        
        back(matr, lines, rows, truthTable, solution, 0);
    }
    
    fprintf(output, "%d", solution);
    return 0;
}