Cod sursa(job #902626)

Utilizator Pop_EmilPal Tamas Pop_Emil Data 1 martie 2013 15:32:24
Problema Jocul Flip Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <stdio.h>

using namespace std;

int N,M,t[17][17],v[17][17];
long RES1=0,RES2=0;

void _read()
{
 freopen("flip.in","r",stdin);
 freopen("flip.out","w",stdout);

 scanf("%d %d",&N,&M);

 for(int i=0;i<N;i++)
  for(int j=0;j<M;j++)
    scanf("%d",&v[i][j]);
}

void flipcol(int m)
{
    for(int i=0;i<N;i++)
      t[i][m]*=-1;
}
void fliprand(int n)
{
    for(int j=0;j<M;j++)
     t[n][j]*=-1;

}

int sumcol(int m)
{
   long s=0;
    for(int i=0;i<N;i++)
      s+=t[i][m];
    return s;
}

int sumrand(int n)
{
    long s=0;
    for(int j=0;j<M;j++)
     s+=t[n][j];
   return s;
}

int main()
{

_read();

//1
for(int i=0;i<N;i++)
 for(int j=0;j<M;j++)
    t[i][j]=v[i][j];

bool modif=true;
while(modif)
 {
   modif=false;

   for(int j=0;j<M;j++)
      {
         if(sumcol(j)<0){ flipcol(j); modif=true; }
      }

 for(int i=0;i<N;i++)
      {
         if(sumrand(i)<0){ fliprand(i); modif=true; }
      }

 }

for(int j=0;j<M;j++)
    RES1+=sumcol(j);

//2
for(int i=0;i<N;i++)
 for(int j=0;j<M;j++)
    t[i][j]=v[i][j];

 modif=true;
while(modif)
 {
   modif=false;

 for(int i=0;i<N;i++)
      {
         if(sumrand(i)<0){ fliprand(i); modif=true; }
      }

 for(int j=0;j<M;j++)
      {
         if(sumcol(j)<0){ flipcol(j); modif=true; }
      }

 }

for(int j=0;j<M;j++)
    RES2+=sumcol(j);

if(RES1>=RES2)
    printf("%ld",RES1);
 else printf("ld",RES2);

return 0;
}