Cod sursa(job #1770081)

Utilizator pringonGoje Samuel Andrei Daniel pringon Data 3 octombrie 2016 18:56:21
Problema Copii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <fstream>
#include <string.h>

using namespace std;

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

int n;
char mat[11][11];
bool gasti[11][11];
int sol[11];
int grupari;

void prelucrare(int m) {

    for(int i = 1; i <= m; i++) {
        for(int j = 1; j <= m; j++) {
            gasti[i][j] = 0;
        }
    }

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            if(mat[i][j] == '1')
                gasti[sol[i]][sol[j]] = 1;

    for(int i = 1; i <= m; i++) {
        for(int j = 1; j <= m; j++) {
            if(!gasti[i][j] and i != j) {
                return;
            }
        }
    }

    grupari++;
}

void backtrack(int p, int m) {
    if(p-1 == n) {
        prelucrare(m);
        return;
    }
    for(int i = 1; i <= m; i++) {
        sol[p] = i;
        backtrack(p+1, m);
    }
    sol[p] = m+1;
    backtrack(p+1, m+1);
}

int main() {
    in>>n>>ws;

    for(int i = 1; i <= n; i++)
        in.getline(1+mat[i], 15);

    backtrack(1, 0);
    out<<grupari-1;

    return 0;
}