Cod sursa(job #487393)

Utilizator GodiesVlad Voicu Godies Data 25 septembrie 2010 00:50:45
Problema Jocul Flip Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <cstdio>
#include <cstdlib>
#include <iostream>

#define NMAX 17
#define DMAX 65536

using namespace std;

int main()
{
	FILE *f, *g;
	f = fopen("flip.in", "rt");
	g = fopen("flip.out", "wt");
	int i, j, k, l, n, h, m, sum, r, total_sum, max;
	int a[NMAX][NMAX];
	fscanf (f, "%d%d", &n, &m);
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			fscanf (f, "%d", &a[i][j]);
	max = -1;
	for (h = 0; h <= DMAX; h++) {
		for (i = 0; i < n; i++) {
			total_sum = 0;
			r = 1;
			if ((h & (1 << i)) > 0) {
				r = -1;
			} 
			for (j = 0; j < m; j++) {
				sum = 0;
				a[i][j] = r*a[i][j];
				for (l = 0; l < n; l++) {
					sum += a[l][j];
				}
				if (sum < 0) {
					total_sum += -sum;
				}
				else
					total_sum += sum;
			}
			if (total_sum > max) {
				max = total_sum;
			} 
		}
	}
	fprintf(g, "%d", max);
	fclose(f);
	fclose(g);
	return 0;
}