Cod sursa(job #2953491)

Utilizator naivejulianIulian Macovei naivejulian Data 11 decembrie 2022 16:08:33
Problema Jocul Flip Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.62 kb
#include <iostream>
#include <fstream>
#include <bitset>

using namespace std;

ifstream in("flip.in");
ofstream out("flip.out");
int colectie[16][16];
int rezerva[16][16];
string bit_array;
int x = 0, y = 0;

int suma_rand_poz(int n) {
	int sum = 0;
	for (int j = 0; j < x; j++) {
		if (colectie[j][n]>0) {
			sum += colectie[j][n];
		}
	}
	return sum;
}
int suma_rand_neg(int n) {
	int sum = 0;
	for (int j = 0; j < x; j++) {
		if (colectie[j][n] < 0) {
			sum += colectie[j][n];
		}
	}
	return sum;
}
int suma_coloana_poz(int n) {
	int sum = 0;
	for (int i = 0; i < y; i++) {
		if (colectie[n][i] > 0) {
			sum += colectie[n][i];
		}
	}
	return sum;
}
int suma_coloana_neg(int n) {
	int sum = 0;
	for (int i = 0; i < y; i++) {
		if (colectie[n][i] < 0) {
			sum += colectie[n][i];
		}
	}
	return sum;
}

void invert_rand(int n) {
	for (int j = 0; j < x; j++) {
		colectie[j][n] = colectie[j][n]*(-1);
	}
}

void invert_coloana(int n) {
	for (int i = 0; i < y; i++) {
		colectie[n][i] *= (-1);
	}
}

int calc_tot_sum() {
	int tot_sum = 0;
	for (int i = 0; i < y; i++) {
		for (int j = 0; j < x; j++) {
			tot_sum = tot_sum + colectie[j][i];
		}
	}
	return tot_sum;
}

void print_colectie(int arr[][16]) {
	for (int i = 0; i < y; i++) {
		for (int j = 0; j < x; j++) {
			std::cout << arr[j][i] << " ";
		}
		std::cout << endl;
	}
}

void save_colectie() {
	for (int i = 0; i < y; i++) {
		for (int j = 0; j < x; j++) {
			rezerva[j][i] = colectie[j][i];
		}
	}
}

void load_colectie() {
	for (int i = 0; i < y; i++) {
		for (int j = 0; j < x; j++) {
			colectie[j][i] = rezerva[j][i];
		}
	}
}

int main()
{
	int tot_sum_max = 0, mau = 0;
	int num = 0;
	int nomber = -1;
	in >> y;
	in >> x;

	for (int i = 0; i < y; i++) {
		for (int j = 0; j < x; j++) {
			in >> num;
			colectie[j][i] = num;
		}
	}

	bit_array = bitset<32>(nomber).to_string();

	string final_str = bitset<32>(0).to_string();
	for (int i = 32-(x + y); i < 32; i++) {
		final_str[i] = '1';
	}

	while (bitset<32>(nomber).to_string() != final_str) {
		short rand = y-1, col = 0;
		nomber++;
		bit_array = bitset<32>(nomber).to_string();
		save_colectie();
		for (int i = 31; i > (31 - (x + y)); i--) {
			if (i > (31 - y)) {
				if (bit_array[i] == '1') {
					invert_rand(rand);
				}
				rand -= 1;
			}
			if (i <= (31 - y)) {
				if (bit_array[i] == '1') {
					invert_coloana(col);
				}
				col += 1;
			}
		}
		mau = calc_tot_sum();
		if (mau > tot_sum_max) {
			tot_sum_max = mau;
		}
		load_colectie();
	}

	out << tot_sum_max;

	in.close();
	out.close();
}