File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programming Diversions and the fly likes Farmer Goat Wolf Cabbage Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Farmer Goat Wolf Cabbage Problem" Watch "Farmer Goat Wolf Cabbage Problem" New topic

Farmer Goat Wolf Cabbage Problem

Butch El

Joined: Nov 10, 2011
Posts: 1
So im sure everybody knows this problem. The goal is to move the farmer from the East side to the West side with either the goat, wolf, or cabbage one by one until everyone is on the WEST. However certain things cannot be left alone (ie leaving the goat and wolf on one side or goat and cabbage). Basically I have to use different Search functions: Depth First Search, Breadth First Searh, and Best Fit Search. For now Im just trying to figure out how to test my code so far before I implement a solution.

Here's my my class so far.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

is there a solution?
if move wolf first, that leaves goat with cabbage.
if move cabbage first, that leaves wolf with goat.
if move goat first, then have to move either wolf or cabbage next and leave it with goat to go back for the last item.

on second thought it doesn't say he has to do it in 3 trips.
take goat across.
take wolf across and bring back goat.
take cabbage across.
take goat across.

a more practical solution is to kill the wolf first.

Visit my download page
Tim Moores

Joined: Sep 21, 2011
Posts: 2413
Randall Twede wrote:is there a solution?

Hint: the farmer can take something into his boat in both directions.
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 194

The Solution is:
1. Move the Goat first.
2. Now take the wolf.
3. while dropping the wolf on other side take the goat back on the boat.
4. deboard the goat and take the cabbage
5. now come back and take the cabbage..

Do not wait to strike till the iron is hot; but make it hot by striking....
Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2746

Won't the wolf eat the farmer first? :p
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Or the farmer could simplify the problem by eating the cabbage.
Steve Fahlbusch

Joined: Sep 18, 2000
Posts: 601

i think you mean the farmer could simply eat the goat.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Yes, that would make the problem even simpler.
Steve Fahlbusch

Joined: Sep 18, 2000
Posts: 601

Getting back to the OP....

I am just a bit confused here.......

This is a classic state transition problem.

There are 2 unique solutions to this problem (do we bring cabbage or wolf over first, after we took the goat at the first step)...... what will depth first, breadth first or best fit show anything different?

to Paul,

if we are going to make it simple, we might as well make it trivial.

farmer ate a stew of goat and cabbage and then got fid of the wolf. farmer crossed the river in one pass :-)
I agree. Here's the link:
subject: Farmer Goat Wolf Cabbage Problem
jQuery in Action, 3rd edition