algorithm - program to get all the combinations of ball-box application -
i new combination , permutation related algorithms. have thoughts on how program solve classical problem? there 3 boxes(a,b,c) , 10 balls(1,2,3,...,10), want put balls boxes. result should {box a: ball 1; box b: ball 2,3,4; box c: ball 5 6 7 8 9 10}, {box a: ball 1 2; box b: ball 3 4 5; box c: 7 8 9 10}, .... want combinations (not number of different combinations).
furthermore, if there constraint each box contains @ 4 balls?
thank you.
you can put first ball in of 3 boxes, have 3 variants.
there 3 variants second ball, 3 third , on.
they independent, have 3^10 variants, , each variant has 1:1 mapping number in range 0..3^10-1.
consider number in ternary number system, k-th ternary digit of number tells box (a=0,b=1,c=2) k-th ball belongs to.
example 3 balls:
number 14 = 112 ternary, first ball in c, second , third in b
for case of limited box size simple approach recursive generation - arguments of recursion list of available balls , current combination (list of boxes balls , vacant places).
Comments
Post a Comment