[Prolog] Missionaries and Cannibals – M Missionaries & C Cannibals & Boat carries N passengers version

In the missionaries and cannibals problem, three missionaries and three cannibals must cross a river using a boat which can carry at most two people, under the constraint that, for both banks, if there are missionaries present on the bank, they cannot be outnumbered by cannibals (if they were, the cannibals would eat the missionaries). The boat cannot cross the river by itself with no people on board.

(Source: Wikipedia)

Please read [Prolog] State space search method before continuing. Here I represent the state as a list in below structure:

[Boat, Missionaries, Cannibals]

  • Boat: If Boat = 0 then the boat is on the left bank, if Boat = 1 then the boat is on the right bank.
  • Missionaries: Number of missionaries are currently on the right bank.
  • Cannibals: Number of cannibals are currently on the right bank.

Beside the game state, we also have the game settings represented by a list with below structure:

mcproblem = [M, C, N]

  • M is the total number of missionaries on both banks. In the problem statement above, M = 3.
  • C is the total number of cannibals on both banks. In the problem statement above, C = 3.
  • N is the maximum number that the boat can carry. In the problem statement above, N = 2.
  • mcproblem is the global variable that carry the list represents game settings.

We also make some observations:

  1. If there are Missionaries missionaries on the left bank, then there are M – Missionaries missionaries on the right bank. Same goes for cannibals.
  2. We call A is the number of missionaries that is on the boat. If the boat is moved from left to right then: Missionaries (new) = Missionaries (old) + A. If the boat is moved from right to left then: Missionaries (new) = Missionaries (old) – A. Same goes for Cannibals with B is the number of cannibals on the boat. We also have contrains: 0 <= A <= N, 0 <= B <= N, 1 <= A + B <= N.
  3. If the boat is on the left bank, then only missionaries or cannibals currently being on the left bank can use the boat to move to the right bank, and vice versa.
    • So if Boat = 1, then missionaries can only be on boat if Missionaries > 0 (there is at least one missionary on the right bank), cannibals can only be on boat if Cannibals > 0 (there is at least one cannibal on the right bank).
    • If Boat = 0, then missionaries can only be on boat if Missionaries < M (there is at least one missionary on the left bank), cannibals can only be on boat if Cannibals < C (there is at least one cannibal on the left bank).

    Here we also observe that after do +- A and +- B [second observation], despite whether the boat is on the left or on the right bank: 0 <= Missionaries <= M, 0 <= Cannibals <= C.

If the right bank has at least one missionary then check if they are outnumbered by cannibals, if they are then the state is not valid:

But we also want to check the left bank with the same fail condition:

Together with constraints in the third observation, we have the full condition of a valid state:

Before continuing, let me make an observation: If you run:

It will return X = 0 ; X = 1 ; X = 2.

So number A of missionaries on boat and number B of cannibals on boat can be generated through this generate predicate:

And here is our f – state transfer predicate:

Full program:

Run the program:


Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *