Python learning path-DP algorithm first test, python path dp Algorithm

Source: Internet
Author: User

Python learning path-DP algorithm first test, python path dp Algorithm
This question is as follows:

Suppose there is a 6*6 chessboard with a prize in each lattice (the value of each prize is between 100 and 1000). Now it is required to end from the upper left corner to the lower right corner, you can only go to the right or down one grid at a time. The prize in the grid passes by belongs to you. Ask how many prizes can be collected.

           
           
           
           
           
           

When we first saw this problem, many brain holes immediately appeared in our minds: brute force, binary tree, and weighted path in the diagram. However, I did not come up with a reason. I accidentally saw the section about the DP algorithm (Dynamic Planning) in an algorithm book. I think it seems I can use it here.

The DP algorithm is applicable to the problems that affect the decision-making of the previous step. The decision of the blue square in this question depends on the optimal decision on the left and above. Therefore, for the blue part a [I] [j], you only need to take max {a [I-1] [j], a [I] [J-1]} + a [I] [j]; for the white part, it is only affected by the decision on the left or above, therefore, for horizontal a [I] [j], a [I] [J-1] + a [I] [j], for vertical a [I] [j], a [I-1] [j] + a [I] [j] should be taken. In fact, there are still many problems that DP can solve. I believe this is also a particularly useful algorithm and it is strongly recommended to read and learn.

 

Tips: The Code involves the initialization and assignment of two-dimensional arrays in python. You can use list derivation to initialize them.

Code:

1 import random 2 count = 0 3 a = [[0 for I in range (6)] for I in range (6)] 4 print ("randomly generate a 6*6 two-dimensional array as the weight in the Board:") 5 for I in range (6): 6 for j in range (6 ): 7 a [I] [j] = random. randint (100,100 0) 8 print ('% 2d' % a [I] [j], end = "") 9 count + = 110 if count % 6 = 0: 11 print ("\ n") 12 for I in range ): 13 a [0] [I] = a [0] [I-1] + a [0] [I] 14 a [I] [0] = a [I-1] [0] + a [I] [0] 15 for I in range (1, 6): 16 for j in range (): 17 a [I] [j] = max (a [I-1] [j], a [I] [J-1]) + a [I] [j] 18 print ("changed array:") 19 for I in range (6): 20 for j in range (6 ): 21 print ('% 2d' % a [I] [j], end = "") 22 count + = 123 if count % 6 = 0: 24 print ("\ n ") 25 print ("the final maximum value is % d" % a [5] [5])

Running result:

 

 

Of course, before I did it myself, I had to ask our du Niang about the brute-force solution. Instead, I had to use recursion to solve the problem. It was too violent to burn my brains, I haven't figured it out for a long time. Here I will rewrite the original c ++ version code into the python code for everyone to learn and think about.

# Import randomglobal counttotalprice = 0tmp = 0 count = 0a = [[0 for I in range (6)] for I in range (6)] B = [[0 for I in range (6)] for I in range (6)] next = [[1, 0], [0, 1] def find (x, y ): global totalprice global tmp totalprice + = a [x] [y] if x = 5 and y = 5: if totalprice> tmp: tmp = totalprice return for k in range (2 ): tx = x + next [k] [0] ty = y + next [k] [1] if tx <0 or tx> 5 or ty <0 or ty> 5: continue if B [tx] [ty] = 0: B [tx] [ty] = 1 find (tx, ty) totalprice-= a [tx] [ty] B [tx] [ty] = 0 returnfor I in range (6): for j in range (6 ): a [I] [j] = random. randint (1,100) print ('% 2d' % a [I] [j], end = "") count + = 1 if count % 6 = 0: print ("\ n") B [0] [0] = 1 find (0, 0) print (tmp)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.