Cod sursa(job #363231)

Utilizator Bit_MasterAlexandru-Iancu Caragicu Bit_Master Data 12 noiembrie 2009 13:15:48
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>

using namespace std;

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

const int N = 17;
const int M = 17;

int v[N][M],n,m;
int s_max = 0;

void citire()
{
	in>>n>>m;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			in>>v[i][j];
}

void reglare_coloane()
{
	int s, st = 0;
	for (int j = 1; j <= m; ++j)
	{
		s = 0;
		for (int i = 1; i <= n; ++i)
			s += v[i][j];
		if (s > 0)
			st += s;
		else
			st -= s;
	}
	if (st > s_max)
		s_max = st;
}

void backtracking(int i)
{
	if (i > n)
		return;
	reglare_coloane();
	backtracking(i+1);
	//Comutare.
		for (int j = 1; j <= m; ++j)
			v[i][j] = -v[i][j];
	reglare_coloane();
	backtracking(i+1);
}

int main()
{
	citire();
	backtracking(1);
	out<<s_max;
	return 0;
}