Cod sursa(job #1447241)

Utilizator allexx2200Atanasiu Alexandru-Marian allexx2200 Data 3 iunie 2015 22:16:49
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <iostream>
#include <climits>

#define FIN "flip.in"
#define FOUT "flip.out"

#define MAX_LEN 16
#define GET_BIT(n,p) ((n>>p)&1)

FILE *in, *out;

int mat[MAX_LEN][MAX_LEN], M, N;
int act[MAX_LEN][MAX_LEN];
int result = INT_MIN;

void solve(){
	long p = pow(2.f,N);
	for(int caz=0; caz < p; caz++){
		memcpy(act, mat, sizeof(mat));

		for(long j=0; j < N; j++){
			if(GET_BIT(caz,j)){
				for(int k=0; k < M; k++){
					act[j][k] = -act[j][k];
				}
			}
		}
		int sum = 0;
		for(int k=0; k < M; k++){
			int column_sum = 0;
			for(int j=0; j < N; j++){
				column_sum += act[j][k];
			}
			if(column_sum < 0) column_sum = - column_sum;
			sum += column_sum;
		}

		if(sum > result) result = sum;
	}
}

int main(){
	in = fopen(FIN, "rt");
	out = fopen(FOUT, "wt");
	if(!in || !out) return 1;

	fscanf(in, "%d%d", &N, &M);
	for(int i=0; i < N; i++){
		for(int j=0; j < M; j++){
			fscanf(in, "%d", &mat[i][j]);
		}
	}

	solve();
	fprintf(out, "%d", result);

	fclose(in);
	fclose(out);
	return 0;
}