/// <summary> /// 组合 /// </summary> /// <param name="curItem">当前项</param> /// <param name="itemIndex">项索引</param> private void ToCombination() { while (_stk.Count > 0) { CombinationItem <T> item = _stk.Pop(); ICombinationState curSender = item.Sender; if (curSender.DoBuy(item.Item)) { item.TotlePoint += item.Item.GetPoint(); if (curSender.LeftMoney == 0) { _queItems.Enqueue(item); } else { for (int i = item.ItemIndex; i < _lstItems.Count; i++) { ICombinationState newSender = curSender.CopyCombination(); CombinationItem <T> newitem = new CombinationItem <T>(_lstItems[i], i, newSender, item); _stk.Push(newitem); } } } } }
/// <summary> /// 组合项 /// </summary> /// <param name="previous"></param> public CombinationItem(T item, int itemIndex, ICombinationState sender, CombinationItem <T> previous) { _item = item; _previous = previous; _sender = sender; _itemIndex = itemIndex; if (previous != null) { TotlePoint = previous.TotlePoint; } }
/// <summary> /// 返回组合合 /// </summary> /// <param name="item"></param> /// <returns></returns> public List <T> ToList() { List <T> lst = new List <T>(); CombinationItem <T> item = this; do { lst.Add(item.Item); item = item.Previous; } while (item != null); return(lst); }
/// <summary> /// 开始组合 /// </summary> private void DoCombination(ICombinationState sender) { _queItems = new Queue <CombinationItem <T> >(); _stk = new Stack <CombinationItem <T> >(); for (int i = 0; i < _lstItems.Count; i++) { ICombinationState curSender = sender.CopyCombination(); CombinationItem <T> item = new CombinationItem <T>(_lstItems[i], i, curSender, null); _stk.Push(item); } ToCombination(); }
public CombinationItem <T> GetMaxPoint(ICombinationState sender) { DoCombination(sender); if (_queItems.Count == 0) { return(null); } CombinationItem <T> maxPoint = _queItems.Peek(); foreach (CombinationItem <T> obj in _queItems) { if (obj.TotlePoint > maxPoint.TotlePoint) { maxPoint = obj; } } return(maxPoint); }