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

Start

Welcome to GeekyJackie  well this is my technical blog where I intend to showcase my Technical Ideas, project etc.... And its Day One.  A Little About My self will do great right ? Myself JazeemAzeez  AKA Jackie. Well Right now im a Student doing Post-graduation in CSE(Computer Science and Engineering ) @ Hindustan University Chennai. I feel Sometimes a little Geeky/Nerdy. This is my final year of PG right now doing . About the project  A long story in short.During PG we students can choose our project under the domain which we like to specialize. so Initially i Had Chosen Network security Domain & Later on due to Circumstances I Had been re allocated to Specialize in Data Mining & Far most Shocking was that I had Go a Guide Who is know for his perfectionism. Data Mining is a field or paper which I really intended to avoid. My previous Guide Motivated me to do it, I had started searching for a Topic. basically it was ...
Going OpenSource ... Recently I have been thinking, I have finished several projects , but whats my contribution to the public cuz most of  the works have been for clients... So gonna start uploading some of the nightly builds over there

Need For a Simple Distributed Computing Tools

I have been working on a few hobby project these days. while going through,I came across two key challenges. Challenge One "The Need to tap in to parallel processing power of all the resources one might have in a simple way" Assume the scenario of a person who is in a need to jump start a research activity/ some computationally intensive task, which needs a large computing power & resources. And he or she is in such a situation that they want to be able in include resources in much more dynamic way & is on a Mobile or Adhoc network where anything can happen. At the very same time i don't want to ignore the point that these days even our mobile devices have turned so powerful (a typical arm cpu of  Samsung s4   will churn out 49 Gigaflops). And the overall system should be simple to use understand & operate. I came across a thess work Self organised software agents where the agent programs are designed in such a way that they understands the enviro...