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. #include #include #include #include #include #include "farm.hpp For you to implement std::vector getNeighboursInventory p return all the states that can be reached from p in one day each day you receive one new bale of hay and one egg for each chicken you have you can then take one of the following actions: Do nothing Trade bales of hay for chicken. Trade bale of hay for eggs. Trade eggs for chicken You cannot go into debt. For example, to trade bales of hay for a chicken you must have at least bales of hay. dummy return return ; int daysForEggsconst Inventory origin, int numberOfEggs queue holding vertices to be explored First item of pair is an inventory, second item is distance of the item from origin std::queue inventoryQueue ; inventoryQueue.pushorigin; record how we arrived at an Inventory. We first reach v from prevv This is not needed for the exercise, but may help with your debugging std::map prev ; record the Inventories that have already been visited visited means you have previously been put into the queue std::set visited origin; while inventoryQueue.empty auto current dist inventoryQueue.front; inventoryQueue.pop; if currenteggs numberOfEggs For debugging you can see the path taken: for Inventory state current; state origin; state prevstate std::cout state; std::cout ; std::cout origin; return dist; Here is where the function you implement is used std::vector neighbours getNeighbourscurrent; for Inventory neighbour : neighbours if visited.containsneighbour prevneighbour current; visited.insertneighbour; if not visited, the distance of neighbour from the origin is one plus the distance from the origin of the node from which we visit neighbour inventoryQueue.pushneighbour dist ; should not reach here on a valid input return ; #ifndef FARMHPP #define FARMHPP #include #include #include struct Inventory int hay ; int eggs ; int chickens ; spaceship operator lets us use all comparison operations between two Inventory objects auto operatorconst Inventory other const default; To help in debugging you can print out an Inventory as usual: Inventory state ; std::cout state; friend std::ostream operatorstd::ostream out, const Inventory state out "hay: state.hay ; out "eggs: state.eggs ; out "chickens: state.chickens ; return out; ; first parameter is starting state, second parameter is target number of eggs int daysForEggsconst Inventory int; #endif FARMHPP
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