# -*- coding: cp936 -*-
#Define the allowed state set
def allowexistset():
merchants=int(raw_input("Please enter the number of merchants: "))
slivers=int(raw_input("Please enter the number of followers: "))
allowset=[]
for i in range(merchants+1):
for j in range(slivers+1):
#The number of merchants is 0
if i==0:
allowset.append([i,j])
#All businessmen together
elif i==merchants:
allowset.append([i,j])
#The number of merchants on one bank is greater than or equal to the number of followers, and the number of merchants on the other bank is greater than or equal to the number of followers
elif i>=j and ((merchants-i)>=(slivers-j)):
allowset.append([i,j])
#print allowset
return allowset,merchants,slivers
"""
Run and return results:
>>> allowexistset()
Please enter the number of merchants: 3
Please enter the number of followers: 3
[[0, 0], [0, 1], [0, 2], [0, 3], [1, 1], [2, 2], [3, 0], [3, 1], [3, 2], [3, 3]]
"""
#Define allowed action set
def allowmoveset(merchants,slivers):
boat_num=int(raw_input("Please enter the maximum number of people on board: "))
allowacset=[]
for i in range(merchants+1):
for j in range(slivers+1):
if (i+j)<=boat_num:
allowacset.append([i,j])
allowacset.pop(0)
return allowacset
"""
Run and return results:
>>> allowset,merchants,slivers=allowexistset()
Please enter the number of merchants: 3
Please enter the number of followers: 3
>>> allowactionset(merchants,slivers)
Please enter the maximum number of people on board: 2
[[0, 1], [0, 2], [1, 0], [1, 1], [2, 0]]
"""
#Start action
def move(merchants,slivers):
k=1#Set the initial number of moves
current=[merchants,slivers]#Set the initial state of this shore
current1=[0,0]#Set the initial state of the opposite bank
#First randomly select an action plan from all allowacsets to execute
import random
move=random.choice(allowacset)
#Split the two elements of the move set list into the number of merchants and the number of followers
#Test if you are in a safe state after the action, let's assume one
trys=[current[0]+((-1)**k)*move[0],current[1]+((-1)**k)*move[1]]
#If the state after the action is executed belongs to the allowset, update current
print " " ,k ,"action:"
while current!=[0,0] and current1!=[3,3]:
if trys in allowset:
current[0]=current[0]+((-1)**k)*move[0]
current[1]=current[1]+((-1)**k)*move[1]
current1[0]=current[1]-((-1)**k)*move[0]
current1[1]=current[1]-((-1)**k)*move[1]
if k %2 ==1:
print [move[0],move[1]], "to the other side"
print current
else:
print [move[0],move[1]],"Return to the local bank"
print current
else:
continue
k+=1
Problem Description In the dark night, N travelers came to a narrow bridge without guardrails. If you don't use a flashlight, you wouldn't dare to cross the bridge anyway. Unfortunately, N people only...
P1002 River crossing problem topic Problems DFS BUG appears DP BUG appears topic Title description There is a pawn on point A on the board, and you need to go to point B. The rule of pawn walking: You...
#Analyzing conditions How to determine if the current state is effective 1. Number of wilders <= number of missionaries This rule includes: the left side is met, and the right side must be satisfie...
Hunter Crossing Summarize For two List, the same elements but different sequences, the two are not equal For the List described above, after the sorting Add a list A to the list L. After changing the ...
Crossing the river (greedy algorithm) - CapitalAccumulation - Bo Garden Thoughts are very clear River problem First, the problem description Second, the question answer Third, expand Fourth, problem p...
This is the "Functional Fun" reading notes, I use javascript to re-implement the algorithm. See you The author of this question is too complicated to implement. I define the initial state as...
Problem description: The farmer takes wolves, sheep, and cabbage across the river. If the farmer does not look at them, the wolf will eat the sheep and the sheep will eat the cabbage. But the boat can...
Farmer crossing the river (stack implementation, depth priority) 1. Problem description 2. Problem analysis 3. Code analysis refer to Farmer Crossing (BFS) (Queue) C ++ retrospective law seeking masto...
Crossing the river Description of the topic: In the dark night, N travelers came to a narrow bridge without guardrails. If you don't use a flashlight, everyone will not dare to cross the bridge anyway...
Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boa...