Cod sursa(job #1246786)

Utilizator boghiu.mariusBoghiu Marius Cristian boghiu.marius Data 21 octombrie 2014 17:41:27
Problema Jocul Flip Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.03 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);
//int noMove;
	theSum = sumaMatice();
//	do{
//		noMove=0;
	for (int i = 0; i < n; i++) {
		switchLinie(i);
		if (theSum < sumaMatice()) {
			theSum = sumaMatice();
			//	noMove=1;
		} else
			switchLinie(i);
		for (int j = 0; j < m; j++) {
			switchColoana(j);
			if (theSum < sumaMatice()) {
				theSum = sumaMatice();
				//			noMove=1;
			} else
				switchColoana(j);
		}
	}

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

	for (int i = n-1; i >=0; i--) {
			switchLinie(i);
			if (theSum < sumaMatice()) {
				theSum = sumaMatice();
				//	noMove=1;
			} else
				switchLinie(i);
			for (int j = m-1; j>=0; j--) {
				switchColoana(j);
				if (theSum < sumaMatice()) {
					theSum = sumaMatice();
					//			noMove=1;
				} else
					switchColoana(j);
			}
		}

	for (int j = m-1; j >=0; j--) {
		switchColoana(j);
		if (theSum < sumaMatice()) {
			theSum = sumaMatice();
			//	noMove=1;
		} else
			switchColoana(j);
		for (int i = n-1; i >=0; i--) {
			switchLinie(i);
			if (theSum < sumaMatice()) {
				theSum = sumaMatice();
				//			noMove=1;
			} else
				switchLinie(i);
		}
	}
//	}while(noMove);

	fprintf(g, "%d", theSum);
	fclose(f);
	fclose(g);

	return 0;
}