Pagini recente » Cod sursa (job #2750102) | Cod sursa (job #3129073) | Cod sursa (job #2376317) | Cod sursa (job #3201902) | Cod sursa (job #1856542)
#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];
int distinctGroups[MAXK];
int groupsCount = 0;
int 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;
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;
}
bool isValid(int *childGroup, int curr) {
//
int freq[MAXK], max = -1;
memset(freq, 0, MAXK * sizeof(int));
for (int i = 1; i <= curr; i++) {
if (!freq[childGroup[i]]) {
if (max != childGroup[i] - 1) return false;
freq[childGroup[i]] = 1;
max = childGroup[i];
}
}
return true;
}
void combinations(char **children, int *childGroup, int curr, int len, int &countTeams) {
//
for (int i = 0; i <= childGroup[curr - 1] + 1; i++) {
//
childGroup[curr] = i;
if (curr < len) {
combinations(children, childGroup, curr + 1, len, countTeams);
} else if (isSolution(children, childGroup, curr, len)) {
countTeams++;
}
}
}
int teamUp(char **children, int len) {
//
int childGroup[MAXK], countTeams = 0;
memset(childGroup, 0, MAXK * sizeof(int));
childGroup[0] = -1;
combinations(children, childGroup, 1, len, countTeams);
return countTeams;
}
int main() {
//
int n;
char **children = 0;
int teams;
readData(children, n);
teams = teamUp(children, n);
g << teams;
return 0;
}