Link to home
Start Free TrialLog in
Avatar of ozguny
ozguny

asked on

"Wolf, Goat, Cabbage problem"s solution code in Java

Hi!
The problem is:
A farmer with his wolf, goat and cabbage come to the edge of a river they wish to cross. There is a boat at the river's edge. But, of course, only the farmer can row it. The boat also can carry only two things (including the rower) at a time. If the wolf is ever left alone with the goat, the wolf will eat the goat; similarly, if the goat is left alone with the cabbage, the goat will eat the cabbage. Devise a sequence of crossings of the river so that all four characters arrive safely on the other side of the river.


 I need source code which finds a solution to this problem in Java/C/Pascal/Basic.

Thanks
SOLUTION
Avatar of contactkarthi
contactkarthi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
...I vaguely remember doing this exact problem in school...it was an exercise in understanding stacks....
step1--> take wolf and goat
step2 --> on return take wolf with
step3 -->now  take cabbage  and wolf

for this u must use stack
Avatar of Mike McCracken
Mike McCracken

Can only take 1 item at a time (rower plus 1 item)

mlmcc
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozguny

ASKER

I should make use of trees and Breadth-first Search or Depth-first Search.
I think I understood the solution, I'll build up a tree for every probability.
4 boolean variables or an bool array for farmer, wolf, goat, cabbage which marks near bank or far bank
Thanks everyone for your help!