Algorithm series (iii) Monster and Monk crossing the river problem

Source: Internet
Author: User
Tags final

There are three monks (or missionaries) and three monsters (or Cannibal Monsters) crossing the river, there is only one ship that can hold two men (monks or monsters) on either side of the river or on board, and if the number of monsters is greater than the number of monks, then the monk is in danger of being eaten. Can you find a safe way of crossing the river?

This is a very interesting puzzle, but not difficult, each time you can choose a person or two people across the river, as long as the number of monks on either side of the river is always greater than or equal to the number of monsters. Here we first give a way to cross the river:

Two monsters first River, a monster back;

Two more monsters across the river, a monster come back;

Two monks cross the river, a genie and a monk come back;

Two monks across the river, a monster came back;

Two monsters across the river, a monster come back;

Two monsters across the river.

The way across the river in fact, more than this one, this paper gives a method to solve all the river crossing algorithm program, can be exhaustive (state tree search) method to get all four kinds of river crossing method.

The idea of solving problems

The initial condition of the topic was three monks and three monsters on one side of the river (and a boat), the termination condition after solving the problem is three monks and three monsters safely across the river, if the position of the monster and monk at any moment as a "state", the problem is to find a way from the initial state to the end state of the path. Starting from the initial state, each select a group of monsters or monks across the river (moving the boat once), will produce a new state from the original state, if the human mind to solve this problem, each will choose the best monster and monk combination across the river, making them across the river after the formation of the new state closer to the final state, and constantly repeat the process, Until the final state is achieved.

Using computer to solve the problem of Monster and Monk crossing the river It is also through state transitions to find a transition path from the initial state to the end state. The computer does not conduct rational analysis, do not know how to choose the best way to cross the river every time, but the computer is good at fast computing and fatigue, since do not know how to choose the way to the river, then simply put all the way across the river to try to find all possible results, of course, including the results of successful crossing the river.

This idea in fact and the "three bucket water" solution similar to, starting from the initial state, through the construction of a specific exhaustive algorithm to solve all the states in the solution space, get a tree with the initial state as the root of the state tree. If a leaf node in the state tree is the final state of the title requirement, then all state nodes from the root node to the leaf node are a solution to the problem of crossing the river.

Mathematical model of the state

This section explores how to establish a mathematical model of the state. The title requirement does not emphasize the difference between the three monsters or the three monks, but focuses on the number of them on both sides of the river, so there is no need to endow monks and goblins with too many attributes to determine the state of a particular moment by numerical representation of their number on both sides. In addition to the number of monks and monsters, there is also a key factor will affect the mathematical model, that is the state of the ship. For example, at a certain moment, there are two monks and two monsters on the local river, and there is a monk and a monster on the other side, at this time the ship is in the river and across the river is two completely different state. The state of the monk and the monster is the number, the ship has two states, in the local river (locally) and on the opposite shore (REMOTE), we use a five-dollar group to indicate a certain moment of the river State: [Local monk number, the number of local monsters, the opposite side monk number, the devil number on the other side, the ship's position] The initial state of the five-tuple group is [3, 3, 0, 0, local], and the problem-solving River State is [0, 0, 3, 3, REMOTE]. This state model is defined with C + + as follows:

struct itemstate   
       
{   
       
..... to     int local_monster;     int Local_monk;     Remote_monster int;     int Remote_monk;     boatlocation boat/*local or remote*/   
       
..... 37};

The state space of the subject is to [3, 3, 0, 0, local] as a state tree of the root, if a leaf node represents the state is the solution state [0, 0, 3, 3, REMOTE], then the direct relationship node from the root node to this node is all the intermediate states in the process of crossing, It is a solution process to export these intermediate states in sequence by parent-child relationship. As an example of a solution process given at the beginning of this article, the state transition process is shown in the following illustration:

Figure (1) A state transition process for a solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.