Hanoi Tower Problem Analysis

Source: Internet
Author: User
Preface I have been home for 3rd days, and my status has not been very good. I want to fill in my teeth and check my health. seeing my friends, I am worried about school moves. It is rare that I can spare time to think about it, here, we also read <cracking the coding interview> and saw the tower of death problem. Here we record the idea and analyze the tower of death as a classic recursive question. Here we will first introduce the key To Recursive use: The key to recursion is to define a function first. Do not try to implement it in a hurry, but clarify its functions.For the tower of Hanoi Problem, we define the following function prototype:
void hanoi(char src, char mid, char dst, int n)

We should not care about how it is implemented, but first clarify its functions:

Move n plates from column SRC to column DST, with the help of column mid (middle middleware)

Since recursion is used, the function itself is used in this function. that is to say, we will also use the haoni operation to complete this task, but the parameters may be different. we define a tuple to indicate the status of the three columns: (SRC, mid, DST). The number is the number of disks on each column:

  1. Initial status: (n, 0, 0)
  2. Target status: (0, 0, n-1)
  3. Intermediate status: (N, N-1, 0)
Only when the intermediate state exists can we move the largest plate N from SRC to DST, and then the n-1 plate on the Mid is moved back to SRC, so that the problem scale is reduced from N to n-1.
  1. From the initial status to the intermediate status, use the Hanoi (SRC, DST, mid, n-1) operation to move n-1 plate from SRC to mid.
  2. Next, move the disc N from SRC to DST to printf ("Move disk % d from % C to % C", N, SRC, DST );
  3. After the preceding operation is complete, the obtained status is (0, n-1, n)
  4. Next, we need to move the n-1 plate on the mid to DST and use Hanoi (MID, SRC, DST, n-1)
Recursive code
/*** In the classic problem of the towers of Hanoi, you have 3 rods and N disks of different * sizes which can slide onto any tower. the puzzle starts with disks sorted in ascending * order of size from top to bottom. you have the following constraints: * (a) only one disk can be moved at a time * (B) A disk is slid off the top of one rod onto the next rod * (c) a disk can only be placed on top A larger disk * write a program to move the disks from the first rod to the last using stacks */# include <stdio. h> # include <stdlib. h>/*** recursive code ** T = O (2 ^ n-1) Proof of derivation ***/void Hanoi (char SRC, char mid, char DST, int N) {If (n = 1) {printf ("Move disk % d from % C to % C \ n", N, SRC, DST );} else {Hanoi (SRC, DST, mid, n-1); printf ("Move disk % d from % C to % C \ n", N, SRC, DST ); hanoi (MID, SRC, DST, n -1) ;}} int main (void) {int N; while (scanf ("% d", & N )! = EOF) {Hanoi ('A', 'B', 'C', n);} return 0 ;}
Tower Deformation
About the end of the 19th century, I sold an intellectual toy in a store in ozhou, with three poles on a copper plate, on the leftmost bar, the Tower consists of 64 disks in ascending order. The purpose is to move all the disks on the leftmost bar to the right bar, with the condition that only one disk can be moved at a time and the tray cannot be placed on a small disk. Now we can change the gameplay. We cannot directly move from the leftmost (rightmost) side to the rightmost (leftmost) side (each move must be moved to or from the middle ), you cannot place the dashboard on the lower disk. Daisy has already done the original tower problem and Tower II, but when she encountered this problem, she thought for a long time and couldn't solve it. Please help her now. Now there are n discs. How many times does she move these discs from the leftmost to the rightmost? Input: contains multiple groups of data. Each time you enter an N value (1 <= n = 35 ). Output: for each group of data, the minimum number of output moves. Sample input: 1312 sample output: 226531440

We need to remember the essence of recursion:Define a function first. Do not care about implementation details. first define the function.

First, we will analyze several state changes (SRC, Bri, DST) in this tower deformation to indicate:

  1. (1 ~ N, 0, 0) --> initial state
  2. (N, 0, 1 ~ N-1) --> move n-1 plates from SRC to DST (first send to Bri and then send to DST)
  3. (0, N, 1 ~ N-1) --> move the nth plate from SRC to Bri
  4. (1 ~ N-1, n, 0) --> move n-1 plates on DST back to SRC
  5. (1 ~ N-1, 0, n) --> move the nth plate on BRI to DST
  6. (0, 0, 1 ~ N) --> move the remaining n-1 plates on SRC to DST (problem scale reduced)
We define a function:
void move(char src, char dst, int n)

The function is used to move n plates from SRC to dstsrc and DST:

Void send (char SRC, char DST, int num) {// SRC first moves to Bri, Bri then moves to DST}

Therefore, the above statuses can be expressed using the move and send functions. The specific recursive details should not be entangled in the code.

#include <stdio.h>#include <stdlib.h>long int count;void send(char src, char dst, int num){printf("Move %d disk from %c to %c!\n", num, src, dst);count += 1;}void move(char src, char dst, int n){if (n == 1) {send(src, 'B', n);send('B', dst, n);} else {move(src, dst, n - 1);send(src, 'B', n);move(dst, src, n - 1);send('B', dst, n);move(src, dst, n - 1);}}int main(void){int n;while (scanf("%d", &n) != EOF) {count = 0;move('A', 'C', n);printf("%ld\n", count);}return 0;}

Reference http://www.cnblogs.com/yanlingyin/archive/2011/11/14/2247594.html

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.