Pagini recente » Cod sursa (job #2604099) | Cod sursa (job #210989) | Cod sursa (job #1822475) | Cod sursa (job #1364188) | Cod sursa (job #1246770)
//============================================================================
// Name : Flip.cpp
// Author : Boghiu Marius Cristian
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstdio>
#include <fstream>
using namespace std;
FILE *f, *g;
int n, m;
int **mat;
int sumaMatice()
{
int suma=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
suma+=mat[i][j];
return suma;
}
int sumaLinie(int i) {
int suma = 0;
//cout << n<<" "<<m<<".";
for (int j = 0; j < m; j++) {
//cout<<mat[i][j]<<" ";
suma += mat[i][j];
}
return suma;
}
int sumaColoana(int j)
{
int suma =0;
for(int i=0;i<n;i++)
suma+= mat[i][j];
return suma;
}
void switchLinie(int i)
{
for(int j=0;j<m;j++)
mat[i][j]*=-1;
}
void switchColoana(int j)
{
for(int i=0;i<n;i++)
mat[i][j]*=-1;
}
int main() {
int theSum=0;
// ofstream myfile;
// myfile.open("flip.in");
// myfile << 2 <<" "<< 2 << "\n" << 3 << " " << 3 << "\n" << 4 << " " << 4;
// myfile.close();
f = fopen("flip.in", "r");
g = fopen("flip.out", "w");
fscanf(f, "%d", &n);
fscanf(f, "%d", &m);
mat = new int*[n];
for (int i = 0; i < n; i++)
mat[i] = new int[m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
fscanf(f, "%d", &mat[i][j]);
// cout<<mat[i][j]<<" ";
}
//cout<<sumaLinie(1);
int noMove;
theSum=sumaMatice();
do{
noMove=0;
for(int i=0;i<n;i++)
{
switchLinie(i);
if(theSum<sumaMatice())
{
theSum=sumaMatice();
noMove=1;
}
else
switchLinie(i);
for(int j=0;j<m;j++)
{
switchColoana(j);
if(theSum<sumaMatice())
{
theSum=sumaMatice();
noMove=1;
}
else
switchColoana(j);
}
}
}while(noMove);
fprintf(g, "%d", theSum);
fclose(f);
fclose(g);
return 0;
}