TCS MockVita 2 Solution : Problem A

Problem : Catch22

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 a ditch at a distance FD from initial position in forward direction as well as a ditch at a distance BD from initial position in backward direction. This forward and backward movement is performed repeatedly by the Robot.

Your task is to calculate amount of time taken, before the Robot falls in either ditch, if at all it falls in a ditch.


Input Format:
First line contains total number of test cases, denoted by N
Next N lines, contain a tuple containing 5 values delimited by space
F B T FD BD, 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. FD denotes distance from Robot's starting position and the ditch in forward direction
5. BD denotes distance from Robot's starting position and the ditch in backward direction

Output Format:
For each test case print time taken by the Robot to fall in the ditch and also state which ditch he falls into. Print F for forward and B for backward. Both the outputs must be delimited by whitespace
OR Print No Ditch if the Robot does not fall in either ditch

Constraints:
1. First move will always be in forward direction
2. 1 <= N <= 100
3. forward displacement > 0
4. backward displacement > 0
5. time > 0
6. distance of ditch in forward direction (FD) > 0
7. distance of ditch in backward direction (BD) > 0
8. All input values must be positive integers only

Sample Input 1:

3
9 4 3 13 10
9 7 1 11 13
4 4 3 8 12

Sample Output 1:

63 F
25 F
No Ditch

Sample Input 2:

5
8 4 7 11 22
4 5 4 25 6
4 9 3 6 29
7 10 6 24 12
10 10 1 9 7

Sample Output 2:

133 F
216 B
231 B
408 B
9 F

C++ Implementation:

#include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <climits>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <cstring>
    using namespace std;
    int main()
    {
        int N;
        cin>>N;
        while(N--)
        {
            int f,b,t,fd,bd;
            bool result = true;
            int position = 0;
            int cost = 0;
            char movement;
            cin>>f>>b>>t>>fd>>bd;
            if(f==b)
            {
                if(fd>f && bd>f)
                {
                    result = false;
                }
                else
                {
                    //calculate code here
                    while(position<fd)
                    {
                        position += f;
                        cost = cost + (f * t);
                        movement = 'F';
                        if(position>=fd)
                        {
                            cost = cost - ((position-fd)*t);
                            break;
                        }
                   
                        position -= b;
                        cost = cost + (b * t);
                        movement = 'B';
                        if(abs(position)>=bd)
                        {
                            cost = cost - ((abs(position)-bd)*t);
                            break;
                        }
                    }
                }
            }
            else
            {
                while(position<fd)
                {
                    position += f;
                    cost = cost + (f * t);
                    movement = 'F';
                    if(position>=fd)
                    {
                        cost = cost - ((position-fd)*t);
                        break;
                    }
                   
                    position -= b;
                    cost = cost + (b * t);
                    movement = 'B';
                    if(abs(position)>=bd)
                    {
                        cost = cost - ((abs(position)-bd)*t);
                        break;
                    }
                }
            }
            if(N!=0)
            {
                if(result)
                    cout<<cost<<" "<<movement<<endl;
                else
                    cout<<"No Ditch"<<endl;
            }
            else
            {
                if(result)
                    cout<<cost<<" "<<movement;
                else
                    cout<<"No Ditch";
            }
        }
        return 0;
    }


Other Problems from TCS Mockvita - 1 and Mockvita - 2:

Solution : MockVita-I Problem D

Solution : MockVita-II Problem C

Solution : MockVita-II Problem B

Solution : MockVita-II Problem D

Solution : MockVita-II Problem A

Solution : MockVita-I Problem B

Solution : MockVita-I Problem C

Solution : MockVita-I Problem F

Solution : MockVita-I Problem A