Python uses the backtracking algorithm subset tree template to solve the problem of finding zero. python returns zero.

Source: Internet
Author: User

Python uses the backtracking algorithm subset tree template to solve the problem of finding zero. python returns zero.

The example in this article describes how Python solves the problem of finding zero Based on the subset tree template of the Backtracking Method. We will share this with you for your reference. The details are as follows:

Problem

There are 10 yuan, 5 yuan, 2 yuan, 1 Yuan coins, the number is 3, 5, 7, 12 respectively. Now we need to change the number of coins for the customer to 16 RMB. How can we change the number of coins? Or the problem cannot be solved.

Analysis

Element-State Space Analysis Method: four denominations of coins are regarded as four elements, the corresponding number is regarded as their respective state space, traversing the state space, and other things are handed over to the pruning function.

Fixed solution length: 4

Decoding Code: (x1, x2, x3, x4) x1, [0, 1, 2, 3], x2, [0, 1, 2, 3, 4, 5], x3, [0, 1, 4, 4, 2 ,..., 7], x4, [0, 1, 2 ,..., 12]

Find the optimal solution and add the global variables best_x and best_num.

Apply the Backtracking Method to the subset tree template.

Code

'''Zero-problem ''' n = 4a = [10, 5, 2, 1] # four denominations B = [3, 5, 7, 12] # number of coins (status space) m = 53 # given amount x = [0] * n # A solution (n yuan 0-b [k] array) X = [] # best_x = [] # best_num = 0 # minimum number of coins # conflict Detection def conflict (k): global n, m, x, X, X, the amount of a, B, best_num # decomposition has exceeded if sum ([p * q for p, q in zip (a [: k + 1], x [: k + 1])> m: return True # The amount of partial decomposition plus the remaining amount is not enough if sum ([p * q for p, q in zip ([: k + 1], x [: k + 1]) + sum ([p * q for p, q in zip (a [k + 1:], B [k + 1:]) <m: return True # the number of coins decomposed exceeds best_num = sum (x [: k + 1]) if 0 <best_num <num: return True return False # No conflict # backtracking (recursive version) def subsets (k): # Reach the k-th element global n, a, B, x, X, best_x, best_num if k = n: # elements beyond the end # print (x) X. append (x [:]) # Save (one solution) # calculate the number of coins. if the number is the best, save num = sum (x) if best_num = 0 or best_num> num: best_num = num best_x = x [:] else: for I in range (B [k] + 1): # traverse the available status of element a [k]: 0, 1, 2 ,..., B [k] coins x [k] = I if not conflict (k): # Cut subsets (k + 1) # test subsets (0) print (best_x)

Related Article

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.