Cod sursa(job #1032695)

Utilizator magnusleoMarcu Marian magnusleo Data 15 noiembrie 2013 22:46:27
Problema Jocul Flip Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include<iostream>
#include<fstream>

using namespace std;

struct matrice
{
	int n;
	int m;
	int a[50][50];

	void citire()
	{
		ifstream f("flip.in");
		f >> n;
		f >> m;
		for (int i = 1; i <= n;i++)
		for (int j = 1; j <= m; j++)
			f >> a[i][j];
		f.close();
	}
	void afisare()
	{
		ofstream g("flip.out");
		int result = 0;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
				result += a[i][j]; 
		}
		g << result;
	}
	bool checkLine(int i)
	{
		int result = 0;
		for (int j = 1; j <= m; j++)
		{
			result =result + a[i][j];
			if (result < 0)
			{
				return true;
			}
		}
		return false;

	}
	bool checkCol(int j)
	{
		int result = 0;
		for (int i = 1; i <= n; i++)
		{
			result += a[i][j];
			if (result < 0)
				return true;
		}
		return false;
	}
	void flipCol(int j)
	{
		for (int i = 1; i <= n; i++)
			a[i][j] = a[i][j] * -1;
		//cout << "Coulumn:" << j << " succesfully flipped" << endl;
	}
	void flipLine(int i)
	{
		for (int j = 1; j <= m; j++)
			a[i][j] = a[i][j] * -1;
	//	cout << "Line:" << i << "succesfully flipped" << endl;
	}
	void optim()
	{
		for (int i = 1; i <= n;i++)
		if (checkLine(i) == true) flipLine(i);
		for (int j = 1; j <= m;j++)
		if (checkCol(j) == true) flipCol(j);
	}

};

int main()
{
	matrice mat;
	mat.citire();
	mat.optim();
	mat.afisare();

	return 0;
}