Cod sursa(job #1167782)

Utilizator calin13Calin Nicolau calin13 Data 5 aprilie 2014 22:07:32
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.64 kb
#include <iostream>
#include <fstream>
using namespace std;

int n, m, a[17][17], ans, semn[16];

void read()
{
	ifstream f("flip.in");
	f >> n >> m;
	for (int i = 0;  i < n; ++i)
		for (int j = 0; j < m; ++j) {
			f >> a[i][j];
			ans += a[i][j];
		}
	f.close();
}

void solve()
{
  int i, j, s=0, tmp;

  for (i = 0; i < n; ++i) {
	tmp = 0;
	for (j = 0; j < m; ++j)
	  tmp += a[i][j] * semn[j];
	s += max(tmp, -tmp);
  }
  ans = max(ans, s);
}
void back(int k)
{
  if (k == m) 
	solve();
  else {
	semn[k] = -1;
	back(k+1);
	semn[k] = 1;
	back(k+1);
  }
}
int main ()
{
	read();
	back(0);
	
	ofstream g("flip.out");
	g << ans;
	g.close();

	return 0;
}