Blue and Orange are friendly robots. An evil computer mastermind has
locked them up in separate hallways to test them, and then possibly give
them cake.
Each hallway contains 100 buttons labeled with the positive integers {1, 2, ..., 100}. Button k is always k meters from the start of the hallway, and the robots both begin at button 1. Over the period of one second, a robot can walk one meter in either direction, or it can press the button at its position once, or it can stay at its position and not press the button. To complete the test, the robots need to push a certain sequence of buttons in a certain order. Both robots know the full sequence in advance. How fast can they complete it?
For example, let's consider the following button sequence:
Here,
Each hallway contains 100 buttons labeled with the positive integers {1, 2, ..., 100}. Button k is always k meters from the start of the hallway, and the robots both begin at button 1. Over the period of one second, a robot can walk one meter in either direction, or it can press the button at its position once, or it can stay at its position and not press the button. To complete the test, the robots need to push a certain sequence of buttons in a certain order. Both robots know the full sequence in advance. How fast can they complete it?
For example, let's consider the following button sequence:
O 2, B 1, B 2, O 4
Here,
O 2
means button 2 in Orange's hallway, B 1
means button 1 in Blue's hallway, and so on. The robots can push this
sequence of buttons in 6 seconds using the strategy shown below:
Time | Orange | Blue -----+------------------+----------------- 1 | Move to button 2 | Stay at button 1 2 | Push button 2 | Stay at button 1 3 | Move to button 3 | Push button 1 4 | Move to button 4 | Move to button 2 5 | Stay at button 4 | Push button 2 6 | Push button 4 | Stay at button 2Note that Blue has to wait until Orange has completely finished pushing
O 2
before it can start pushing B 1
.
2 comments :
from time import time
def lookAhead(x,which,i):
" return llok ahead postoion for WHICH robot and reture WHERE next ?"
if which =='RED':
#Next postive number after where..
try:
return [ x[j] for j in range(i+1,len(x)) if x[j]>0 ][0]
except:
# no next
return None
if which =='BLUE':
#Next postive number after where..
try:
return abs([ x[j] for j in range(i+1,len(x)) if x[j]<0 ][0])
except:
# no next
return None
def miniRedBlackRobotMove(x):
""" return Minimun number of sec required to get the sequence
the input is simbolic here :
Ri indicate as +i and Bi indicate as -i
thus input is a collection of + and -ve number"""
#Start from 1 location : indiacte position..
Pr=1;
Pb=1;
#SECOND
sec=0
for i in range(len(x)):
if x[i] < 0: ##BLUE ONE ..
k=abs(x[i])
t=lookAhead(x,'RED',i)
while(Pb !=k):
sec+=1;
print 'sec: ',sec,' |',
if Pb 0: ##RED ONE ..
k=abs(x[i])
t=lookAhead(x,'BLUE',i)
while(Pr !=k):
sec+=1;
print 'sec: ',sec,' |',
if t==None or Pb==t:
print 'BLUE: STAY AT %d |' % (Pb),
elif Pb<t:
Pb+=1;print 'BLUE: MOVE RIGHT UPTO %d |' % (Pb),
else:
Pb-=1;print 'BLUe: MOVE LEFT UPTO %d |' % (Pb),
if Pr<k:
Pr+=1;print 'RED: MOVE RIGHT UPTO %d |' % (Pr)
else:
Pr-=1;print 'RED: MOVE LEFT UPTO %d |' % (Pr)
#RED REACHES ...
sec+=1
print 'sec: ',sec,' |',
if t==None or Pb==t:
print 'BLUE: STAY UPTO %d |' % (Pb),
elif Pb<t:
Pb+=1;print 'BLUE: MOVE RIGHT UPTO %d |' % (Pb),
else:
Pb-=1;print 'BLUE: MOVE LEFT UPTO %d |' % (Pb),
print 'RED: PRESS BUTTON %d |' % (Pr)
return sec
t0=time()
#print lookAhead([1,-2,3,-3,6,7],'BLUE',1)
print miniRedBlackRobotMove([2,-5,4,-1])," | Time Taken by rec : ",time()-t0
@dd are bhai algo batao na code kya likha hai yaar ?
Post a Comment