How to solve the counting principle problem using Python, and how to solve the counting principle using python
A few days ago, I encountered such a mathematical problem:
Four different colors are used to color the six vertices of the triangular prism. Each vertex must be colored with different ends of each rib. How many different coloring methods are there?
After reading the questions, I was overwhelmed. So I picked up the draft paper and ran it without any purpose, trying to find a solution. Nothing is returned. So we plan to solve this problem through program algorithms. After more than two hours of research, I finally completed the code and obtained the answer.
Since Python is easy to write and I prefer Python syntax, I usually use Python when studying algorithms. This is no exception. The following describes the implementation process of the entire algorithm.
Two Algorithms
I have come up with two algorithms for solving this question:
Algorithm 1:Calculate all coloring conditions through the loop of the program, and then determine the conditions of the program to remove all situations that do not match the meaning of the question, and finally get the final result.
Algorithm 2:Starting from any of the endpoints (p0), since all other endpoints are not painted, it can be painted in four colors. Apply the four colors to the endpoint through the cycle. After each color is applied, obtain an endpoint (p1) next to it ), and obtain the color that can be coated, and then apply the available color through the cycle (and cannot be coated with the same color as p0), each coated with a color, the adjacent unpainted points of p1 are painted (and adjacent endpoints except p0 ). After each vertex is painted, the adjacent vertex is painted in the same way, and so on. After the last vertex is painted, the situation is recorded. After all recursion is completed, all cases are obtained.
Algorithm 1 is very straightforward, so I used algorithm 2 to solve the above problem. The following is the specific code.
Algorithm Implementation
I wrote about 90 lines of Python code to implement this algorithm:
colorList = [0, 1, 2, 3]pointList = []amount = 0class Point(object): def __init__(self): super(Point, self).__init__() self.neibors = [] self.color = None def paint(self, c): self.color = c def clean(self): self.color = None def getLeftOverColors(self): copyOfColorList = colorList[0 : 4] for neibor in self.neibors: nc = neibor.color if nc in copyOfColorList: copyOfColorList.remove(nc) return copyOfColorListdef main(): global pointList p0 = Point() p1 = Point() p2 = Point() p3 = Point() p4 = Point() p5 = Point() p0.neibors = [p1, p2, p4] p1.neibors = [p0, p2, p5] p2.neibors = [p0, p1, p3] p3.neibors = [p2, p4, p5] p4.neibors = [p0, p3, p5] p5.neibors = [p4, p3, p1] pointList = [p0, p1, p2, p3, p4, p5] paintPoint(p0) print(amount)def paintPoint(p): global amount colors = p.getLeftOverColors() lastOne = isLastOne() for c in colors: p.paint(c) if lastOne: amount += 1 else: for currentNeibor in p.neibors: if currentNeibor.color == None: paintPoint(currentNeibor) break p.clean()def isLastOne(): global pointList paintedNum = 0 for p in pointList: if p.color != None: paintedNum += 1 return paintedNum == 5if __name__ == "__main__": main()
The following describes the code sections.
Global Variables
ColorList:Color list
PointList:List of six points
Amount:Number of coloring schemes
Point class
Stores information about each vertex, such as the color (None indicates no color) and adjacent vertex ('neibors ). And providePaintThe method is used to mark the dot color;CleanThis method is used to remove colors;GetLeftOverColorsThis method is used to obtain available colors and colors that are not used by neighboring points.
Main Function
The function called when the program starts running. The six points are constructed and the adjacent three points are identified respectively. Note: Because the points here only have the relationship between adjacent and non-adjacent positions, you do not need to care which position these points correspond to in the Three-prism, any setting of the positions of these points does not affect the result. You only need to pay attention to the adjacent relationship between them.
IsLastOne Functions
Determine if it is the last unpainted point.
PaintPoint Function
Used to color vertices passed in as parameters. First, callGetLeftOverColorsMethod to obtain the available color, and then follow the algorithm described above, by traversing the available color list, color the point, if the point is not the last point (throughIsLastOneFunction Judgment), it is called recursivelyPaintPointThe function is used to color adjacent uncolored points. If yes, a coloring scheme is recorded.
Run the code. Expected result-264 is displayed:
OK, so this question was solved with the help of our computer ~ If you have a better solution to this algorithm problem, please leave a message to us ~ I hope this article will help you learn Python and counting principles.