Cod sursa(job #2642609)

Utilizator AnnieMyaAnamaria Ordace AnnieMya Data 16 august 2020 12:21:57
Problema Jocul Flip Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <fstream>

using namespace std;

short n, m, i, j, x, y;
long a[16][16], maxim, s,p[17];


int main ()
{
  ifstream in ("flip.in");
  in >> n >> m;
  maxim = 0;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      {
	    in >> a[i][j];
	    maxim += a[i][j];
      }
  in.close ();
  p[0] = 1;
  for (i = 1; i < 17; i++)
    p[i] = p[i - 1] * 2;

  for (x = 0; x < p[n]; x++)
    for (y = 0; y < p[m]; y++)
      {
    	s = 0;
	    for (i = 0; i < n; i++)
	     for (j = 0; j < m; j++)
	          if ((x & p[i]) ^ (y & p[j]))
	              s -= a[i][j];
	         else
	         s += a[i][j];
	    if (s > maxim)
	      maxim = s;
        }
  ofstream out ("flip.out");
  out << maxim;
  out.close ();

  return 0;
}