Pagini recente » Cod sursa (job #521189) | Cod sursa (job #2332767) | Cod sursa (job #1393845) | Cod sursa (job #25271) | Cod sursa (job #496686)
Cod sursa(job #496686)
#include <cstdio>
void back(int (*matr)[16], int &lines, int &colums, bool *truthTable, int &solution, int index)
{
if (index == colums) {
int sum = 0;
for (int i = 0; i < lines; i++) {
int lineSum = 0;
for (int j = 0; j < colums; j++) {
if (truthTable[j]) {
lineSum -= matr[i][j];
} else {
lineSum += matr[i][j];
}
}
sum += lineSum<0?-lineSum:lineSum;
}
if (sum > solution) {
solution = sum;
}
return;
}
truthTable[index] = false;
back(matr, lines, colums, truthTable, solution, index + 1);
truthTable[index] = true;
back(matr, lines, colums, truthTable, solution, index + 1);
}
//problem: the way the truth table is interpreted
int main()
{
int lines;
int colums;
int solution = 0;
bool truthTable[16];
int matr[16][16];
FILE *input = fopen("flip.in", "r");
FILE *output = fopen("flip.out", "w");
if (input != NULL) {
fscanf(input, "%d %d", &lines, &colums);
for (int i = 0; i < lines; i++) {
for (int j = 0; j < colums; j++) {
fscanf(input, "%d", &matr[i][j]);
}
}
back(matr, lines, colums, truthTable, solution, 0);
}
fprintf(output, "%d", solution);
return 0;
}