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:
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;
}
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:
- First move will always be in backward direction
- 1 <= N <= 100
- forward movement > 0
- backward movement > 0
- time > 0
- distance from the whirlpool ahead of them (FD) > 0
- distance from the devil behind them (BD) > 0
- 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
Post a Comment