Cod sursa(job #3195211)

Utilizator vladimir.gavris.1Gavris Mihai Vladimir vladimir.gavris.1 Data 20 ianuarie 2024 11:37:31
Problema Jocul Flip Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>

using namespace std;

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

#define MAXN 16

int array[ MAXN ][ MAXN ];
bool lines[ MAXN ];
int m, n, maxx;

void read( )
{
    in >> n >> m;
    for( int i = 0; i < n; i++ )
    {
        for( int j = 0; j < m; j++ )
        {
            in >> array[ i ][ j ];
        }
    }
}

void backt( int pos )
{
    if( pos == m )
    {
        int total = 0;
        for( int i = 0; i < n; i++ )
        {
            int colsum = 0;
            for( int j = 0; j < m; j++ )
            {
                if( lines[ j ] == true )
                {
                    colsum -= array[ i ][ j ];
                }
                else
                {
                    colsum += array[ i ][ j ];
                }
            }

            if( colsum < 0 )
            {
                colsum *= ( - 1 );
            }

            total += colsum;
        }

        maxx = std::max( maxx, total );
    }
    else
    {
        lines[ pos ] = true;
        backt( pos + 1 );

        lines[ pos ] = false;
        backt( pos + 1 );
    }
}

void solve( )
{
    read( );
    backt( 0 );

    out << maxx;
}


int main()
{
    solve( );
    return 0;
}