private static bool SearchBag(string color, IBag bag) { AllUniqueBags.ClearUsed(); Queue <IBag> queue = new Queue <IBag>(); bag.Used = true; foreach (var itemBag in bag.InsideBags) { if (!itemBag.Used) { queue.Enqueue(itemBag); } } while (queue.Count > 0) { var qB = queue.Dequeue(); if (qB.Header.Color == color) { return(true); } qB.Used = true; foreach (var itemBag in qB.InsideBags) { if (!itemBag.Used) { itemBag.Used = true; queue.Enqueue(itemBag); } } } return(false); }
private static int CountBag(IBag bag) { int countBags = 0; AllUniqueBags.ClearUsed(); var queue = new Queue <Tuple <int, IBag> >(); foreach (var itemBag in bag.InsideBags) { if (!itemBag.Used) { countBags += itemBag.Count; queue.Enqueue(new Tuple <int, IBag>(itemBag.Count, itemBag)); } } while (queue.Count > 0) { var qB = queue.Dequeue(); foreach (var itemBag in qB.Item2.InsideBags) { if (!itemBag.Used) { countBags += qB.Item1 * itemBag.Count; queue.Enqueue(new Tuple <int, IBag>(qB.Item1 * itemBag.Count, itemBag)); } } } return(countBags); }