十三水最优组合问题快速运算求解方案

十三水共发13张牌,要求组成3道牌,首道3张,中道5张,尾道5张.

牌型:首道:单张<一对(有2张一样)<三条(3张大小一样),中道和尾道:高牌<一对<两对<三张<顺子<同花<葫芦<炸弹<同花顺(9)<五同(10)

出牌需把牌理为3道,必须尾道>=中道>=首道

 

自动摆牌方案:首先提取尾道,(剩下的牌)提取中道,(剩下的牌)首道.

提取尾道方案:先从五同开始从大到小找,找N阶(找出最大的牌型的个数)

提取中道方案:从跟首道一样大的牌型开始从大到小找,找2阶(为什么只找2阶?因为可能存在牌型一样大的解,如果牌型一样大的找不到,找牌型小一点的解,再小没必要了)

 

伪代码实现:

    public class CardModel
    {
        public List<TypeCard> TypeCardList;
        public CardModel()
        {
            TypeCardList = new List<TypeCard>();
        }
    }

    public class TypeCard
    {
        public long Weight { internal set; get; }//牌型权重比较值
        public List<int> CardList { internal set; get; }//手牌
        public bool IsFalse { internal set; get; }
    }
 public static List<CardModel> GetAllResult(List<int> cardList)
        {
            List<CardModel> models = new List<CardModel>();
            var cardTypeList = GetAllCardType(cardList, 10, 2);
            foreach (var item in cardTypeList)
            {
                List<int> newCardlist = new List<int>(cardList);
                DeleteListElement(newCardlist, item.CardList);
                var cardTypeList2 = GetAllCardType(newCardlist, (int)(item.Weight / Math.Pow(10, 10)), 2);
                foreach (var item2 in cardTypeList2)
                {
                    if (item.Weight >= item2.Weight)
                    {
                        List<int> lastCardlist = new List<int>(newCardlist);
                        DeleteListElement(lastCardlist, item2.CardList);
                        var weight = GetCardWeight(lastCardlist);
                        if (item2.Weight >= weight)
                        {
                            var model = new CardModel();
                            model.TypeCardList.Add(item);
                            model.TypeCardList.Add(item2);
                            model.TypeCardList.Add(new TypeCard() { CardList = lastCardlist, Weight = weight });
                            models.Add(model);
                        }
                    }
                }
            }
            return models;
        }

public static List<TypeCard> GetAllCardType(List<int> cardList, int level, int depth)
        {
            List<TypeCard> typeCardList = new List<TypeCard>();

            //从大到小排序
            SortCard(cardList);

int kingCount = cardList.Count(c => c == SmallKing || c == BigKing);

            

            Action<int, Func<List<TypeCard>>> func = (cur, method) =>
            {
                if (level == cur)
                {
                    if (depth > 0)
                    {
                        var list = method();
                        if (list.Count > 0)
                        {
                            typeCardList.AddRange(list);
                            depth--;
                        }
                    }
                    level--;
                }                
            };

            #region 五炸
            func(10, () => GetFiveBomb(,,,, kingCount));
            #endregion
            #region 顺子(包含同花顺)
            func(9, () => GetStraightFlush(,,,, ));
            #endregion
            #region 炸弹
            func(8, () => GetBomb( ,,,, l3));
            #endregion
            #region 葫芦
            func(7, () => GetGourd(,,,,, OneNum));

            #endregion
            #region 同花
            func(6, () => GetFlush(,,,,));

            #endregion
            func(5, () => GetStraight(,,,, ,));
            #region 三张
            func(4, () => GetThree(,,,,));
            #endregion
            #region 两对
            func(3, () => GetTwoPair( ,,,,));
            #endregion
            #region 对子
            func(2, () => GetPair( ,,,,));

            #endregion
            func(1, () => GetSingle(,,,,));
            return typeCardList;
        }

 

posted @ 2019-11-08 17:06  Joe·Zhou  阅读(2340)  评论(1编辑  收藏  举报