Pagini recente » Cod sursa (job #2784722) | Cod sursa (job #1547523) | Cod sursa (job #16605) | Cod sursa (job #2772986) | Cod sursa (job #2253259)
#include<bits/stdc++.h>
using namespace std;
const int N_MAX = 19, M_MAX = 19;
int initialMat[N_MAX][M_MAX];
void changeSignRow(int row, int N)
{
for(int i = 1; i <= N; i ++)
initialMat[row][i] = (-1) * initialMat[row][i];
}
void changeSignColumn(int column, int M)
{
for(int i = 1; i <= M; i ++)
initialMat[i][column] = (-1) * initialMat[i][column];
}
void print(int N, int M)
{
ofstream fout("flip.out");
int best = 0;
for(int i = 1; i <= N; i ++){
for(int j = 1; j <= M; j ++)
best += initialMat[i][j];
}
fout << best;
fout.close();
}
int main()
{
ifstream fin ("flip.in");
int N, M;
fin >> N >> M;
for(int i = 1; i <= N; i ++)
for(int j = 1; j <= M; j++)
fin >> initialMat[i][j];
int positiveValues, negativeValues;
for(int i = 1; i <= N; i ++ )
{
positiveValues = negativeValues = 0;
for(int j = 1; j <= M; j ++){
if( initialMat[i][j] > 0 ) positiveValues += initialMat[i][j];
else negativeValues += initialMat[i][j];
}
negativeValues = (-1)*negativeValues;
if( negativeValues > positiveValues )
changeSignRow( i, N );
}
for(int j = 1; j <= M; j ++)
{
positiveValues = negativeValues = 0;
for(int i = 1; i <= N; i ++)
{
if( initialMat[i][j] > 0 ) positiveValues += initialMat[i][j];
else negativeValues += initialMat[i][j];
}
negativeValues = (-1)*negativeValues;
if( negativeValues > positiveValues )
changeSignColumn( j, N );
}
print(N, M);
fin.close();
return 0;
}