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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
...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
step2 --> on return take wolf with
step3 -->now take cabbage and wolf
for this u must use stack
Can only take 1 item at a time (rower plus 1 item)
mlmcc
mlmcc
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
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!