Cod sursa(job #133637)

Utilizator bilygates2005Liviu Cristian Mirea-Ghiban bilygates2005 Data 9 februarie 2008 12:54:12
Problema Jocul Flip Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include<stdio.h>
#include<math.h>
long v[16][16],f[16][16];
int m,n;
int flipx(int x)
{
 int i,c,j;
 c=0;
 for(i=1;i<=x;i*=2)
 {
	if((x & i) == i)
	{ for(j=0;j<m;j++) {f[j][c]*=-1;}	}
	c++;
 }
 return 0;
}
int flipy(int x)
{
 int i,c,j;
 c=0;
 for(i=1;i<=x;i*=2)
 {
	if((x & i) == i)
	{ for(j=0;j<n;j++) {f[c][j]*=-1;}	}
	c++;
 }
 return 0;
}
int main()
{
 long long i,j,k,l,max,maxset=0,maxcur,a,b;
 freopen("flip.in","r",stdin);
 freopen("flip.out","w",stdout);

 scanf("%d%d",&m,&n);
 for(i=0;i<m;i++)
 {
	for(j=0;j<n;j++)
	{
	 scanf("%ld",&v[i][j]);
	}
 }

 a=pow(2,m);
 b=pow(2,n);
 for(i=0;i<a;i++)
 {
	for(j=0;j<b;j++)
	{
	 for(k=0;k<m;k++)
	 {
		for(l=0;l<n;l++)
		{f[k][l]=v[k][l];}
	 }
	 flipx(j);
	 flipy(i);
	 maxcur=0;
	 for(k=0;k<m;k++)
	 {
		for(l=0;l<n;l++)
		{maxcur+=f[k][l];}
	 }
	 if(maxset==0 || maxcur>max)
	 {max=maxcur;maxset=1;}
	}
 }
							/*
 for(i=0;i<m;i++)
 {
	for(j=0;j<n;j++)
	{
	 printf("%d ",f[i][j]);
	}printf("\n");
 }          */
 printf("%lld",max);
 fclose(stdin);
 fclose(stdout);
}