Cod sursa(job #1857024)

Utilizator contnouAndrei Pavel contnou Data 25 ianuarie 2017 18:48:50
Problema Copii Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.24 kb
#include <fstream>
#include <string.h>
#define MAXK 13
using namespace std;

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


void readData(char **&children, int &n) {
	//
	f >> n;

	children = new char*[n + 1];
	for (int i = 0; i < n; i++) {
		children[i] = new char[n + 1];
		f >> children[i];
	}

	return;
}

bool isSolution(char **children, int *assignedGroup, int childrenCount, int groupsCount) {
	//
	int groups[MAXK][MAXK];
	int countRelations = 0;
	
	if (groupsCount < 2) return false;

	memset(groups, 0, MAXK * MAXK * sizeof(int));
	// memset(distinctGroups, 0, MAXK * sizeof(int));

	/*
	for (int i = 1; i <= groupsAssignedCount; i++) {
		groupsCount += (distinctGroups[assignedGroup[i]] == 0 ? 1 : 0);
		distinctGroups[assignedGroup[i]] = 1;
	}
	*/
	
	for (int i = 1; i <= childrenCount; i++) {
		for (int j = 1; j <= childrenCount; j++) {
			if (assignedGroup[i] != assignedGroup[j] && children[i - 1][j - 1] == '1' && countRelations < (groupsCount * groupsCount - groupsCount)) {
				//
				if (!groups[assignedGroup[i]][assignedGroup[j]]) countRelations++;
				groups[assignedGroup[i]][assignedGroup[j]] = 1;
			}
		}
	}

	return (countRelations == (groupsCount * groupsCount - groupsCount));
}

int getMax(int a, int b) {
	//
	return a > b ? a : b;
}

void combinations(char **children, int *childGroup, int pos, int len, int *prevmax, int &countTeams) {
	//
	for (int i = 0; i <= prevmax[pos - 1] + 1; i++) {
		//
		childGroup[pos] = i;
		prevmax[pos] = getMax(prevmax[pos - 1], i);

		if (pos < len) {
			combinations(children, childGroup, pos + 1, len, prevmax, countTeams);
		}
		else if (isSolution(children, childGroup, pos, getMax(prevmax[pos - 1], i) + 1)) {
			countTeams++;
		}
	}
}

int teamUp(char **children, int len) {
	//
	int childGroup[MAXK], prevmax[MAXK], countTeams = 0;
	memset(childGroup, 0, MAXK * sizeof(int));
	memset(prevmax, 0, MAXK * sizeof(int));

	childGroup[0] = -1;
	prevmax[0] = -1;

	combinations(children, childGroup, 1, len, prevmax, countTeams);

	return countTeams;
}

int main() {
	//
	int childCount;
	char **children = 0;
	int teams;

	readData(children, childCount);
	teams = teamUp(children, childCount);
	g << teams;

	return 0;
}