Cod sursa(job #1455960)

Utilizator GilgodRobert B Gilgod Data 29 iunie 2015 15:45:35
Problema Jocul Flip Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <fstream>

const char IN[] = "flip.in", OUT[] = "flip.out";
const int NMAX = 16;
const int INF = 0x3f3f3f3f;

using namespace std;

int N, M;
int A[NMAX][NMAX];
int row[NMAX], col[NMAX];
int maxSum = -INF;

inline void read_data() {
	freopen(IN, "r", stdin);
	scanf("%d %d", &N, &M);
	for (int i = 0; i < N; ++i)
	for (int j = 0; j < N; ++j)
		scanf("%d", &A[i][j]);
	fclose(stdin);
}

void chk_max() {
	int s = 0;
	for (int i = 0; i < N; ++i)
	for (int j = 0; j < N; ++j)
		s += A[i][j] * row[i] * col[j];
	if (s > maxSum) maxSum = s;
}

void bk(int step) {
	if (step == N) {
		chk_max(); return;
	}
	for (int rv = -1; rv <= 1; rv += 2)
	for (int cv = -1; cv <= 1; cv += 2)
		row[step] = rv, col[step] = cv, bk(step + 1);
}

int main() {
	read_data();
	bk(0);
	fprintf(fopen(OUT, "w"), "%d\n", maxSum);
	return 0;
}