Pagini recente » Cod sursa (job #628150) | Cod sursa (job #746678) | Cod sursa (job #2546776) | Cod sursa (job #1473246) | Cod sursa (job #1246776)
//============================================================================
// 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++)
{
if(j==0)
switchLinie(i);
switchColoana(j);
if(theSum<sumaMatice())
{
theSum=sumaMatice();
// noMove=1;
}
else
switchColoana(j);
}
for(int j=0;j<m;j++)
{
if(j==0)
switchLinie(i);
switchColoana(j);
if(theSum<sumaMatice())
{
theSum=sumaMatice();
// noMove=1;
}
else
switchColoana(j);
}
}
// }while(noMove);
fprintf(g, "%d", theSum);
fclose(f);
fclose(g);
return 0;
}