Topic Requirements
Problem Description: Three cattle three Tigers cross the river, the ship can only hold up to two animals, and the ship can not be empty on the way. On either shore, if the number of cows is less than the number of tigers, the cows will be eaten by tigers. In order to make all the animals cross the river and make no loss, please make a reasonable crossing plan.
Solution Solutions
This is also a classic cross-river problem, thus derived from the version of the merchant Servant (attendant) across the cross, the farmer monster across the river, missionary savage across the river ... In addition to changing roles, the content is essentially the same.
and Dynamic properties Static property is the ship docked on shore A or B shore, a shore cattle, tiger number (a shore number must be, B shore is also certain, so just consider one side on the line), dynamic property is the ship in operation, a shore or b shore cattle, tiger number.
Further consideration, as long as you know the adjacent two static properties, you will know the dynamic properties that occur in the meantime. For example, to start the ship on shore A, a shore cattle, tigers each three, the next state of the boat on the B shore, a shore of the Ox Tiger each two, then the dynamic property between them must be the ship from A to B, and transported a cow a tiger past (regardless of the situation of duplication ). So here, we just need to determine the corresponding static property of each step, which is described as state in programming . For the number of cattle or tigers on the shore, only 0 to 3 of these four values, for the ship's docking position, only a bank and B shore two cases, so that there is a 4*4*2=32 state. In these 32 states, some states cause cows to eat tigers, which must be ruled out.
In this case, the problem of developing a cross-river scheme is converted into the 32 states to seek a reasonable "State Path"the problem. US fromInitial State--a Shore Cattle Tiger each three, the ship on the shore a state, and constantlyJudgingencounter the nextStatusisReasonable. If the next state is reasonable, add it to the "State path" and leaveAccess Tokens (prevent duplicate additions, forming loops), otherwise, skip this state. In the process of continuous exploration, if you encounter a tag that is marked ashas been visitedstate, indicating that the status has been added to the path, the next possibility needs to be explored. If you encounter aEnd State--a Shore Bull Tiger is zero, ship on shore B, then found a complete "State Path ", you need to print the path, clear the current state of the access token, and exit the previous state to continue looking for the next possibility.
in specific programming, you can useStackTo Store all states of the initial state to the end state, in order to return to the original path in case of a non-compliant test instructions state, which facilitates backtracking. As the propulsion process, each State is based on the number of shore cattle and the ship's docking position, so it is easier to consider recursion. But it is worth noting that the recursion here is not as constant as the Fibonacci sequence, but more like "Tail recursion", the side recursion, the side advancing, thatRecursive Shell,the heart of the iteration.
Source code Example
results show
Summary
a classic cross-river problem, the essence is to seek to meet the test instructions state path between the states, in the case of multiple paths, you need to use the stack to save the previous state, in order to meet the conditions of non-conforming state can be traced back. This retrospective thought is reflected in the maze pathfinding and The eight Queens problem . Can be said to be a female compatriots, belonging to the same type of problem.
Cross-river problem (Ox Tiger River, merchant servant across the river, farmer monsters across the river, missionary Savage cross the River) (2nd session 2nd question)