During the past few days of the National Day, in the python technology exchange group, on October 11, October 1, I found that everyone was discussing a hacker.org online robot escape game and started a few tests, I understood the playing principle, and played 26 points manually. I didn't plan to use programming to solve this game, because I didn't even think of and noticed that the flash game on this web page can be programmed to solve the problem. After listening to the experts in the group, I started to use python to program and solve the problem.
At first, I came up with a method of exhaustion, because I had no experience in game programming. But this is definitely not feasible. Then I searched for keywords such as paths in the game on the Internet, and found the * algorithm and the materials:
I see the information is mainly: JS + Ajax IMPLEMENTATION OF A * game Path Algorithm finishing page 1/2 http://www.jb51.net/article/10130.htm
JS + Ajax implementation of a * game Path Algorithm arrangement page 2/2 http://www.jb51.net/article/10130_2.htm
Python astar A * a star http://www.cnblogs.com/kdy71107216/archive/2011/08/06/2129139.html
Use the astar (A *) algorithm to achieve the Shortest Path of the maze. Code comparison specification http://www.cnblogs.com/kdy71107216/archive/2010/08/06/1794447.html
-----
There are also a lot of basic information and examples about the list tuple operation.
Use the astar (A *) algorithm to achieve the Shortest Path of the maze. Code comparison specification http://www.cnblogs.com/kdy71107216/archive/2010/08/06/1794447.html
This is the astar algorithm of Java version. You can check it in Java.
My program is implemented in Python, which is based on the above JS + Ajax version.
The entire process should be divided into three phases:
1. Online Data Query, reading and digesting data mainly involves understanding the astar algorithm and adapting the JS program to the Python version. In this case, we mainly solve the classic example introduced in the astar algorithm.
2. Start to use the compiled Python program to solve the first several rounds of games. The map doesn't need to be big. Use a 6x6 program to draw a picture with a pen or paper and deduct it from the program, you can print running information using programs for debugging and troubleshooting. You can also better understand the astar algorithm. Until the problem of several small maps is solved and the path is obtained. My initial path was a list of coordinate values, and later I thought of another way to convert it into an escape Command similar to a rdrdrd string. Then I checked how to download and save the webpage using python, analyzed the parameters and other information in the webpage link, and finally started to pass the driver.
3. In the process of running the program, you need to tune the program. At the beginning, you are not aware of it, so you can leave the computer busy and wait for the result. Later, I realized that speed is very important. Otherwise, I would like to wait for the year or month.
At the beginning, it was easy to go to more than 260 levels, but it was still very fast before the 200 level, but it took more and more minutes to pass the customs, I thought it was almost the same time for others, so I let the computer run there for one night until dawn, and the program had long run and an error stopped there. The specific error is what webpage download error, no service, etc. In the morning, I asked Jack again. He said that I was too slow, and I changed it a lot, reducing many loop processes. At last, it only took more than one minute to start from the 295 off, but then the map gradually grew bigger. when it reached the 460 off, it ran for more than 10 minutes and then changed the program, it takes 10 seconds from 460 to 500. It takes only a few minutes to close the last-, and then 10 minutes.
-----------------------------
Several bottleneck functions in Python.
The following seq refers to sequences such as list and tuple.
1. Seq. Count () function
In practice, the count function takes a lot of time, especially when seq is large. If the count function is used in the loop, your loop will take a lot of time.
For example, remove a small part of the function that contains repeated elements in the list:
# Clear repeated bombs and reduce Loops
# Keep sorting
Nodupes = []
[Nodupes. append (I) For I in seq if not nodupes. Count (I)]
In this short section, the total time required for the subsequent 400-plus off operations can reach more than 250 seconds. Later, I didn't need the count function, and the whole program running time was immediately lowered.
2 Some in seq, this in judgment will also take a lot of time, especially when seq is relatively large, I do not know the specific reason. You can use the for loop. After finding some in seq, you can use break to interrupt the for loop. This some in seq, when solving this game, in my seq, there is a large string of tuple (), that is, the coordinates are represented by (x, y. The result takes a long time.
-------------------
Materials learned this time include:
1. Search for paths using the astar Algorithm
2. List and tuple operation examples
3. Functions for downloading web pages
4. simply use the python profile to check which functions consume the most time. This helps me tune the most.
--------------------
Current understanding of the astar algorithm:
1. Calculate the F value of each node. The F value is G + H.
2. one of the main tasks in the algorithm is to maintain two lists. One is to enable the list openlist, which stores nodes that have not been tested, and the other is to close the list, it stores vertices that have been tested.
3. The final path can be obtained through the reverse end point to the start point.
4. the start condition can be classified into two important parameters. One is the MAP range, and the simplified point is the rectangle range (after simplification, the start and end points are changed to the two corners of the rectangle ), one is a list of bombs or obstacles. With these two parameters, any other path search problems can be solved by converting them to the astar algorithm.
-------------
Experience:
1. Solve the problem to zero, and convert unfamiliar problems into familiar problems.
2. When you encounter problems, keep learning and try to solve the problems.
3. Absorb others' ideas, but cultivate the ability to solve problems independently based on your own code and practice.
-----
2011-10-6
In general, this game will be an unforgettable programming experience for many people. I found several people in the group have passed. When the mountains stand up and the scenery stops, I suddenly feel like walking with experts. In the boundless online world, this feeling makes people feel that you are not fighting alone, but you are not alone. I hope to share my hopes with you.