- Edited
Welcome to yet another programming exercise.
So suddenly in the middle of an RPG game, you find yourself stranded on a tower with lava beneath you, a bridge, a candle light, and another tower that is safe and connects you to safety.
Each of your fellows requires a different time to reach the other end.
The bridge can hold two people at most.
No one can cross the bridge without the candle light.
Two people moving together must stay together (at the pace of the slower fellow).
You are all stranded on the dangerous side for starters, along with your candle light.
What is the minimum time required to pass the bridge?
Input: You are given the number of fellows (including you) that want to cross to safety. You are also given an array of positive integers representing the time needed for each of your fellows to cross the bridge.
PS: This is not an easy problem (it's not hard though, classic Computer Science material), and the calculation of the minimum time entails a calculation of the steps required to move to the other tower (at least that's how I had to implement it).
Example:
3 fellows,
Times for each of the fellows: 1 2 3
timer starts
1 and 2 move to the safe side. (Timer reads 2 minutes)
1 alone moves back to the dangerous side. (Timer reads 3 minutes)
1 and 3 move to the safe side. (Timer reads 6 minutes)
Everyone is safe in six minutes if the lava doesn't eat them up by then.
I wrote an implementation in C# and learned a few lessons about the .net collection containers. I am relatively still new to the language.
Hope you'll enjoy, oh and there's possibly a much more elaborate formulation to the problem that I am thinking of - but it will only test your ability to keep your sanity while debugging rather than challenge you algorithmically.
So suddenly in the middle of an RPG game, you find yourself stranded on a tower with lava beneath you, a bridge, a candle light, and another tower that is safe and connects you to safety.
Each of your fellows requires a different time to reach the other end.
The bridge can hold two people at most.
No one can cross the bridge without the candle light.
Two people moving together must stay together (at the pace of the slower fellow).
You are all stranded on the dangerous side for starters, along with your candle light.
What is the minimum time required to pass the bridge?
Input: You are given the number of fellows (including you) that want to cross to safety. You are also given an array of positive integers representing the time needed for each of your fellows to cross the bridge.
PS: This is not an easy problem (it's not hard though, classic Computer Science material), and the calculation of the minimum time entails a calculation of the steps required to move to the other tower (at least that's how I had to implement it).
Example:
3 fellows,
Times for each of the fellows: 1 2 3
timer starts
1 and 2 move to the safe side. (Timer reads 2 minutes)
1 alone moves back to the dangerous side. (Timer reads 3 minutes)
1 and 3 move to the safe side. (Timer reads 6 minutes)
Everyone is safe in six minutes if the lava doesn't eat them up by then.
I wrote an implementation in C# and learned a few lessons about the .net collection containers. I am relatively still new to the language.
Hope you'll enjoy, oh and there's possibly a much more elaborate formulation to the problem that I am thinking of - but it will only test your ability to keep your sanity while debugging rather than challenge you algorithmically.