Cod sursa(job #1246765)

Utilizator boghiu.mariusBoghiu Marius Cristian boghiu.marius Data 21 octombrie 2014 17:16:27
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
//============================================================================
// 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 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;
}
int main() {
int theSum=0;
//	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);

	theSum=sumaMatice();
	for(int i=0;i<n;i++)
	{
		switchLinie(i);
		if(theSum<sumaMatice())
			theSum=sumaMatice();
		else
			switchLinie(i);
		for(int j=0;j<m;j++)
		{
			switchColoana(j);
			if(theSum<sumaMatice())
				theSum=sumaMatice();
			else
				switchColoana(j);
		}
	}


g<< theSum;
	fclose(f);
	fclose(g);

	return 0;
}