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. // First idea from left to right while (didWeMeet()) { distance = 1; for (int i = 0; i < distance; ++i) { moveLeft(); if (didWeMeet()) 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; } for (int i = 0; i < distance; ++i) { moveLeft(); if (didWeMeet()) return true; } ++distance; } ========================================================= 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(); } 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(); } for (int i = 0; i < distance; ++i) { moveLeft(); if (didWeMeet()) return true; } ++distance; }
run
|
edit
|
history
|
help
0
Empty C++
search_n algorithm
hack qus
MapTel2
BinTree playground
lab17feb22x4B.cpp
Microsoft Question - MaxEmployeeAttendence (original question)
Gauss Jordan Ida
Sieve of Eratosthenes
Atul