Cod sursa(job #36788)

Utilizator RobytzzaIonescu Robert Marius Robytzza Data 24 martie 2007 01:06:22
Problema Jocul Flip Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include<fstream.h>
long a[10000][100];
int main(){
ifstream fin("flip.in");
ofstream fout("flip.out");
short m;
int n;
long S,S1,max=0,P=0;
fin>>n>>m;
for (int i=0;i<n;i++)
   for (short j=0;j<m;j++)
       fin>>a[i][j];
short ok=0;
   while (ok==0){
     ok=1;
for (int h=0;h<n;h++)      {
     S=0;
     S1=0;
	 for (short u=0;u<m;u++){
	    S1+=a[h][u];
	    S+=(-1)*a[h][u];}
	 if (S>S1) {
	    for (int g=0;g<m;g++)
	       a[h][g]=a[h][g]*(-1);
	    ok=0;}
	 else
	   if (S==(-1)*S1){
     for (int r=0;r<m;r++){
	  S=0;
	  S1=0;
	  for (int z=0;z<n;z++){
	     S+=(-1)*a[z][r];
	     S1+=a[z][r];}
       if (S>S1)  {
	   for (int c=0;c<n;c++)
	       a[c][r]=a[c][r]*(-1);
	       ok=0;}
       else
       if (S==(-1)*S1){
	     S=0;S1=0;
	  while (S==S1){
	  for (int pp=0;pp<m;pp++){
	       S=0;
		S1=0;
	  for (int zz=0;zz<n;zz++){
	      S+=(-1)*a[zz][pp];
	      S1+=a[zz][pp];}
	 if (S>S1)  {
	    for (int c=0;c<n;c++)
		a[c][pp]=a[c][pp]*(-1);
		ok=0;}
	    }       }  }
	 else
	  for (int l=0;l<n;l++){
	       a[h][l]=a[h][l]*(-1);
		 ok=1;}}  }
	   P=0;}
     for (int w=0;w<n;w++)
	for (short j=0;j<m;j++)
	    P+=a[w][j];
     if (P>max)
	max=P;     }
	    fout<<max;
fin.close();
fout.close();
return 0;
}