代码之家  ›  专栏  ›  技术社区  ›  Luiscencio

如何从列表中选择不同的列表?

  •  0
  • Luiscencio  · 技术社区  · 14 年前

    嗨,我正在编写一个简单的类来组合任何类型的项…这是一个扑克游戏,它看起来是这样的:

    public static List<List<T>> combinar<T>(List<T> items, int take)
    {
        List<List<T>> combs = new List<List<T>>();
        var stuff = permutar<T>(items, take);
    
        var all = from s in stuff
                    select new Tuple<List<T>, string>(s, String.Join("", s.OrderBy(c => c).Select(c => c.ToString())));
        var strs = all.Select(s => s.Item2).Distinct();
        foreach (var str in strs)
        {
            combs.Add(all.First(a => a.Item2 == str).Item1);
        }
        return combs;
    }
    public static List<List<T>> permutar<T>(List<T> list, int take)
    {
        List<List<T>> combs = new List<List<T>>();
        foreach (var item in list)
        {
            var newlist = list.Where(i => !i.Equals(item)).ToList();
            var returnlist = take <= 1 ? new List<List<T>> { new List<T>() } : permutar(newlist, take - 1);
            foreach (var l in returnlist)
            {
                l.Add(item);
            }
            combs.AddRange(returnlist);
        }
    
        return combs;
    }
    

    所以排列是完美的。但我在组合方面遇到了一些困难,当T是一张牌时,要花很多时间才能完成…所以我的问题是如何从排列的结果中选择不同的列表????

    这是卡类:

    public class Card : IComparable
    {
        Suite _Suite;
        public Suite Suite
        {
            get { return _Suite; }
            set { _Suite = value; }
        }
        Grade _Grade;
        public Grade Grade
        {
            get { return _Grade; }
            set { _Grade = value; }
        }
        string _symbol;
        public string Symbol
        {
           //stuff
        }
        public PictureBox Picture
        {
            //stuff
        }
        public override string ToString()
        {
            return _Grade.ToString() + " " + _Suite.ToString();
        }
        public int CompareTo(object obj)
        {
            Card card = (Card)obj;
            return card.Grade > this.Grade ? -1 : card.Grade < this.Grade ? 1 : 0;
        }
    }
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   mqp    14 年前

    假设你不想做任何大的算法改变,你最大的问题是

    combs.Add(all.First().Item1);
    

    这没有任何意义。也许你的意思是

    combs.Add(all.First(c => c.Item2 == str)).Item1);
    

    然而,这将是非常缓慢的;如果这是你想要的,你应该把结果 all 进入一个由字符串键控的哈希表,并使用它而不是循环通过 Distinct 结果。

    如果你想在不首先计算排列的情况下得到组合,那么方法应该是这样的。给定一些对象,要查找长度k的组合:如果k为0,则返回空列表。否则,对于每个对象,获取该对象,然后递归地附加其余对象的所有k-minus-1长度组合。