首页 学习日记正文

简单的胡牌算法

阿沐 学习日记 2019-09-18 570 0 C#


简单的胡牌算法:

public static bool canHu(List<int> list, int num)
        {
            List<int> pai = new List<int>(list);
            pai.Add(num);

            if (pai.Count == 2)
            {
                if (pai[0] == pai[1])
                {
                    return true;
                }
            }
            pai.Sort();

            for (int i = 0; i < pai.Count; i++)
            {
                List<int> paiList = new List<int>(pai);
                List<int> doublePai = pai.FindAll(delegate (int a) {
                    return paiList[i] == a;
                });

                if (doublePai.Count >= 2)
                {
                    paiList.Remove(pai[i]);
                    paiList.Remove(pai[i]);

                    i += doublePai.Count;
                    if (isHu(paiList))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static bool isHu(List<int> list)
        {
            if (list.Count == 0)
            {
                return true;
            }
            List<int> valueList = list.FindAll(delegate (int a) {
                return list[0] == a;
            });

            if (valueList.Count == 3)
            {
                list.Remove(list[0]);
                list.Remove(list[0]);
                list.Remove(list[0]);
                return isHu(list);
            } else
            {
                if (list.Contains(valueList[0] + 1) && list.Contains(valueList[0] + 2))
                {
                    list.Remove(valueList[0]);
                    list.Remove(valueList[0]+1);
                    list.Remove(valueList[0]+2);
                    return isHu(list);
                }
            }
            return false;
        }


参考链接:https://www.cnblogs.com/laddc/p/6646365.html

打赏

评论

Music