Cod sursa(job #65446)

Utilizator nobodybanAna Ban nobodyban Data 9 iunie 2007 23:30:08
Problema Jocul Flip Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <fstream>

using namespace std;

int n;
int m;
long a[16][16],b[16][16] ;

void citire() {
	ifstream in("flip.in");
	in >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			in >> a[i][j];	
}

long suma(long a[16][16]) {
	long s = 0;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) 
			s += a[i][j];
	return s;			
}

void refac(long b[16][16], long a[16][16]) {
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) 
			a[i][j] = b[i][j];
}

void schimba_linie (int l) {
	for (int i = 0; i < m; i++)
		a[l][i] = -a[l][i];
}

void schimba_coloana (int c) {
	for (int i = 0; i < n; i++)
		a[i][c] = -a[i][c];
}


long actiune() {
	long sm =  suma(a);
	refac(a, b);
	for (long x = 0; x < (1 << (n + m)); x++) {
		long s = 0;
		for (int i = 0; i < n; i++)
			if  (x & (1 << i))
				schimba_linie(i);
		for (int i = n; i < n + m; i++)
			if  (x & (1 << i))
				schimba_coloana(i - n);
		s = suma(a);
		if (s > sm)
			sm = s;
		refac(b, a);
	}
	return sm;
}

int main() {
	citire();
	ofstream out("flip.out");
	out << actiune();
	out.close();
	return 0;
}