Cod sursa(job #2137408)

Utilizator ionyp2001Patroescu Ion-Daniel ionyp2001 Data 20 februarie 2018 19:36:46
Problema Jocul Flip Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
using namespace std;
ifstream f("flip.in");
ofstream g("flip.out");
int a[17][17],N,M,Minl=2000001,Minc=2000001,x,y,S,i,j;
int sumal(int a[17][17], int i, int N, int M)
{
    int s=0,j;
    for(j=1;j<=M;j++)
        s=s+a[i][j];
    return s;
}
int sumac(int a[17][17], int i, int N, int M)
{
    int s=0,j;
    for(j=1;j<=N;j++)
        s=s+a[j][i];
    return s;
}
int main()
{
  f>>N>>M;
  for(i=1;i<=N;i++)
    for(j=1;j<=M;j++)
        f>>a[i][j];
  for(i=1;i<=M;i++)
    if(Minc>sumac(a,i,N,M))
  {
        Minc=sumac(a,i,N,M);
        y=i;
  }
  if(Minc<0)
    for(i=1;i<=N;i++)
        a[i][y]*=-1;
  for(i=1;i<=N;i++)
    if(Minl>sumal(a,i,N,M))
  {
        Minl=sumal(a,i,N,M);
        x=i;
  }
  if(Minl<0)
    for(i=1;i<=M;i++)
    {
        if(Minc<0)
            if(i!=y)
            a[x][i]*=-1;
        else
            if(Minc<0)
            a[x][i]*=-1;
    }
  for(i=1;i<=N;i++)
        for(j=1;j<=M;j++)
                S=S+a[i][j];
    g<<S;
}