Pagini recente » Cod sursa (job #266784) | Profil dornescuvlad | Profil andrici_cezar | Profil andrici_cezar | Cod sursa (job #1856768)
#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 groupsAssignedCount, int childrenCount) {
//
int groups[MAXK][MAXK], distinctGroups[MAXK];
int groupsCount = 0, countRelations = 0;
if (groupsAssignedCount != childrenCount) {
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;
}
if (groupsCount < 2) return false;
return false;
for (int i = 1; i <= childrenCount; i++) {
for (int j = 1; j <= childrenCount; j++) {
if (i != j && children[i - 1][j - 1] == '1' && assignedGroup[i] != assignedGroup[j]) {
//
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, len)) {
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;
}