Cod sursa(job #319246)

Utilizator mentallysafenotPerian Bogdan mentallysafenot Data 30 mai 2009 21:53:02
Problema Jocul Flip Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <fstream>
#include <cmath>

using namespace std;
fstream f,g;

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

long summer(int i,int j)
{
   long sum=0;
  	int a,b;
	for(a=0;a<m;a++)
	  for(b=0;b<n;b++)
	     sum+= (i&(1<<a)?-1:1)*(j&(1<<b)?-1:1)*flipper[a][b];
    
   return sum;
}

int main()
{
  f.open("flip.in",fstream::in);
  f >> m >> n;
  int i,j;
  for(i=0;i<m;i++)
     for(j=0;j<n;j++)
	   {
	    f >> flipper[i][j];
         }		
  
  long aux;
  
  int q,w;
  q = pow(2,(float)m)+1;
  w = pow(2,(float)n)+1;
  
  for(i=0;i<q;i++)
    for(j=0;j<w;j++)
	  {
	     aux = summer(i,j);
		 if(aux>maxi)
		    maxi = aux;
	  }
  
  g.open("flip.out",fstream::out);
  g << maxi << "\n";
  g.close();
  return 0;
}