Cod sursa(job #130008)

Utilizator Snavenportnespecificat Snavenport Data 30 ianuarie 2008 20:43:36
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <fstream.h>
#include <math.h>

ofstream g("flip.out");
ifstream f("flip.in");

int st[100],n,m;

long mat[17][17],mat1[17][17];
long long int Smax=0LL, S=0LL;

void flip()
{
    int l,j;
    for (l=1;l<=n;l++)
       for (j=1;j<=m;j++)
            if (st[j]==1)
               mat1[l][j]*=(-1);                   
    
}   

void fliplinie()
{
     int l,j;
     for (l=1;l<=n;l++)
       {
       S=0;                
       for (j=1;j<=m;j++)
          S=S+mat1[l][j];
       if (S<0)
          for (j=1;j<=m;j++)
            mat1[l][j]*=(-1);
       }  
}       

void matcpy()
{
     for (int l=1;l<=n;l++)
       for (int j=1;j<=m;j++)
         mat1[l][j]=mat[l][j];
}        

void rezolva()
{
     matcpy();
     flip();
     fliplinie();
     S=0;
     for (int l=1;l<=n;l++)
       for (int j=1;j<=m;j++)
             S=S+mat1[l][j];
        if (S>Smax)
         Smax=S;  
}

                 

void back(int k)
{
     int i;
     for (i=0;i<=1;i++)
        {
        st[k]=i;
        if (k==m)
            rezolva();
        else
            back(k+1);
}    
}

main()
{
     Smax=0;
     f>>n>>m;
     for (int i=1;i<=n;i++)
      for (int j=1;j<=m;j++)
         f>>mat[i][j];
     back(1);
     g<<Smax;
}