Cod sursa(job #2255953)

Utilizator SAlinStanciu Alin SAlin Data 7 octombrie 2018 19:14:01
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
using namespace std;

int n, m; //matrix dimension
int sum_max;
int matrix[17][17];

int sumMatrixElements();
void switchRowColElements(int val, bool elem); // elem true is row and elem false is column 
void BKT(int k);
bool compareSumMax(int sumValue);

ifstream fin ("flip.in");
ofstream fout ("flip.out");

int main() {

	fin>>n>>m; //read matrix dimensions
	for(int i = 0; i < n; i++ ) {
		for(int j = 0; j < m; j++ ){
			fin>>matrix[i][j];
		}
	}
	
	for(int i = 0; i < m; i++) {
		
		switchRowColElements(i, false);
		BKT(0);
		switchRowColElements(i, false);
	
	}
	
	fout<<sum_max;
	return 0;
}

bool compareSumMax(int sumValue) {
	return sumValue > sum_max ? 1 : 0;
}

void switchRowColElements(int val, bool elem) {
	
	int i;
	if( elem ) {
		for( i = 0; i < m; i++) {
			matrix[val][i] = (-1) * matrix[val][i];
		}
	} else if( !elem ){
		for( i = 0; i < n; i++) {
			matrix[i][val] = (-1) * matrix[i][val];
		}
	}
	
}

int sumMatrixElements() {
	
	int sum_Matrix = 0;
	for(int i = 0; i < n; i++ ) {
		for(int j = 0; j < m; j++ ){
			sum_Matrix += matrix[i][j];
		}
	}
	return sum_Matrix;

}

void BKT(int k) {
	int sum_matrix_partial = 0;
	if( k == n ) {
		//fout<<sum_max<<" || ";
	} else {
		for(int i = 0; i < n; i++ ) {
			switchRowColElements( i, true );
			sum_matrix_partial = sumMatrixElements();
			if(compareSumMax(sum_matrix_partial)) {
				sum_max = sum_matrix_partial;
			}
			BKT(k+1);
			switchRowColElements( i, true );
		
		}
	
	}

}