Cod sursa(job #2301151)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 12 decembrie 2018 18:25:34
Problema Copii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("copii.in");
ofstream fout("copii.out");

char priet[11][11];
bool used[12];
int nrEchipTot = 0;
int echipe[15];
int pr[15];
int grupP[12];
int n;
int adaug;
int rez = 0;

void bkt(int pas)
{
    if(pas == n + 1)
    {
        for(int i = 1; i <= nrEchipTot; ++i)
        {
            for(int j = 1; j <= nrEchipTot; ++j)
            {
                if(j == i)
                    continue;

                if((grupP[i] & echipe[j]) == 0){
                    return;
                }
            }

        }

        rez++;
        return;
    }

    nrEchipTot++;
    int aux = grupP[nrEchipTot];
    echipe[nrEchipTot] += (1 << pas);
    grupP[nrEchipTot] |= pr[pas];

    bkt(pas + 1);

    echipe[nrEchipTot] -= (1 << pas);
    grupP[nrEchipTot] = aux;
    nrEchipTot --;

    for(int j = 1; j <= nrEchipTot; ++j)
    {
        aux = grupP[j];
        echipe[j] += (1 << pas);
        grupP[j] |= pr[pas];

        bkt(pas + 1);

        echipe[j] -= (1 << pas);
        grupP[j] = aux;
    }
}

int main()
{
    fin >> n;

    fin.get();
    for(int i = 1; i <= n; ++i)
    {
        fin.getline(priet[i], 11);

        for(int j = 0; j < n; ++j)
        {
            if(priet[i][j] == '1')
            {
                pr[i] += (1 << (j + 1));
            }
        }
    }

    bkt(1);

    fout << (rez - 1) << '\n';
    return 0;
}