Pagini recente » Cod sursa (job #1316276) | Cod sursa (job #2239798) | Cod sursa (job #455140) | Cod sursa (job #515186) | Cod sursa (job #494642)
Cod sursa(job #494642)
#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;
}