Answered step by step
Verified Expert Solution
Question
00
1 Approved Answer
#include #include #include #include #include #include farm.hpp / / * * * For you to implement std::vector getNeighbours ( Inventory p ) { std::vector
#include #include #include #include #include #include "farm.hpp For you to implement std::vector getNeighboursInventory p std::vector neighbours; Do nothing Inventory neighbour phay peggs pchickens, pchickens; if neighbourhay && neighbour.eggs neighbours.pushbackneighbour; Trade bales of hay for chicken. if phay neighbour phay peggs, pchickens ; if neighbourhay && neighbour.eggs neighbours.pushbackneighbour; Trade bale of hay for eggs. if phay neighbour phay peggs pchickens; if neighbourhay && neighbour.eggs neighbours.pushbackneighbour; Trade eggs for chicken if peggs neighbour phay, peggs pchickens ; if neighbourhay && neighbour.eggs neighbours.pushbackneighbour; Return either neighbour or neighbours based on what is better return neighbours.size neighbours : std::vectorneighbour; int daysForEggsconst Inventory& origin, int numberOfEggs std::queue inventoryQueue ; inventoryQueue.pushorigin; std::map prev ; std::set visited origin; while inventoryQueue.empty auto current dist inventoryQueue.front; inventoryQueue.pop; if currenteggs numberOfEggs return dist; std::vector neighbours getNeighbourscurrent; for Inventory neighbour : neighbours if visited.containsneighbour prevneighbour current; visited.insertneighbour; inventoryQueue.pushneighbour dist ; return ;
#include
#include
#include
#include
#include
#include "farm.hpp
For you to implement
std::vector getNeighboursInventory p
std::vector neighbours;
Do nothing
Inventory neighbour phay peggs pchickens, pchickens;
if neighbourhay && neighbour.eggs
neighbours.pushbackneighbour;
Trade bales of hay for chicken.
if phay
neighbour phay peggs, pchickens ;
if neighbourhay && neighbour.eggs
neighbours.pushbackneighbour;
Trade bale of hay for eggs.
if phay
neighbour phay peggs pchickens;
if neighbourhay && neighbour.eggs
neighbours.pushbackneighbour;
Trade eggs for chicken
if peggs
neighbour phay, peggs pchickens ;
if neighbourhay && neighbour.eggs
neighbours.pushbackneighbour;
Return either neighbour or neighbours based on what is better
return neighbours.size neighbours : std::vectorneighbour;
int daysForEggsconst Inventory& origin, int numberOfEggs
std::queue inventoryQueue ;
inventoryQueue.pushorigin;
std::map prev ;
std::set visited origin;
while inventoryQueue.empty
auto current dist inventoryQueue.front;
inventoryQueue.pop;
if currenteggs numberOfEggs
return dist;
std::vector neighbours getNeighbourscurrent;
for Inventory neighbour : neighbours
if visited.containsneighbour
prevneighbour current;
visited.insertneighbour;
inventoryQueue.pushneighbour dist ;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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