Skip to main content

Programming challenge: escape from devil & whirlpool

 Problem : Between Devil and Deep Sea

Below is the story line 

A band of pirates have made loot. By their misfortune though, instant Karma is delivered. While they are travelling a
treacherous route in the high and the deep seas they are also haunted by a devil, perhaps a spirit who suffered at their
hands previously. The pirates now have to make tricky decisions. They run forward to save themselves from the Devil, but
the deep seas force them backwards. There is a danger of falling in a whirlpool if they move forward rashly and there is a
danger of being caught by the Devil if they move too much back. But move they must.
First they recede backwards B meters and then advance forward F meters, in a straight line. They cover 1 meter in T units
of time. This backward and forward movement is performed repeatedly by the Pirates because they have panicked.
Your task is to find out if they will perish at the hands of the Devil or they will get sucked into the whirlpool of the Deep Sea
and in how much time. It is also possible that by good fortune they might survive. Write a program to calculate the outcome

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 Pirates' starting position and the whirlpool in forward direction
5. BD denotes distance from Pirates' starting position and the Devil in backward direction

Output Format:
For each test case, print time taken by the Pirates to be caught by the devil or for them to fall into the whirlpool. Print F if
they fall in the whirlpool ahead of them and B if they get caught by the devil behind them. Both the outputs must be
delimited by white-space
OR
Print Thank God if the Pirates manage to survive

Constraints:

  1. First move will always be in backward direction
  2. 1 <= N <= 100
  3. forward movement > 0
  4. backward movement > 0
  5. time > 0
  6. distance from the whirlpool ahead of them (FD) > 0
  7. distance from the devil behind them (BD) > 0
  8. All input values must be integer only 


Solution in C++ :



#include <iostream>

using namespace std;
/*
checks all the parameters and returns negative if any of the parameters fail against given constraint
(all the values should be present & positive)
otherwise positive
*/
int inputParameterFilter(int forwardStep=-1,int backwardStep=-1,int timeRateforMeter=-1,int distFromWhirl=-1,int distFromDevil=-1)
{
int pCheck=0;
if((forwardStep>0)&&(backwardStep>0)&&(timeRateforMeter>0)&&(distFromDevil>0)&&(distFromWhirl>0))
{
return 1;
}
return -1;
}

struct pirate
{
    int position;
    int distFromWhirl;
    int distFromDevil;
    int timeElapsed;
    int totalDistance;
};
struct scenario
{
    int forwardStep;
    int backwardStep;
    int timeRateforMeter;
    int distFromWhirl;
    int distFromDevil;

}
;
int main()
{

    scenario s1;
    pirate p1;
    int noTestCases=0;
    cout<<"Enter No of test Cases"<<endl;
    cin>>noTestCases;

while(noTestCases>0)
    {
        noTestCases--;
        // uses number line traversal concept rather than arithmetic progression calculation
    cout <<"Enter Parameters (\nforward step, \nbackward step, \ntime required for a meter, \ndistance from devil, \ndistance from whirl pool )\n"<<endl;
    cin>>s1.forwardStep;
    cin>>s1.backwardStep;
    cin>>s1.timeRateforMeter;
    cin>>s1.distFromDevil;
    cin>>s1.distFromWhirl;

    cout <<"Checking input parameters for constraints restrictions "<<endl;

    if(inputParameterFilter(s1.forwardStep,s1.backwardStep,s1.timeRateforMeter,s1.distFromDevil,s1.distFromWhirl)<0)
    {
    cerr<<"parameter error"<< endl;
    return 0;
    }
    p1.distFromDevil=s1.distFromDevil*-1;
    p1.position=0;
    p1.totalDistance=0;
    p1.distFromWhirl=s1.distFromWhirl;

    // check for case if Pirates will survive (f==b)
    if(s1.forwardStep==s1.backwardStep)

        {
        cout<<"THANK GOD"<<endl;
        return 0;
        }
    // Simulate and find out how much time it requires to actually reach any of the two ends

int simComplete=-1;
    while(simComplete==-1)
    {
        p1.position+=(s1.backwardStep*-1);
        p1.totalDistance+=s1.backwardStep;
        //check if devil caught
        if(p1.distFromDevil>=p1.position)// Caution -ve position in number line
            {
                cout<<(p1.totalDistance/s1.timeRateforMeter)<< " B "<<endl;
                simComplete=1;
            }
        p1.position+=s1.forwardStep;
        p1.totalDistance+=s1.forwardStep;

        // check if fell in whirlpool

        if(p1.distFromWhirl<=p1.position)// Caution -ve position in number line
            {
                cout<<(p1.totalDistance/s1.timeRateforMeter)<< " F "<<endl;
                simComplete=1;
            }
    }

    }


return 0;
}

Comments

Popular posts from this blog

The Indian supremacy theory.

The I ndian supremacy theory : I believe We Indians are supreme above every other human races in the world cuz we are more humane, we are open to incorporate other cultures evolve & adapt to take in the goodness from others & also share what's good in ours. We are more humane & we care for our family & love them more than anything else in the world. We are superior cuz we don't separate ourselves from other people on the basis caste Creed religion nationality. We belong to a bigger United world. Working together for a common goal "betterment of life of everyone in this world & other". By referring one as an "Indian" we point to those people who, no matter where, when, how he or she born or live,  live a life of loving & caring, irrespective & above of all the caste Creed classification, working for the betterment of life & there by the Nature and the Oneness . Don't separate ourselves from other cuz, we are different

Out of curiosity a port of love letter generator in c#

This  is   for  my Harley - Queen(still waiting):  this   is  a cook up code to tinker around: using  System; using  System . Text;   namespace  LoveLetter {      internal   class   Program     {    static   string  NextRandomWord( string [] arr, Random rand)  =>  arr[rand . Next( 0 , arr . Length)];          private   static   void  Main()         {              var  salutation1  =   new   string [] {  "Beloved" ,  "Darling" ,  "Dear" ,            "Dearest" ,  "Fanciful" ,  "Honey"  };              var  salutation2  =   new   string [] {  "Chickpea" ,  "Dear" ,                        "Duck" ,  "Jewel" ,  "Love" ,  "Moppet" ,  "Sweetheart"  };              var  adjs  =   new   string [] {  "affectionate" ,  "amorous" ,  "anxious" ,              "avid" ,  "beautiful" ,  "breathless" ,  "