Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Farmer Fran Farmer Fran needs eggs to make an omelette. She grows hay and also keeps chickens. Each day she can grow one new bale
Farmer Fran Farmer Fran needs eggs to make an omelette. She grows hay and also keeps chickens. Each day she can grow one new bale of hay and each day every chicken lays one egg. We keep track of her hay, eggs and chickens in the Inventory struct: struct Inventory int hay ; int eggs ; int chickens ; ; Each day Fran can optionally make one trade. Here are the possible trades: Trade bales of hay for chicken. Trade bale of hay for eggs. Trade eggs for chicken No trade Each day Fran can only do one of these transactions. For example if she has bales of hay she cannot trade this for chickens in a single day. This transaction can be done after Fran obtains her new hay and eggs for the day. You cannot go into debt to make a trade. For example to trade bales of hay for a chicken you must have after receiving your new bale of hay for the day at least bales of hay. The problem is to determine how many days it will take Fran to reach a target number of eggs, given an initial Inventory. The function we want to implement is: int daysForEggsconst Inventory& origin, int numberOfEggs; Given a starting inventory, and target number of eggs, what is the fewest number of days needed to obtain numberOfEggs? You do not need to return the sequence of trades to obtain this number of eggs, just the number of days. Believe it or not, this is a shortest path problem on a graph and can be solved using breadthfirst search. As we highlighted in lecture, to do breadthfirst search we do not need to explicitly store a graph. All we need is to iterate over the neighbours of a given vertex. In this problem we have a directed graph where vertices represent a state of Inventory. There is a directed edge from state to state if we can reach state from state in one day. Then finding the minimum number of days to go from a starting state to a state with a certain number of eggs is a shortest path problem in this graph. We have already provided the breadthfirst search code for you taken from the Knight Moves tutorial This code relies on the getNeighbours function: std::vector getNeighboursInventory state; You need to implement the getNeighbours function. This function should return a vector containing all the possible Inventories that can be reached from state in one day, in other words all the outadjacent neighbours of state in the graph described above. Example : Let's say we start with bale of hay, eggs, and one chicken. I will write this Inventory as and the target number of eggs is We already have the target number of eggs, so the answer is zero days. Example : Let's say we start with bale of hay, no eggs, and no chickens. I will write this Inventory as We can obtain eggs in two days. starting state day day : we got one new bale of hay, then traded one bale of hay for eggs. day : we got one new bale of hay, then traded one bale of hay for eggs. Example : Let's again start with bale of hay, no eggs, and no chickens. Let's say we want to get eggs. We can do this in days. starting state day day : we got one new bale of hay, then traded hay for chicken day : we got one new bale of hay and the chicken laid one egg. We traded the hay for eggs. day : we got one new bale of hay and the chicken laid one egg. We traded the hay for eggs. day : we got one new bale of hay and the chicken laid one egg. We traded the hay for eggs. day : we got one new bale of hay and the chicken laid one egg. We traded the hay for eggs. day : we got one new bale of hay and the chicken laid one egg. We traded the hay for eggs. Grading: When you press mark your code is run against test cases. Your score out of is the number of test cases you pass times four.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started