Navigation  without Java Scripts

The Farmer, Wolf, Goat, Cabbage problem

Prolog's powerful problem-solving capabilities can be shown with an example, the classic farmer-goat-cabbage-problem.

The problem

A farmer and his goat, wolf, and cabbage come to a river that they wish to cross. There is a boat, but it has only room for two, and the farmer is the only one that can row. If the goat and the cabbage get in the boat at the same time, the goat will eat the cabbage. Similarly, if the wolf and the goat are together without the farmer, the goat will be eaten. Devise a series of crossings of the river so that all concerned make it across safely.

The state of the system is indicated by a structure STATE, stating where the farmer, the goat, the wolf, and the cabbage are located. The goal is then to find out how to get from the start state to the final state through a series of valid states.

The valid states are checked by the predicate 'unsafe'.

The problem is that a state must only be visited once. This is handled by collecting the visited states in a list and checking that a new state isn't already in the list.

The predicate "go" can be called with a start state and a final state:

go( state(east,east,east,east), state(west,west,west,west) ).

 

To execute the program as a CGI script press the "Solve" button below


 

Show Source to FWGC-application.

Show Source to FWGC-application incl. cgi-support.