About the past 48 classic algorithms and 23 commonly used design patterns are not unfamiliar, but for a long time did not see and then looked at when also did not tidy up, now slowly sorted out (follow-up will slowly update, here is just the classic algorithm to organize):
Directory:
1. Hanoi Tower _towersofhanoi
2. The Fermi series _fibonacci
3. Bath Triangle
4. Tri-Color Chess
5. The mouse goes astray 1
6. The mouse goes Astray 2
7. Knight Walking Board _knighttour
8. Eight Queens
9. Eight pieces of silver
10. Life GamesLet's take a look at the basic instructions and solutions: 1. Hanoi Tower _towersofhanoi
Description
Hanoi Tower (Towers of Hanoi) is the French M.claus (Lucas) in 1883 from Thailand to France, Hanoi for the Vietnam War
The capital of North Vietnam, the present Ho Chi Minh City, the 1883 French mathematician Edouard Lucas has mentioned this story, it is said that the Genesis
Ji-Benares has a polo tower, which is supported by three diamond sticks (PAG), at the beginning of which God placed 64 on the first stick.
A gold Platter (DISC) arranged from top to bottom, and ordered the monks to move all the gold plates from the first stone rod to the third.
Stone rods, and in the process of handling the large plate under the principle of the small plate, if only one plate per day, when the plate is fully moved
When the ship is finished, the tower will be destroyed, and the end of the world is coming.
Solution
If the column is labeled ABC, move from A to C and move it directly to C when there is only one plate, when there are two disks
The B is treated as a secondary column. If the number of disks is more than 2, it is very simple to cover up the third plate, each place
Two plates, that is: A->b, A->c, b->c These three steps, and the covered part is actually into the program
of the recursive processing. In fact, if there are n plates, the number of times required to move is 2^n-1, so when the number of disks is 64 o'clock,
The number of times required is: 264-1 = 18446744073709551615 is 5.05390248594782e+16 years, that is, about 5000 centuries,
If there is no concept of this number, it is assumed that every second to move a plate good, also an offer of about 585 billion years.
2. The Fermi series _fibonacci
Description
Fibonacci, a 1200-year-old European mathematician, once mentioned in his work, "if there is a free child every month, a small free
One months later, the baby-free child also began production. At first, there was only one free son, one months later there were two free children, two months later three
Only the child, three months later, there are five free children (small free child into production) ....
If you do not understand this example, let's take a picture and notice that the newborn baby is going to be one months into the long-term.
Production, similar principles can also be used for plant growth, this is the Fibonacci series, the general habit is called the Fermi series, example
such as the following: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ...
Solution
According to the instructions, we can define the Fermi series as follows:
* Fn=fn-1+fn-2 If N>1
* Fn=n If n=0,1
3. Bath Triangle
4. Three-color chess (that is, the general people often say bubble sort, tricolour can be said to be sort Huang)
Description
The issue of the tri-color flag was first proposed by E.w.dijkstra, who used the term Dutch Nation flag (Dijkstra for the Netherlands
The majority of the authors use Three-color flag to call them.
Suppose there is a rope with red, white, and blue flags on it, and at first the color of the flag on the rope is not in order, you
Want to classify, and arranged in blue, white, red order, how to move the number of times will be the least, note you can only on the rope
This action, and only two flags can be swapped at a time.
*
Solution
Moving on a rope means that only one array can be used in the program, and no other array is used to assist, asking
The solution to the problem is simple, you can imagine moving the flag, from the beginning of the rope, encountered the blue move forward, encountered
White left in the middle, and encountered red backward, as shown below:
Just to make the least number of moves, you need some tricks:
If the position of W in the figure is white, then w+1, indicating that the unhandled part is moved to the white group.
If the W part is blue, then the elements of B and W are swapped, while B and W must be +1, indicating that two groups have one more element.
If W is located in red, the W is swapped with R, but R is reduced by 1, which means that the unhandled portion is minus 1.
Note that B, W, R is not the number of tricolour, they are just a moving indicator; What time does the move end? At first
The non-processed R indicator is equal to the total number of flags, and when the index number of R is reduced to less than W, the following flag is indicated
The child is all red, and you can end the move at this point.
5. The mouse goes astray 1
* Description
* Mouse Maze is the basic question of recursive solution, we use 2 in a two-dimensional array to represent the maze wall, use one to denote the mouse's walking path, try to find the path from the entrance to the exit by the program.
*
* Solution
* The mouse's way has the upper, the left, the bottom, the right four directions, after each forward one lattice chooses One direction to advance, cannot go forward the return chooses the next can go forward direction, so in the array the sequence measurement
* Try four directions until you reach the exit, this is the basic problem of recursion, please read the program directly should be understandable.
6. The mouse goes Astray 2
* Description
* Because of the maze design, the mouse to walk the maze of the entrance to the exit path may not be more than one, how to find all the path?
* Solution
* Ask all paths to look complicated but it's simpler, just show the path when the mouse goes to the exit, then go back to the previous one and select the next position to continue to pass.
* Simpler than finding a single path, our program just needs a little modification.
7. Knight Walking Board _knighttour
Description
* Knight Travel (Knight Tour) at the beginning of the 18th century much to the attention of mathematicians and jigsaw puzzles, when it was presented
It is not possible to take the test, the knight's Way for the western chess, the knight can be from any position, it how to walk through [all the bits
Reset
Solution
* Knight's Walk, basically can be used to solve the recursion, but the pure recursion in the dimensions of the large time is quite inefficient,
A clever solution was proposed by J.C Warnsdorff in 1823, simply put, the hardest place to go, the next road
It's wide, and the knight's going to take the next step, "for the next choice, the number of steps you can take is the fewest step." ", using this
method, in the case of not using recursion, there can be a higher probability of finding the way (there is no chance of finding a way).
8. Eight Queens
* Description
* The Queen of Western Chess can go straight forward, eat all the pieces encountered, if there are eight queens on the board, then the eight queen how to put the peace on the Board, 1970 and 1971, E.w.dijkstra and N.wirth used this problem to explain programming skills.
*
* Solution
* Questions about the chessboard can be solved with recursion, but how often do you reduce recursion? In the eight Queens problem, it is not necessary to check all the squares, for example, if a column is checked, the other lattice of the column is not checked again, this method is called Branch pruning.
*
9. Eight pieces of silver
* Description
* Existing eight silver coin a b c d E F g H, one is known to be counterfeit money, its weight is different from the real currency, but it is lighter or heavier, how to use the balance to the minimum number of comparisons, determine which is counterfeit currency, and learned that counterfeit money is lighter or heavier than real money.
*
* Solution
* It is not difficult to ask for counterfeit money alone, but the problem is limited by the minimum number of comparisons, so we cannot solve it with a simple loop comparison, we can use decision trees (decision tree) and use analysis and tree diagrams to assist in solving. A simple
The situation is this, we compare a+b+c and d+e+f, if equal, then the counterfeit currency must be g or H, we first compare G or H which is heavier, if G is heavier, and a comparison (A is real money), if G is equal to a, then G is true currency, H is counterfeit, because H is lighter than g
And G is real money, then H is the weight of counterfeit money than real currency light.
10. Life Games
* Description
* Life Game (game of Live) for 1970 by British mathematician J. H. Conway suggested that a cell's neighbors include the upper, lower, left, right, upper left, lower left, right and upper right adjacent cells, the rules of the game are as follows:
Solitary death: If the cell's neighbor is less than one, then the cell will die in the next state.
Crowding death: If the cell's neighbors are more than four, then the cell will die in the next state.
Stable: If the cell's neighbors are two or three, then the next state is stable to survive.
Resurrection: If there is no cell in a location, and the neighbor of that location is three, then that position will resurrect one cell.
* Solution
* The rules for Life games can be simplified to the following, and you can use the case comparison to implement the program:
When the number of neighbors is 0, 1, 4, 5, 6, 7, 8, the next state of the cell is death.
When the number of neighbors is 2 o'clock, the next state of the cell is resurrected.
When the number of neighbors is 3 o'clock, the next state of the cell is stable.
Code download
Classical algorithms-Review (top ten)