Algorithm series (14) Wolf, sheep, vegetable and farmer crossing the river problem

Source: Internet
Author: User

Topic Description: Farmers need to transport wolves, sheep, vegetables and their own to the other side of the river, only the farmers can row, and the boat is relatively small, except the farmer can only carry one thing at a time, there is a thorny problem, that is, if there is no farmer watching, sheep will eat food, the wolf eats sheep Please consider a way for the farmer to safely arrange these things and to cross his own river.

This topic examines the human fast logic operation and the short-term memory. Analysis, in the Wolf-"sheep-" food chain, "sheep" in the key position, the guiding ideology of solving the problem is to "sheep" and "wolf" and "food" is always in isolation, that is to say "sheep" should always be the last to be brought across the river. Come to an answer:

The farmer brought the sheep across the river

The farmer Returns

The farmer takes the wolf across the river

The farmer returned with the sheep

The farmer brought vegetables across the river

The farmer Returns

The farmer brought the sheep across the river

< end >

Look at one more answer:

The farmer brought the sheep across the river

The farmer Returns

The farmer brought vegetables across the river

The farmer returned with the sheep

The farmer takes the wolf across the river

The farmer Returns

The farmer brought the sheep across the river

< end >

The problem is to be solved around the sheep.

There are two answers mentioned above, so how many answers to this question? The answer also needs to be done with computers. The key to solving this problem with the computer is the state traversal, this and the "algorithm series-three bucket sharing water problem" in the article mentioned in the state traversal is a truth, in the final analysis is a finite state machine. Farmers, wolves, sheep and vegetables can have many states depending on their location, but the total number of States is still limited, and our algorithm is to traverse between these finite states until we find a "path" from the initial state to the termination state, and according to the requirements of the topic, this "path" Every state on the page should be a legitimate state.

Either state modeling or state-conversion algorithms are simpler than "sharing 8 liters of water with three buckets." First is the state, farmers, wolves, sheep and vegetables as four separate item, their state is very simple, either the river, or not across the river, at any time the state of each item only one. If use "Here" means not crossing the river, with "THERE" said has crossed the river, with [Farmers, wolves, sheep, vegetables] Four Yuan group to express the state of a moment, then the state space is to [Here,here,here,here] as the root of a state tree, when this shape A leaf node of a state tree is a state [there,there,there,there], which means that the sequence of States from the root to the leaf node is a solution to this problem.

The state-conversion algorithm for this subject is still a depth-first search for all states in the state space, because wolves, sheep and vegetables do not row, so the state conversion algorithm is also very simple, do not need to be like "with three buckets of 8 liters of water" problem to use the arrangement of the way to determine the conversion method (inverted water action), There are only 8 kinds of fixed state conversion operations (River Movement), respectively:

The farmer crossed the river alone;

The farmer took the wolf across the river;

The farmer carried the sheep across the river;

The farmer brought vegetables across the river;

The farmer returns alone;

The farmer returned with the wolf;

The farmer returned with the sheep;

Farmer with vegetable return;

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.