Cod sursa(job #1356741)

Utilizator CiurezAndreiCiurez Marius-Andrei CiurezAndrei Data 23 februarie 2015 16:04:17
Problema Copii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <fstream>
#include <cstring>

using namespace std;

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

int k, i, j, n, prieteni[11][11], x[11], Sol, nr_pr[11];
char c;


int check(){

    int copii[11];

    for(int j = 1; j <= nr_pr[n]; j ++)
    {
        for(int i = 0; i <= 10; i ++)
        {
            copii[i] = 0;
        }
        copii[j] = 1;
        for(int i = 1; i <= n; i ++)
        {
            if(x[i] == j)
            {
                for(int l = 1; l <= n; l ++)
                    {
                        if(prieteni[i][l] == 1)
                        {
                            copii[x[l]] = 1;
                        }
                    }
            }
        }
        for(int i = 1; i <= nr_pr[n] ; i ++)
        {
            if(copii[i] != 1 || nr_pr[n] == 1)
                return 0;
        }
    }
        return 1;

}
void back_tracking(int k){

    if(k == n + 1)
    {
        if(check())
        {
            Sol ++;
        }
    }
    else
        {
            for(int i = 1; i <= nr_pr[k - 1] + 1; i ++)
                {
                x[k] = i;
                nr_pr[k] = max(nr_pr[k - 1], x[k]);
                back_tracking(k + 1);
                }
        }
}
int main()
{
    fin >> n;
    fin.get();
    for(i = 1; i <= n; i ++)
    {
        for(j = 1; j <= n; j ++)
        {
            fin >> c;
            prieteni[i][j] = c - '0';
        }
    }

    back_tracking(1);

    fout << Sol;

    return 0;
}