Cod sursa(job #694064)

Utilizator beldeabogdanBogdan Beldea beldeabogdan Data 27 februarie 2012 18:27:00
Problema Jocul Flip Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <cstdio>
using namespace std;
int v[20][20];
int n,i,m,j;
long long crt,mx;
int x[20],y[20];
void check() {
	int i,j;
	crt=0;
	for (i=1;i<=m;i++) for (j=1;j<=n;j++) {
		if (x[i]&&y[j]) crt+=v[i][j];
		if (x[i]&&!y[j]) crt-=v[i][j];
		if (!x[i]&&y[j]) crt-=v[i][j];
		if (!x[i]&&!y[j]) crt+=v[i][j];
	}
	if (crt>mx) mx=crt;
}
void permut2(int k) {
	if (k<=n) {
		y[k]=true;
		permut2(k+1);
		y[k]=false;
		permut2(k+1);
	}
	else check();
}
void permut(int k) {
	if (k<=n) {
		x[k]=true;
		permut(k+1);
		x[k]=false;
		permut(k+1);
	}
	else permut2(1);
}
int main() {
	freopen("flip.in","r",stdin);
	freopen("flip.out","w",stdout);
	scanf("%d %d",&m,&n);
	for (i=1;i<=m;i++) for (j=1;j<=n;j++) scanf("%d",&v[i][j]);
	permut(1);
	printf("%d",mx);
	return 0;
}