Shuffle list with some conditions

后端 未结 4 1243
余生分开走
余生分开走 2021-01-04 22:51

I have a list of elements which can be easily compared using Equals(). I have to shuffle the list, but the shuffle must satisfy one condition:

The i\'th

4条回答
  •  佛祖请我去吃肉
    2021-01-04 23:31

    • permute valid 5 in list, if none unsolvable
    • remove permutation from list
    • create a cycle graph of that 5
    • order the list(remaining list) by count of valid positions in new graph (if you dont do this you can end up with a wrong non solvable, because putting items that can go on more positions increases the possible position count of items with less)
    • continue picking items in the list, add items to cycle graph at valid positions
    • if there is no valid position in graph or graph cannot be created continue on next
    • if graph created revert back to start iteration where graph has been created
    • continue creating other graphs
    • save all full graphs in a list
    • pick a random from that list
    • if list is empty unsolvable

提交回复
热议问题