Professional Documents
Culture Documents
Chapter 3
Problem Solving
Chocolate Bars
Consider the problem of breaking a rectangular chocolate bar
with m × n squares into individual squares. Each horizontal or
vertical cut breaks one piece into two. The picture to the right
shows a 3 × 4 bar after three cuts. How many cuts are needed
to completely cut the bar into individual squares? What is the
best way to do so?
17
Emil Sekerinski: Computational Problem Solving, Draft, September 2010!
The key here was not to represent the shapes of the pieces in intermediate states.
This would have made the solution only more complicated.
River Crossing
A farmer needs to bring a wolf, a goat, and a cabbage across the river. The boat is
tiny and can only carry only one of them at a time. If he leaves the wolf and the goat
alone together, the wolf will eat the goat. If he leaves the goat and the cabbage alone
together, the goat will eat the cabbage. How can he bring all three safely across the
river?
For representing the problem, we observe that there are four individuals, the farmer,
the wolf, the goat, and the cabbage that change positions: they are either on the left
bank, in the boat, or on the right bank. In the boat the goat and cabbage are safe, as
the farmer is with them, so the only critical positions are the left and right bank.
Thus we introduce variables f, w, g, c –for the farmer, wolf, goat, cabbage– with val-
ues L, R –for the left and right bank. Initially all of f, w, g, c are L and finally they
should be R. The constraint that the wolf must not be left with the goat alone is ex-
pressed by:
# f=g=w∨g≠w
The constraint that goat and cabbage must not be left alone is expressed by:
# f=g=c∨g≠c
18