Cod sursa(job #314435)

Utilizator andreioneaAndrei Onea andreionea Data 11 mai 2009 20:47:18
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include<cstdio>
long sumcol[17],max;
int n,m,gasit=0;
int s[17];
int a[17][17];
int schimb(int p)
{if(s[p]==0) {s[p]=1;return 1;}
 else if(s[p]==1) {s[p]=-1;return 1;}
 else return 0;
}
void calc_sol()
{int i,j;
 long sumtot=0;
 for(j=1;j<=m;++j) sumtot+=sumcol[j]*s[j];
 for(i=1;i<=n;++i)
  {long k=0;
    for(j=1;j<=m;++j) 
     k+=a[i][j]*s[j];
   if(k<0) sumtot-=2*k;
  } 
  
  if(!gasit) {max=sumtot;gasit=1;}
  else if(max<sumtot) max=sumtot;
 }
void back()
{int p=1;
 s[p]=0;
 while(p)
         {if(!schimb(p)) p--;
          else if(p<=m) if(p==m) calc_sol();
           else
               s[++p]=0;
          
          }
}
int main()
{FILE *f=fopen("flip.in","r");
 fscanf(f,"%d%d",&n,&m);
 int i,j;
 for(i=1;i<=n;++i)
 for(j=1;j<=m;++j)
 fscanf(f,"%d",&a[i][j]);
 for(j=1;j<=m;++j)
 {sumcol[j]=0;
  for(i=1;i<=n;++i)
   sumcol[j]+=a[i][j];
  }
 fclose(f);
 back();
 FILE *g=fopen("flip.out","w");
 fprintf(g,"%ld",max);
  fclose(g);
 return 0;
}