Permutation algorithms in C#

前端 未结 4 1232
我在风中等你
我在风中等你 2021-01-13 13:02

I\'m struggling with this algorithm I need to write. I\'m using C#.

Say I have a List and I have a List. I need to

4条回答
  •  独厮守ぢ
    2021-01-13 13:25

    If you allow dupes [a lunch can be in two bags] - as the example suggests you have #bags^#lunches possibilities.

    Each bag has its own unique "choice" which lunch to put
    To genereate these possibilities - just "choose" a lunch for a bag, and recursively invoke the algorithm. repeat for each lunch.

    pseudo code for generating them:

    generateAll(bags,lunches,sol):
      if (bags is empty):
          print sol
          return
      bag <- bags.first
      bags.remove(bag)
      for each lunch in lunches:
         sol.append(BagLunch(bag,lunch)
         generateAll(bags,lunches,sol)
         sol.removeLast()
    

提交回复
热议问题