1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
|
using System;
using System.Linq;
using System.Collections.Generic;
class Infoboard
{
public static void Main (string[] args)
{
int bunnies = 12; //8
int races = 3; //2
int participants = bunnies / races;
List<Race> raceList = Race.CreateRaces(participants, bunnies);
List<Day> days = Day.CreateMatchdays (raceList, races);
List<Tournament> tournaments = Tournament.CreateTournament (days, 495 / races); //495 = binom(12, 4)
}
class Race {
public int[] Participants { get; private set; }
public Race(int[] participants) {
this.Participants = (int[]) participants.Clone();
}
public bool Collide(Race r) {
foreach (int p in this.Participants) {
foreach (int q in r.Participants) {
if (p == q)
return true;
}
}
return false;
}
public static List<Race> CreateRaces(int participants, int bunnies) {
List<Race> matches = new List<Race> ();
CreateRaces (matches, new int[participants], 0, 0, bunnies);
return matches;
}
private static void CreateRaces(List<Race> matches, int[] current, int index, int taken, int max) {
if (taken == current.Length) {
matches.Add (new Race (current));
return;
}
if (index >= max)
return;
CreateRaces (matches, current, index + 1, taken, max);
current [taken] = index;
CreateRaces (matches, current, index + 1, taken + 1, max);
}
public override string ToString ()
{
return string.Join (" ", Participants.Select (p => p + 1));
}
}
class Day {
public Race[] Races { get; private set; }
public Day(Race[] races) {
this.Races = (Race[]) races.Clone();
}
public static List<Day> CreateMatchdays(List<Race> raceList, int racesPerDay) {
List<Day> days = new List<Day>();
CreateMatchdays (days, raceList, new Race[racesPerDay], 0, 0);
return days;
}
private static void CreateMatchdays(List<Day> days, List<Race> matches, Race[] current, int index, int taken) {
if (taken == current.Length) {
days.Add (new Day (current));
return;
}
if (index >= matches.Count)
return;
CreateMatchdays (days, matches, current, index + 1, taken);
if (!current.Take (taken).Any (c => c.Collide (matches [index]))) {
current [taken] = matches [index];
CreateMatchdays (days, matches, current, index + 1, taken + 1);
}
}
public bool Collide(Day d) {
foreach (Race p in this.Races) {
foreach (Race q in d.Races) {
if (p == q)
return true;
}
}
return false;
}
public override string ToString ()
{
return string.Join<Race> (" ", Races);
}
}
class Tournament {
public Day[] Days { get; private set; }
public Tournament(Day[] days) {
this.Days = (Day[]) days.Clone();
}
public static List<Tournament> CreateTournament(List<Day> dayList, int daysPerTournament) {
List<Tournament> tournaments = new List<Tournament>();
CreateTournament (tournaments, dayList, new Day[daysPerTournament], 0, 0);
return tournaments;
}
private static void CreateTournament(List<Tournament> tournaments, List<Day> days, Day[] current, int index, int taken) {
if (taken == current.Length) {
tournaments.Add (new Tournament (current));
Console.WriteLine (tournaments.Last () + "\n\n");
return;
}
if (index >= days.Count)
return;
if (current.Length - taken > days.Count - index)
return;
CreateTournament (tournaments, days, current, index + 1, taken);
if (!current.Take (taken).Any (c => c.Collide (days [index]))) {
current [taken] = days [index];
CreateTournament (tournaments, days, current, index + 1, taken + 1);
}
}
public override string ToString ()
{
return string.Join<Day> (Environment.NewLine, Days);
}
}
} |