A Python implementation of the classic river crossing puzzle featuring a farmer trying to transport a wolf, a goat, and a cabbage across a river.
A farmer needs to transport three items across a river:
- ๐บ Wolf
- ๐ Goat
- ๐ฅฌ Cabbage
- ๐ฃ The boat can only carry the farmer and at most one item
- ๐บ If left alone, the wolf will eat the goat
- ๐ If left alone, the goat will eat the cabbage
- ๐งโ๐พ The farmer must be present for all river crossings
This solution features:
- Object-oriented design with proper encapsulation
- Type hints for better code clarity
- Breadth-first search algorithm for finding the optimal solution
- State validation to ensure no animals eat each other
- Comprehensive move tracking
python river_crossing.py
The program will output a step-by-step solution showing how to safely transport all items across the river.
Bank
: Enum representing river banksAnimalType
: Enum for different transportable itemsAnimal
: Dataclass representing an item with its current locationGameState
: Class managing the current state of the puzzleRiverCrossing
: Main solver class implementing BFS algorithm
MIT License