简体   繁体   中英

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"

M: [maths,maths,maths]
T: [pc progr,pc progr]
W: [pc progr]
T: [physics]
F: [physics]

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. 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. 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.

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. 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. As described above we will only try with days later then W ie Thursday and Friday. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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