Cod sursa(job #959805)

Utilizator VincentVegaVincent Vega VincentVega Data 8 iunie 2013 20:58:08
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>

using namespace std;

const int dMax = 17;
int N, M, sol;
int A[dMax][dMax], bin[dMax + 1];

void nextBinary(int V[])
{
	int position = 1;
	V[position]++;
	while (V[position] == 2) {
		V[position + 1]++;
		V[position++] = 0;
	}
}

int getMax(int A[dMax][dMax])
{
	int res = 0;
	for (int j = 1; j <= M; j++) {
		int sum = 0;
		for (int i = 1; i <= N; i++)
			sum = sum + A[i][j];
		if (sum < 0) res = res - sum;
		else res = res + sum;
	}
	return res;
}

int main()
{
	ifstream fin("flip.in");
	ofstream fout("flip.out");
	
	fin >> N >> M;
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= M; j++)
			fin >> A[i][j];
	
	while (bin[N + 1] == 0) {
		int Aux[dMax][dMax];
		
		for (int i = 1; i <= N; i++)
			for (int j = 1; j <= M; j++)
				Aux[i][j] = A[i][j] * (bin[i] == 0 ? 1 : -1);
		sol = max(sol, getMax(Aux));
		nextBinary(bin);
	}

	fout << sol;
}