Algorithm to divide a chocolate bar in equal parts

后端 未结 3 714
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-03 10:14

A random thought popped into my head (when I was sharing a chocolate bar of course!). I was wondering if there is a generic algorithm to solve this problem.

The problem

3条回答
  •  醉话见心
    2021-02-03 11:11

    A good way to answer this question would be to use a breadth-first search algorithm. The algorithm would try every possible break of the whole chocolate bar. Then for each of those possible states of the problem, try all possible breaks, and this would continue while keeping track of the evenness of the pieces.

    I'd like to add that the rules would enforce which breaks of the chocolate bar are legal and those possible states which are not legal are thrown out from the algorithm.

提交回复
热议问题