Cod sursa(job #1592612)

Utilizator Sergiu1256Ionita Sergiu1256 Data 7 februarie 2016 19:56:20
Problema Jocul Flip Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include<bits/stdc++.h>
 using namespace std;
ofstream fout("flip.out");
ifstream fin("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);
}    
} 
int main()
{
     Smax=0;
     fin>>n>>m;
     for (int i=1;i<=n;i++)
      for (int j=1;j<=m;j++)
         fin>>mat[i][j];
     back(1);
     fout<<Smax;
     return 0;
}