Cod sursa(job #29743)

Utilizator carloneIoan-Carol Plangu carlone Data 9 martie 2007 21:19:00
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include<stdio.h>
#include<conio.h>
long int a[20][20],b[20],c[20],s1=-1000000,s2;
int n,m,g,h,w,w1,s,max,bol=1;
void check()
{
    for(g=0;g<n;g++)
    {
     s=0;
     for(h=0;h<m;h++)
     s=s+a[g][h];
     b[g]=s;
    }
    for(g=0;g<m;g++)
    {
     s=0;
     for(h=0;h<n;h++)
     s=s+a[h][g];
     c[g]=s;
    }
    max=0;
    for(g=0;g<n;g++)
     if(b[g]<max)
      {max=b[g];w=0;w1=g;}
    for(g=0;g<m;g++)
     if(c[g]<max)
      {max=c[g];w=1;w1=g;}
}
int sum()
{
    int suma=0;
    for(g=0;g<n;g++)
    for(h=0;h<m;h++)
    suma=suma+a[g][h];
    return suma;
}
void flipg(int q)
{
     for(h=0;h<m;h++)
     {
      a[q][h]=0-a[q][h];
     }               
}
void fliph(int q)
{
     for(g=0;g<n;g++)
     {
      a[g][q]=0-a[g][q];
     }
}
int main()
{
    FILE *f;
    f=freopen("flip.in","r",stdin);
    //freopen("flip.out","w",stdout);
    scanf("%d",&n);scanf("%d",&m);
    
    for(g=0;g<n;g++)
     {
      for(h=0;h<m;h++)
      {
       scanf("%d",&a[g][h]);
      }
      
     }
    while(s1<s2)
    {
    check();
    if(w==0)
    flipg(w1);
    else
    fliph(w1);
    if(bol==0)
    s1=sum();
    else
    s2=sum();
    if(bol==0)
    bol=1;
    else
    bol=0;
    }
    printf("%d",s1);
    getch();
    return 0;
}