Cod sursa(job #2654770)

Utilizator StefanSanStanescu Stefan StefanSan Data 2 octombrie 2020 11:29:59
Problema Jocul Flip Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include      <iostream>
#include      <fstream>
#include      <algorithm>
#include      <queue>
#include      <vector>
#include      <stack>

using namespace std;

ifstream in("flip.in");
ofstream out("flip.out");

int n, m, mat[20][20], maxi;

void flipLine(int l) {
	for (int i = 0; i < m; i++)mat[l][i] *= -1;
}

void flipColumn(int c) {
	for (int i = 0; i < n; i++)mat[i][c] *= -1;
}


void greedy() {

	int sum = 0, ps = 0;

	for (int i = 0; i < m; ++i) {

		ps = 0;

		for (int j = 0; j < n; ++j) {

			ps += mat[j][i];

		}

		sum += (ps > 0) ? ps : -ps;

	}

	if (sum > maxi) {

		maxi = sum;

	}

}



void back(const int l) {

	if (l == n) {

		greedy();

	}
	else {

		back(l + 1);

		flipLine(l);

		back(l + 1);

		flipLine(l);

	}

}

int main() {
	ios::sync_with_stdio(false);
	in.tie(NULL), out.tie(NULL);

	in >> n >> m;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			in >> mat[i][j];
		}
	}
	back(0);
	out << maxi;
	
	return 0;
	 
}