Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
OTHER - Two robots
Two robots land with their parachutes on an infinite one-dimensional number line. They both release their parachutes as soon as they land and start moving. They are allowed only to make use of the following functions. I. moveLeft() // robot moves to left by 1 unit in 1 unit time II. moveRight() // robot moves to right by 1 unit in 1 unit time III. noOperation() // robot does not move and takes 1 unit time IV. onTopOfParachute() // returns true if the robot is standing on top of either of the parachute, else false V. didWeMeet() // returns true if the robot meets to the other robot, else false Write a function in order to make the robots meet each other. Robots will be executing the same copy of this function. ========================================================= while (didWeMeet()) { distance = 1; for (int i = 0; i < distance; ++i) { moveLeft(); if (didWeMeet()) return true; // we found the other robot's parachute if (onTopOfParachute()) { while (didWeMeet()) moveLeft(); return true; } } for (int i = 0; i < distance; ++i) { moveRight(); if (didWeMeet()) return true; } // we are at on our parachute for (int i = 0; i < distance; ++i) { moveRight(); if (didWeMeet()) return true; // we found the other robot's parachute if (onTopOfParachute()) { while (didWeMeet()) moveRight(); return true; } } for (int i = 0; i < distance; ++i) { moveLeft(); if (didWeMeet()) return true; } distance *= 2; }
run
|
edit
|
history
|
help
0
AgeGroupHisto
SegTree
Breakfast Function
chocolate Distribution Problem
PointSum
A number is prime or not
Elevator 2
Minimum Vertices to Traverse Directed Graph
Test 13(2020)
no copy elision