简体   繁体   English

回溯python或c ++

[英]backtracking python or c++

The problem is: 问题是:

Output all the possibilities to mix 3 classes of maths, 3 classes of pc programming, 2 classes of physics in a week of 5 days considering that in a day of week a student has at least one of these classes and the maximum of 3, no matter how they are mixed" 考虑到在一周中的某一天学生至少有其中一门课程,而最多有三门课程,则输出在5天的一周内混合3门数学课程,3门pc编程课程,2门物理课程的所有可能性不管它们如何混合”

M: [maths,maths,maths] M:[数学,数学,数学]
T: [pc progr,pc progr] T:[个人电脑程序,个人电脑程序]
W: [pc progr] W:[个人电脑程序]
T: [physics] T:[物理学]
F: [physics] F:[物理学]

I have an idea in Python to create a list of 15 elements which will contain all the classes from a week 15/5 = 3 (the maximum classes a student can have) so for the example above the list will look like [m,m,m,cs,cs,0,cs,0,0,p,0,0,p,0,0] but I don't really know how to generate all the lists using backtracking. 我在Python中有一个想法,创建一个包含15个元素的列表,其中将包含一周15/5 = 3(一个学生可以拥有的最大课程)中的所有课程,因此对于上面的示例,该列表看起来像[m,m,m,cs,cs,0,cs,0,0,p,0,0,p,0,0]但我真的不知道如何使用回溯来生成所有列表。 Can you give me any ideas? 你能给我任何想法吗?

First try to solve a simpler problem. 首先尝试解决一个更简单的问题。 Try output all the possibilites to mix the classes having a two more constraints: 尝试输出所有可能的结果以混合具有两个以上约束的类:

  1. No class can appear more then once in a day 每天一次班级上课人数最多

  2. The order of the classes in a day does not matter 一天的课程顺序无关紧要

As usual with backtrack problems it is easiest to solve them with recursion. 与回溯问题一样,最简单的方法是递归解决。 Do the following: take the sequence of the classes in any order cs,cs,cs,m,m,m,p,p and on each step of the recursion assign the next class to a "possible" day. 请执行以下操作:以cs,cs,cs,m,m,m,p,p的任何顺序获取类的顺序,并在递归的每个步骤中将下一个类分配给“可能”的一天。 Here a day is possible if it has less then 3 classes assigned so far and it does not yet have a class of the same type yet. 如果一天到目前为止分配的班级少于3个,并且还没有同一类型的班级,则一天是可能的。

To avoid repetition in the possibilities add one more requirement - if on the current step of the recursion we need to assign class A to some day and class A has already been added to some of the days, then only try to assign A to days later in the week then the last one it has been assigned to. 为了避免重复,添加了另一个要求-如果在递归的当前步骤中,我们需要将A类分配给某天,并且已经将A类添加到某些天,则仅尝试在几天后分配A在一周中,最后一个已分配给它。 As this may sound a bit confusing let me give an example: 听起来有些混乱,让我举个例子:

Say we have the current state: 说我们有当前状态:

M: m, cs
T: 0
W: cs
T: p
F: p

and on the next step we have to add a cs class. 下一步,我们必须添加一个CS类。 As described above we will only try with days later then W ie Thursday and Friday. 如上所述,我们只会在W之后的几天(即周四和周五)尝试。 It requires some consideration but you should be able to figure out that if we do not add this restriction some possibilities will be found more then once. 它需要一些考虑,但是您应该能够弄清楚,如果我们不添加此限制,那么将发现更多的可能性,而一旦发现。

The end of the recursion is met in two cases: 递归的结束在两种情况下得到满足:

  • if all the classes have been assigned to some day we have found a possible assignement and we output it 如果某天所有班级都被分配了,我们找到了可能的分配,然后将其输出

  • if on the next step we need to assign class A to some day, but A has already been assigned to some day of the week say X and all days after X have 3 classes already assigned to them, then we can not find a possible day for A and therefor no possible solution can be found in this branch. 如果在下一步中,我们需要将某类分配给某天,但是已经将A类分配给了某周,例如X,并且X之后的所有天数已经分配了3个类,那么我们找不到可能的一天对于A,因此在此分支中找不到可能的解决方案。

Now having solved this a bit easier problem, try removing the two additional constraints I added one by one. 现在已经解决了这个简单的问题,请尝试删除我逐个添加的两个其他约束。 First modify the solution in a way that a class may appear more then once in a day(although it seems that the problem is simpler in this case additional work is needed to avoid counting some possibilities more then once). 首先以某种方式修改解决方案,使一个班级每天出现的次数多于一次(尽管在这种情况下问题似乎更简单),但需要额外的工作来避免多次计算某些可能性。 After this remove the last remaining contraint - make the order of the classes in a day matter. 在此之后,删除最后剩下的约束-在一天中确定课程的顺序。 There is a simple approach to that but I am not sure if it is permitted as you state the problem - after finding a solution do all the permutations on all the days in it to generate all the possible arrangements. 有一个简单的方法,但是在您陈述问题时我不确定是否允许-找到解决方案后,在其中进行所有工作,以产生所有可能的安排。

I really hope this answer helps. 我真的希望这个答案会有所帮助。 I can write down the solutions of all the problems I stated above, but I prefer to try to give you tips on how to approach them on your own so that you can actually do them yourself. 我可以写下我上面提到的所有问题的解决方案,但我更愿意尝试向您提供有关如何自行解决这些问题的技巧,以便您实际可以自己解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM