Cod sursa(job #3326998)

Utilizator anaska92Andrei Anusca anaska92 Data 1 decembrie 2025 19:43:25
Problema Jocul Flip Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <fstream>
using namespace std;

void readMatrix(int n, int m, ifstream &fin, long arr[][100])
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			fin >> arr[i][j];
		}
	}
}

void calculateColumnSums(int n, int m, long arr[][100], long sumColumns[])
{
	for (int j = 0; j < m; j++)
	{
		long sum = 0;
		for (int i = 0; i < n; i++)
		{
			sum += arr[i][j];
		}
		sumColumns[j] = sum;
	}
}

void calculateRowSums(int n, int m, long arr[][100], long sumRows[])
{
	for (int i = 0; i < n; i++)
	{
		long sum = 0;
		for (int j = 0; j < m; j++)
		{
			sum += arr[i][j];
		}
		sumRows[i] = sum;
	}
}

void calculateSumOfMatrix(int n, int m, long arr[][100], long &totalSum)
{
	totalSum = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			totalSum += arr[i][j];
		}
	}
}

void flipColumns(int n, int m, long arr[][100], long sumColumns[])
{
	for (int j = 0; j < m; j++)
	{
		if (sumColumns[j] < 0)
		{
			for (int i = 0; i < n; i++)
			{
				arr[i][j] = -arr[i][j];
			}
		}
	}
}

void flipRows(int n, int m, long arr[][100], long sumRows[])
{
	for (int i = 0; i < n; i++)
	{
		if (sumRows[i] < 0)
		{
			for (int j = 0; j < m; j++)
			{
				arr[i][j] = -arr[i][j];
			}
		}
	}
}

int main()
{
	ifstream fin("flip.in");
	ofstream fout("flip.out");
	int n, m;
	fin >> n >> m;
	long arr[100][100];
	long sumColumns[100] = { 0 };
	long sumRows[100] = { 0 };
	readMatrix(n, m, fin, arr);
	
	calculateColumnSums(n, m, arr, sumColumns);

	flipColumns(n, m, arr, sumColumns);

	calculateRowSums(n, m, arr, sumRows);

	flipRows(n, m, arr, sumRows);

	long totalSum;
	calculateSumOfMatrix(n, m, arr, totalSum);
	fout << totalSum << endl;
}