Cod sursa(job #1476932)

Utilizator p.cristi0Cristi p.cristi0 Data 25 august 2015 14:16:10
Problema Jocul Flip Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include<iostream>
#include<fstream>
//#include<Windows.h>
using namespace std;
ifstream in;
ofstream out;

int main() {
	unsigned short N, M;
	long long int a[16][16], s1=0, s2=0, S=0, max=-1;

	in.open ("flip.in");
	in >> N >> M;
	for (short i = 0; i < N; i++)
	{
		for (short j = 0; j < M; j++)
		{
			in >> a[i][j];
		}
	}
	in.close();

	do
	{
		for (short i = 0; i < N; i++)
			{
				for (short j = 0; j < M; j++)
				{
					s1 += a[i][j];
					s2 -= a[i][j];
				}
				if (s2 > s1)
				{
					for (short j = 0; j < M; j++)
					{
						a[i][j] = -a[i][j];
					}
				}
				s1 = 0;
				s2 = 0;
			}
			for (short j = 0; j < M; j++)
			{
				for (short i = 0; i < N; i++)
				{
					s1 += a[i][j];
					s2 -= a[i][j];
				}
				if (s2 > s1)
				{
					for (short i = 0; i < N; i++)
					{
						a[i][j] = -a[i][j];
					}
				}
				s1 = 0;
				s2 = 0;
			}
			for (short i = 0; i < N; i++)
			{
				for (short j = 0; j < M; j++)
				{
					S += a[i][j];
				}
			}
			if (max < S)
			{
				max = S;
			}
	} while (max<S);
	
	
/*	for (short i = 0; i < N; i++)
	{
		for (short j = 0; j < M; j++)
		{
			cout << a[i][j] <<" ";
		}
		cout << "\n";
	}
	cout << "\n" << S <<"\n";
	system("pause"); */
	out.open("flip.out");
	out << S;
	out.close();
}