A farmer with his wolf, duck and bag of corn come to the east side of a river they wish to cross. There is a boat at the river’s edge, but of course only the farmer can row. The boat can only hold two things (including the rower) at any one time. If the wolf is ever left alone with the duck, the wolf will eat it. Similarly if the duck is ever left alone with the corn, the duck will eat it. How can the farmer get across the river so that all four arrive safely on the other side? Solve this problem using Depth First Search to find the shortest solution. Write Code in C++/Java.Its my assignment im unable to do it although have made the state space
Recommended Answers
Jump to Post@AssertNull running
astyle --style=allman --align-pointer=middle --max-code-length=79 farmer.cpp
does a pretty good job on formatting Builder_1's code
Jump to PostI posted a solution in python for this problem Click Here. The algorithm could perhaps give you some ideas to compare with your code.
Jump to PostI drew the following graph, which shows that there are only two routes of equal length. The starting point is the green dot
fdwc
(farmer, duck, wolf, corn) and the final point is the red dot, labelled 0. The arrows are possible transitions. I removed the yellow states from the …
Jump to PostHere is the graph with all states allowed. Note that an undirected graph can be drawn, because every move is reversible.
All 14 Replies
happygeek 2,411 Most Valuable Poster Team Colleague Featured Poster
Builder_1 -6 Junior Poster in Training
AssertNull 1,094 Practically a Posting Shark
AssertNull 1,094 Practically a Posting Shark
Gribouillis 1,391 Programming Explorer Team Colleague
AssertNull 1,094 Practically a Posting Shark
Builder_1 -6 Junior Poster in Training
Gribouillis 1,391 Programming Explorer Team Colleague
AssertNull 1,094 Practically a Posting Shark
Gribouillis 1,391 Programming Explorer Team Colleague
Gribouillis 1,391 Programming Explorer Team Colleague
AssertNull 1,094 Practically a Posting Shark
Gribouillis 1,391 Programming Explorer Team Colleague
AssertNull 1,094 Practically a Posting Shark
Builder_1 commented: Thanks alot +2
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.