Python uses a subset tree template based on the Backtracking Method to Solve the Problem of savage and missionary issues.
This article describes how Python solves the problem of savage and missionary issues 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
On the left bank of the river, there were N missionaries, N wild men and a ship. The missionaries wanted to use this ship to cross the river, but there were the following restrictions:
(1) The monks and the savage row, but the ship can only ship up to M people at a time;
(2) On any shore or ship, no more than monks are allowed. Otherwise, the monks will be eaten by the monks.
Assuming that the savage will obey any kind of cross-river arrangement, plan a plan to ensure that the monk can cross the river safely.
Analysis
Baidu, the Internet is all in the status of a triple such as the number of missionaries, the number of wild people, and the location of the ship on the Left Bank.
I changed my mind to consider only the status of the ship.
Status of the ship: (x, y) x represents the ship's x missionaries, and y represents the ship's y wild men, of which | x | records [0, m], | y | records [0, m], 0 <| x | + | y | <= m, x * y> = 0, | x |> = | y |
When the ship is left to right, the x and y values are not negative. When the ship is from right to left, x and y take non-positive numbers.
Encoding: [(x0, y0), (x1, y1 ),..., (xp, yp)] x0 + x1 +... + xp = N, y0 + y1 +... + yp = N
The length of the solution is not fixed, but it must be an odd number.
At the beginning, the Left Bank (N, N) and Right Bank (0, 0 ). Final Left Bank (0, 0), Right Bank (N, N)
Because the valid status of a ship is dynamic and two-dimensional. Therefore, use a functionget_states()
To generate its state space to make the winner program clearer.
Code
N = 3 # n missionaries and n savage m = 2 # ships can carry m people x = [] # A solution, it is a series of States of a ship X = [] # A group of solutions is_found = False # global termination signs # Calculate the valid state space of the ship (two-dimensional) def get_states (k ): # The ship is about to run the k-trip global n, m, x if k % 2 = 0: # from left to right, only the number of people on the original left bank is considered as s1, s2 = n-sum (s [0] for s in x), n-sum (s [1] for s in x) else: # from right to left, only the number of people on the original right bank is considered (the Historical Status of the ship can be accumulated !!!) S1, s2 = sum (s [0] for s in x), sum (s [1] for s in x) for I in range (s1 + 1 ): for j in range (s2 + 1): if 0 <I + j <= m and (I * j = 0 or I> = j): yield [(-I, -j), (I, j)] [k % 2 = 0] # generate the valid State of the ship # conflict Detection def conflict (k ): # The ship starts to run the k-trip global n, m, x # if the ship was uploaded to the same person as the previous one (it would be in an endless loop !!!!) If k> 0 and x [-1] [0] =-x [-2] [0] and x [-1] [1] =-x [-2] [1]: return True # at any time, the number of missionary people on the ship is less than the number of wild people, no one, or overload (the legitimate State Space of the ship has been taken into account .) # If 0 <abs (x [-1] [0]) <abs (x [-1] [1]) or x [-1] = (0, 0) or abs (sum (x [-1])> m: # return True # at any time, on the left bank, the number of missionaries is less than that of the savage if 0 <n-sum (s [0] for s in x) <n-sum (s [1] for s in x ): return True # at any time, the number of evangelists on the right bank is less than if 0 (s [0] for s in x) <sum (s [1] for s in x ): return True return False # No conflict # Backtracking Method def backtrack (k): # the ship is about to run the k global n, m, x, is_found if is_found: return # terminate all recursion if n-sum (s [0] for s in x) = 0 and n-sum (s [1] for s in x) = 0: #0 print (x) is_found = True else: for state in get_states (k): # traverse the valid status space of the Ship x. append (state) if not conflict (k): backtrack (k + 1) # depth first x. pop () # backtracking # test backtrack (0)
Explanation:
Conclusion
Looks likeOnly meetm = n-1
To solve this problem..