Cod sursa(job #2283194)

Utilizator Adi55AdrianCiurea Adi55 Data 15 noiembrie 2018 09:26:28
Problema Jocul Flip Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.65 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("flip.in");
ofstream g ("flip.out");
int n,m,st[1000],st2[1000],k=0,k2=0,i,j,x,x2,maxim=0,a[100][100],s;
int E_Valid2()
{
    for(i=0;i<k2;i++)
    {
        if(st2[i]>=st2[k2])
        {
            return 0;
        }
    }
return 1;
}
int Am_Succesor2()
{
    if(st2[k2]<m-1)
    {
        st2[k2]++;
        return 1;
    }
return 0;
}
void backcoloane()
{
     int AS;
    st2[k2]=-1;
    while(k2>-1)
    {
        while((AS=Am_Succesor2())&&!E_Valid2());
        {
            if(AS)
            {
                if(k2==x2)
                {
                    s=0;
                    for(i=0;i<x;i++)
                    {
                        for(j=0;j<x2;j++)
                        {
                            a[st[i]][st[j]]*=-1;
                        }
                    }
                    for(i=0;i<n;i++)
                    {
                        for(j=0;j<m;j++)
                        {
                            s+=a[i][j];
                        }
                    }

                    for(i=0;i<x;i++)
                    {
                        for(j=0;j<x2;j++)
                        {
                            a[st[i]][st[j]]*=-1;
                        }
                    }
                    if(maxim<s)
                    {
                        maxim=s;
                    }
                }
                else{
                    k2++;
                    st2[k2]=-1;
                }
            }
            else{
                k2--;
            }
        }
    }
}
void forcubackcoloane()
{
for(x2=0;x2<=m;x2++)
{
    k2=0;
    backcoloane();
}
}
int Am_Succesor()
{
    if(st[k]<n-1)
    {
        st[k]++;
        return 1;
    }
return 0;
}
int E_Valid()
{
for(i=0;i<k;i++)
    {
        if(st[i]>=st[k])
        {
            return 0;
        }
    }
return 1;
}
void backlinii()
{
    int AS;
    st[k]=-1;
    while(k>-1)
    {
        while((AS=Am_Succesor())&&!E_Valid());
        {
            if(AS)
            {
                if(k==x)
                {
                    forcubackcoloane();
                }
                else{
                    k++;
                    st[k]=-1;
                }
            }
            else{
                k--;
            }
        }
    }
}
int main()
{
    f>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            f>>a[i][j];
        }
    }
    for(x=0;x<=n;x++)
    {
    k=0;
    backlinii();
    }
    g<<maxim;
}