Question: A robot is standing on an integer number line spanning the range from to . Its position, x, starts at 0. There is an antenna

A robot is standing on an integer number line spanning the range from to . Its position, x, starts at 0. There is an antenna at an unknown location y that the robot must reach as quickly as possible in order to repair it. Since it doesnt know whether to move left or right, it searches in both directions by first moving one step to the right, then two steps to the left, then three to the right, and so on until it hits the antenna. So the locations it touches are as follows: (0, +1, 1, +2, 2, +3, 3, . . .). What is the -runtime of the robots search in terms of integer unit steps, if the antenna is n steps away? You should get the same answer regardless of whether its to the left or right.

Concretely, the robot plans its motion according to the following C++ code:

int position = 0;

int movement = 1;

int direction = 1;

while (!checkForPole(position)) {

for (int i = 0; i < movement; i++) {

if (direction == 1) goRight();

else goLeft(); }

movement += 1;

direction = -direction;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!