[Practice] a bridge to solve the problem with Xiao Ming python

Title:
Xiao Ming a cross a bridge when the bridge is night, it must have lights. Now Bob bridge to one second, Xiao Ming's brother to three seconds, Xiao Ming's father to six seconds, Xiao Ming's mother to eight seconds, Xiao Ming's grandfather to 12 seconds. Each time through the bridge may be up to two, and the bridge according to the speed of the slowest bridge may be, and the lamp goes out in 30 seconds after ignition. Q: Xiao Ming how a bridge?

Comparative idiot code below can be calculated from one kind of bridge scheme:

(Posted no indentation in the blog, so when the actual implementation Please note ......)

import random
 
list1 = [] # store all results traversal

# Wait staff across the bridge
bridge1 = { 'Bob': 1, 'brother': 3 'father': 6, 'Mother': 8 'grandfather': 12}

Staff after the bridge #
bridge2={}

# Determine whether the bridge is not empty (all had finished, the program stops, look how long it took total)
= len1 only (Bridge1)
print(len1)

#while True:

print ( 'Initial situation: the person is not currently bridge:', end = '')
print(bridge1)

print ( 'Initial situation: Current users of the bridge are:', end = '')
print(bridge2)
print('*********************************')

# A while, to complete a full family of river flow; once fow, to complete the course of a river and back (2 individuals across the river, a man come back)
while True:

for i in range(1,10):

print ( '% d of the bridge and back views as follows:'% i)

# Random art never take the bridge out of one (key), is taken out of a data string
p1=random.choice(list(bridge1))
print (p1 + 'bridge ready')
#print(type(p1))

After the # removed, these individual time corresponding bridge taken out (value), is taken out of an integer data type
print (p1 + 'a time' + str (bridge1 [p1]))
#print(type(bridge1[p1]))
time1 = bridge1 [p1] # of the first person used the time to save it for later comparison whichever is greater

# After removing the first person, this individual has been assigned to people across the river, across the river in the crowd and never deleted
bridge2[p1]=bridge1[p1]
del bridge1[p1]
print(bridge1)
#print ( 'ready bridge:', end = '')
#print(bridge2)

# Similarly, never cross the bridge and then take a random person and the corresponding time. At this time we found a problem, it may take time before people and the same, so it should take a front, once deleted.
p2=random.choice(list(bridge1))
print (p2 + 'ready bridge', end = '')
#print(type(p2))

#print(bridge1[p2])
#print(type(bridge1[p2]))
time2 = bridge1 [p2] # individual second used time stored together for subsequent comparison whichever is greater
print (p2 + 'a time:' + str (bridge1 [p2]))

# After removing the second person, this individual has been assigned to people across the river, across the river in the crowd and never deleted
bridge2[p2]=bridge1[p2]
del bridge1[p2]
print(bridge1)
print ( 'bridge has:', end = '')
print(bridge2)

# Find the maximum values ​​of the above two
time3=max(time1, time2)
print ( 'bridge time:' + str (time3))

total_time=total_time+time3


# Then the total time minus the first to cross the river most of the time
#left_time=30-time3

print ( 'who is not currently the bridge:', end = '')
print(bridge1)

print ( 'currently bridge:', end = '')
print(bridge2)
 
# If the river have been finished, there is no need to return it! So terminated!
if len(bridge1)==0:
print ( 'final total duration was:% d'% total_time)
break

#print ( 'the current remaining time is:% d'% left_time)

# Then select a person from the bridge has been the shortest time people come back, then that person's time used to take out, then moved back to the people who did not have people crossing the bridge from the bridge
p3=min(bridge2, key=bridge2.get)
#p3=random.choice(list(bridge2))
time4=bridge2[p3]
print (p3 + 'back', end = '')
#print(bridge2[p3])
print (p3 + 'a time:' + str (bridge2 [p3]))
bridge1[p3]=bridge2[p3]
del bridge2[p3]

= len1 only (Bridge1)

print ( 'currently bridge:', end = '')
print(bridge2)
print ( 'not currently bridge:', end = '')
print(bridge1)
total_time = total_time+time4
print ( 'current total time spent:% d'% total_time)

## record this time round complete bridge
list1.append(total_time)
#print(list1)

if total_time<=30:
break

# After performing a re-restore the original state, pitted round
bridge1 = { 'Bob': 1, 'brother': 3 'father': 6, 'Mother': 8 'grandfather': 12}
bridge2={}
total_time=0
 
list1.sort()
print(list1)
 
operation result:

 

Guess you like

Origin www.cnblogs.com/reinhard314/p/11418092.html