Cod sursa(job #2546407)

Utilizator Alex_AeleneiAlex Aelenei Ioan Alex_Aelenei Data 14 februarie 2020 10:06:34
Problema Jocul Flip Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <cmath>
#include <fstream>
#include <iostream>

using namespace std;

int a [ 20 ] [ 20 ] ;
int bitmask [ 20 ] ;
int n , m , ans = 0 ;

void backtrack ( int poz )
{
    if ( poz == n + 1 )
    {
        int aux = 0 , i , j , s , val ;

        for ( int j = 1 ; j <= m ; ++ j )
        {
            s = 0 ;

            for ( int i = 1 ; i <= n ; ++ i )
            {
                if ( bitmask [ i ] ) val = - 1 ;
                else val = 1 ;

                s += a [ i ] [ j ] * val ;
            }

            aux += abs ( s ) ;
        }

        ans = max ( ans , aux ) ;

        return ;
    }

    bitmask [ poz ] = 0 ;
    backtrack ( poz + 1 ) ;
    bitmask [ poz ] = 1 ;
    backtrack ( poz + 1 ) ;
}

int main()
{
    ifstream in ( "flip.in" ) ;
    ofstream out ( "flip.out" ) ;

    int i , j ;

    in >> n >> m ;

    for ( i = 1 ; i <= n ; ++ i )
        for ( j = 1 ; j <= m ; ++ j )
            in >> a [ i ] [ j ] ;

    backtrack ( 1 ) ;

    out << ans ;

    return 0;
}