static void fillSegment(int segmentId, Hobbit hobbit, List <Hobbit> segment) { if (hobbit.SegmentId != 0) { return; } hobbit.SegmentId = segmentId; segment.Add(hobbit); foreach (var h in hobbit.Heavier) { fillSegment(segmentId, h, segment); } foreach (var h in hobbit.Lighter) { fillSegment(segmentId, h, segment); } }
static void fillMatrix(Hobbit hobbit, List <Hobbit> lighter) { foreach (var l in lighter) { if (!hobbit.Lighter.Contains(l)) { hobbit.Lighter.Add(l); l.Heavier.Add(hobbit); } } lighter.Add(hobbit); var heavier = hobbit.Heavier.ToArray(); foreach (var h in heavier) { fillMatrix(h, lighter); } lighter.Remove(hobbit); }