Saturday, August 8, 2015

Robot's move

Problem : Robot's move
A robot is programmed to move forward F meters and backwards again, say B meters, in a straight line. The Robot covers 1 meter in T units of time. On Robot's path there is an obstacle after distance D from initial position in forward direction. This forward and backward movement is performed repeatedly by the Robot.

Your task is to calculate amount of time taken before the Robot hits the obstacle.

Input Format:
First line contains total number of test cases, denoted by N
Next N lines, contain a tuple containing 4 values delimited by space
F B T D, where
  1. F denotes forward displacement in meters
  2. B denotes backward displacement in meters
  3. T denotes time taken to cover 1 meter
  4. D denotes distance from Robot's starting position and the obstacle in forward direction

Output Format:

For each test case print time taken by the Robot to hit the obstacle

Constraints:
First move will always in forward direction
1 <= N <= 100
backward displacement < forward displacement i.e. (B < F)
forward displacement (F) > 0
backward displacement (B) > 0
time (T) > 0
distance (D) > 0
All input values must be positive integers only

Sample Input and Output

SNo.InputOutput
1
2
5 4 8 10
8 2 3 15

400
69

Solution:
Find the distance to the point just before the last forward cycle starts. Find the time taken to reach this point. Beyond this point just add the time taken to take the forward steps.

C++ Program:
#include <iostream>
using namespace std;

int main(void) 
{
 int F,B,T,D;
 int N;
 cin>>N;
 while(N--)
 {
  cin>>F>>B>>T>>D;
  int forward = F-B; //The distance moved by robot in one forward and backward cycle
  int timeInOneCycle = (F+B)*T; //The time taken by robot to complete one forward and backward cycle
  int secondLastDistance = D-F; //The distance that the robot has to move before it can hit the obstacle
  int timeToSecondLastDistance = 0;
  int quotient = secondLastDistance/forward; //The number of forward and backward cycles needed to reach the secondLastDistance
  int remainder = secondLastDistance%forward;
  timeToSecondLastDistance = timeInOneCycle*quotient; //The time taken to reach the secondLastDistance
  int currentDistance = quotient*forward; //The distance covered by robot after completing quotient number of cycles
  int remainingTime = 0;
  int remainingDistance = D-currentDistance; //The distance remaining from the secondLastDistance point
  if(secondLastDistance <= 0) //If the total distance needed to be covered is less than or equal to the distance F covered by robot in forward direction
  {
   timeToSecondLastDistance = D*T;
  }
  else if(remainingDistance <= F) //If the remaining distance needed to be covered is less than or equal to the distance F covered by robot in forward direction
  {
   remainingTime = remainingDistance*T;
  }
  else //If the quotient calculated above is less than the number of cycles needed to reach the secondLastDistance point
  {
   currentDistance = currentDistance + forward;
   remainingTime = remainingTime + timeInOneCycle;
   remainingDistance = remainingDistance - forward;
   remainingTime = remainingTime + remainingDistance*T;
  }
  cout<<timeToSecondLastDistance + remainingTime<<endl;
 }
 return 0;
}

No comments:

Post a Comment