Pagini recente » Cod sursa (job #28651) | Cod sursa (job #3254220) | Cod sursa (job #1995251) | Cod sursa (job #412798) | Cod sursa (job #1469885)
#include <fstream>
#include <algorithm>
using namespace std;
int lin[20], col[20], m[20][20], suma_max = 0, N, M;
void suma () {
int max1 = 0;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
if (lin[i] == col[j])
max1 += m[i][j];
else
max1 -= m[i][j];
if (max1 > suma_max)
suma_max = max1;
}
int main(void) {
ifstream input ("flip.in");
ofstream output ("flip.out");
input >> N >> M;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
input >> m[i][j];
for (int i = 0; i <= N; i++) {
lin[i] = 1;
do {
for (int j = 0; j <= M; j++) {
col[j] = 1;
do {
suma ();
}while (next_permutation(col+1,col+M+1));
}
for(int j = 0; j <= M; j++)
col[j] = 0;
}while (next_permutation(lin+1,lin+N+1));
}
output << suma_max;
}