Cod sursa(job #1087418)

Utilizator SCBbestofSocaciu-Cumpanasu Bogdan SCBbestof Data 19 ianuarie 2014 13:23:04
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f ("flip.in");
ofstream g ("flip.out");

int n,m,i,j,a[50][50],solutie = 0,y[50];

void sol()
{
    int suma = 0, total = 0;
    for(i = 1; i <= m; i++)
    {
        suma = 0;
        for(j = 1; j <= n; j++)
            suma += (a[j][i] * y[j]);
        if(suma < 0)
            suma = suma * -1;
        total+=suma;
    }
    if(solutie < total)
        solutie = total;
}
void backtracking(int k)
{
    if(k == n + 1)
        sol();
    else
        for(int i = -1; i <= 1; i += 2)
        {
            y[k] = i;
            backtracking(k + 1);
        }
}
int main()
{
    f>>n>> m;
    for(i = 1; i <= n; i++)
        for(j = 1; j <= m; j++)
            f>>a[i][j];
    backtracking(1);
    g<<solutie;
    return 0;
}