Cod sursa(job #2191823)

Utilizator Florin77Nacu Florin Florin77 Data 3 aprilie 2018 19:57:29
Problema Jocul Flip Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include<iostream>
#include<fstream>
using namespace std;

int N, M, a[100][100];

void comutare_linie(int l)
{
	for (int z = 1; z <= M; z++)
		a[l][z] = -a[l][z];
}

void comutare_coloana(int c)
{
	for (int z = 1; z <= N; z++)
		a[z][c] = -a[z][c];
}

void suma_matrice(int& s)
{
	s = 0;
	for (int p = 1; p <= N; p++)
		for (int q = 1; q <= M; q++)
			s += a[p][q];
}

int main()
{
	ifstream f("flip.in");
	ofstream g("flip.out");
	int i, j;
	f >> N >> M;
	for (i = 1; i <= N; i++)
		for (j = 1; j <= M; j++)
			f >> a[i][j];
	f.close();
	bool sw=false;
	int max, s;
	suma_matrice(max);
	do
	{
		sw = true;
		for (i = 1; i <= N; i++)
		{
			comutare_linie(i);
			suma_matrice(s);
			if (s > max)
			{
				max = s;
				sw = false;
			}
			else
				comutare_linie(i);
		}
		for (j = 1; j <= M; j++)
		{
			comutare_coloana(j);
			suma_matrice(s);
			if (s > max)
			{
				max = s; 
				sw = false;
			}
			else
				comutare_coloana(j);
		}
	} while (sw == false);
	g << max;
	g.close();
}