Pagini recente » Cod sursa (job #1487746) | Cod sursa (job #107589) | Profil Rodik_Rody | Monitorul de evaluare | Cod sursa (job #1249939)
//============================================================================
// 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 theSum = 0;
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;
}
void specialCalculationOne(int i)
{
if(theSum < sumaMatice())
{
theSum = sumaMatice();
}
switchLinie(i);
// for(int j=0;j<m;j++)
// {
// if(theSum < sumaMatice())
// theSum = sumaMatice();
// switchColoana(j);
// }
if(theSum < sumaMatice())
{
theSum = sumaMatice();
}
else
switchLinie(i);
}
void specialCalculationTwo(int j)
{
if(theSum < sumaMatice())
{
theSum = sumaMatice();
}
switchColoana(j);
if(theSum < sumaMatice())
{
theSum = sumaMatice();
for(int i=0;i<n;i++)
specialCalculationOne(i);
}
else
switchColoana(j);
}
int main() {
// 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++)
{
specialCalculationOne(i);
}
for(int j=0;j<m;j++)
{
specialCalculationTwo(j);
}
// 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);
// }
// }
//
// for (int j = 0; j < m; j++) {
// switchColoana(j);
// if (theSum < sumaMatice()) {
// theSum = sumaMatice();
// // noMove=1;
// } else
// switchColoana(j);
// for (int i = 0; i < n; i++) {
// switchLinie(i);
// if (theSum < sumaMatice()) {
// theSum = sumaMatice();
// // noMove=1;
// } else
// switchLinie(i);
// }
// }
//
// for (int i = n-1; i >=0; i--) {
// switchLinie(i);
// if (theSum < sumaMatice()) {
// theSum = sumaMatice();
// // noMove=1;
// } else
// switchLinie(i);
// for (int j = m-1; j>=0; j--) {
// switchColoana(j);
// if (theSum < sumaMatice()) {
// theSum = sumaMatice();
// // noMove=1;
// } else
// switchColoana(j);
// }
// }
//
// for (int j = m-1; j >=0; j--) {
// switchColoana(j);
// if (theSum < sumaMatice()) {
// theSum = sumaMatice();
// // noMove=1;
// } else
// switchColoana(j);
// for (int i = n-1; i >=0; i--) {
// switchLinie(i);
// if (theSum < sumaMatice()) {
// theSum = sumaMatice();
// // noMove=1;
// } else
// switchLinie(i);
// }
fprintf(g, "%d", theSum);
fclose(f);
fclose(g);
return 0;
}
// }while(noMove);