Cod sursa(job #563437)

Utilizator jamescrea23Perian Gheorghe-Bogdan jamescrea23 Data 25 martie 2011 09:28:18
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <fstream>
#include <ctime>
#include <iostream>

using namespace std;

long long result;
long mat[17][17];
double start,stop;
   int m,n;

void check(int i,int j)
{
   long long sum=0;
   long long sum2=0;
   int x;
   for(x=0;x<n;x++)
     if(x!=j)
       sum+=mat[i][x];
   for(x=0;x<m;x++)
    if(x!=i)
       sum+= mat[x][j];

   for(x=0;x<n;x++)
      sum2+=(-mat[i][x]);
   for(x=0;x<m;x++)
      sum2+=(-mat[x][j]);

   if(sum2>sum)
   {
    cout << i << " "<< j << endl;
    for(x=0;x<m;x++)
         if(x!=i)
             mat[x][j]*=-1;
      for(x=0;x<n;x++)
         if(x!=j)
           mat[i][x]*=-1;
   }

   
   
}

int main()
{
   start = clock();
   fstream f,g;
   f.open("flip.in",fstream::in);
   int i,j;

   f >> m >> n;
   for(i=0;i<m;i++)
     for(j=0;j<n;j++)
         f >> mat[i][j];
   for(i=0;i<m;i++)
     for(j=0;j<n;j++)
     {
      check(i,j);
       
     }

   for(i=0;i<m;i++)
    for(j=0;j<n;j++)
      result+=mat[i][j]; 

   g.open("flip.out",fstream::out);
   g << result ;
   g.close();
   stop=clock();
   cout << (stop-start)/CLOCKS_PER_SEC << endl;
  
   return 0;
}