Pagini recente » Cod sursa (job #2233581) | Cod sursa (job #343530) | Cod sursa (job #2290119) | Cod sursa (job #2172709) | Cod sursa (job #2591253)
//Iterative solution
#include <fstream>
int n, m, t[16][16], colSum, tempSum, maxSum;
int main() {
std::ifstream fin("flip.in");
std::ofstream fout("flip.out");
fin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; ) {
fin >> t[i][j++];
}
}
for (int flippedLinesBitMask = 0; flippedLinesBitMask < (1 << n); flippedLinesBitMask++) {
tempSum = 0;
for (int col = 0; col < m; col++) {
colSum = 0;
for (int line = 0; line < n; line++) {
colSum += (flippedLinesBitMask&(1 << line)) ? -t[line][col] : t[line][col];
}
tempSum += abs(colSum);
}
maxSum = (tempSum > maxSum) ? tempSum : maxSum;
}
fout << maxSum;
return 0;
}